How to develop a Lambda in Terraform

To develop an AWS Lambda function utilizing Terraform, you require to specify the needed resources in a Terraform setup file. Here’s an example of how you can develop a Lambda function utilizing Terraform:

Choice 1 – Seperate Lambda Source

  1. Produce a brand-new directory site for your Terraform setup and browse to it in your terminal.
  2. Produce a brand-new file with a tf extension, such as lambda.tf, and open it in a full-screen editor.
  3. In the lambda.tf file, include the following code:
 supplier "aws" {
area="us-east-1" # Change with your wanted AWS area
}

resource "aws_lambda_function" "my_lambda" {
function_name="my-lambda-function".
function = aws_iam_role. lambda_role. arn.
handler="index.handler".
runtime="nodejs14.x" # Change with your wanted runtime.

// Change with the course to your Lambda function code.
filename="path/to/lambda/ code.zip".

// Change with the proper worths for your Lambda function.
environment {
variables = {
SECRET="WORTH".
KEY2="VALUE2".
}
}
}

resource "aws_iam_role" "lambda_role" {
name="my-lambda-role".
assume_role_policy = <<< < EOF.
{
" Variation": "2012-10-17",.
" Declaration":[
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "lambda.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
EOF.
}

resource "aws_iam_role_policy_attachment" "lambda_policy_attachment" {
function = aws_iam_role. lambda_role. name.
policy_arn="arn: aws: iam:: aws: policy/service-role/AWSLambdaBasicExecutionRole".
}
  1. In the above code, you can personalize the following parts:
  • area: Define the AWS area where you wish to develop the Lambda function.
  • runtime: Define the runtime environment for your Lambda function (e.g., nodejs14.x, python3.8, and so on).
  • filename: Update the course to your Lambda function code. Guarantee that the code is packaged in a ZIP file.

You can likewise customize the environment variables area ( SECRET and WORTH) according to your requirements.

  1. Conserve the lambda.tf file.

  2. Initialize the Terraform setup by running the following command in your terminal:

 terraform init.
  1. Once the initialization is total, you can develop the Lambda function by running the following command:
 terraform use.

Terraform will examine the setup, trigger for verification, and after that develop the needed resources, consisting of the Lambda function.

Choice 2 - Inline Lambda Source

An inline Lambda function suggests that the function code is specified straight in the Terraform setup file rather of referencing an external code file. Here's an example of how you can develop an inline Lambda function utilizing Terraform:

  1. Follow actions 1 and 2 from the previous response to establish your Terraform setup.
  2. In the lambda.tf file, include the following code:
 supplier "aws" {
area="us-east-1" # Change with your wanted AWS area.
}

resource "aws_lambda_function" "my_lambda" {
function_name="my-lambda-function".
function = aws_iam_role. lambda_role. arn.
handler="index.handler".
runtime="nodejs14.x" # Change with your wanted runtime.

// Specify the inline Lambda function code.
inline_code = <<<

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: