Create a Python function in AWS Lambda
provider "aws" {
region = "us-west-2"
}
resource "aws_lambda_function" "example" {
filename = "example_function.zip"
function_name = "example_function"
handler = "example_function.handler"
runtime = "python3.6"
role = "${aws_iam_role.example.arn}"
}
resource "aws_iam_role" "example" {
name = "example_role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "lambda.amazonaws.com"
}
}
]
})
}
In this example, we create two AWS resources:
aws_lambda_function.example
defines a Lambda function that executes a Python file namedexample_function
, which is namedexample_function.zip
. This function uses the AWS Identity and Access Management (IAM) roleaws_iam_role.example
.aws_iam_role.example
defines an IAM role to provide permissions for Lambda functions
To use this code, make sure you have Terraform installed and configured with AWS credentials. Name the configuration file example.tf
using the following command, replacing YOUR_ACCESS_KEY
and YOUR_SECRET_KEY
with your AWS credentials:
terraform init
terraform apply \
-var access_key=YOUR_ACCESS_KEY \
-var secret_key=YOUR_SECRET_KEY
Terraform will prompt you to confirm the plan. If all goes well, it will output a success message and create the Lambda function and its associated IAM role
Post comment 取消回复