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 named example_function, which is named example_function.zip. This function uses the AWS Identity and Access Management (IAM) role aws_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

点赞(0)

评论列表 共有 0 评论

暂无评论

微信服务号

微信客服

淘宝店铺

support@elephdev.com

发表
评论
Go
顶部