Home Software Engineering Easy methods to create a Lambda in Terraform

Easy methods to create a Lambda in Terraform

0
Easy methods to create a Lambda in Terraform

[ad_1]

To create an AWS Lambda operate utilizing Terraform, you could outline the required assets in a Terraform configuration file. Right here’s an instance of how one can create a Lambda operate utilizing Terraform:

Possibility 1 – Seperate Lambda Supply

  1. Create a brand new listing in your Terraform configuration and navigate to it in your terminal.
  2. Create a brand new file with a .tf extension, comparable to lambda.tf, and open it in a textual content editor.
  3. Within the lambda.tf file, add the next code:
supplier "aws" {
  area = "us-east-1"  # Substitute together with your desired AWS area
}

useful resource "aws_lambda_function" "my_lambda" {
  function_name = "my-lambda-function"
  position          = aws_iam_role.lambda_role.arn
  handler       = "index.handler"
  runtime       = "nodejs14.x"  # Substitute together with your desired runtime

  // Substitute with the trail to your Lambda operate code
  filename      = "path/to/lambda/code.zip"

  // Substitute with the suitable values in your Lambda operate
  setting {
    variables = {
      KEY   = "VALUE"
      KEY2  = "VALUE2"
    }
  }
}

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

useful resource "aws_iam_role_policy_attachment" "lambda_policy_attachment" {
  position       = aws_iam_role.lambda_role.title
  policy_arn = "arn:aws:iam::aws:coverage/service-role/AWSLambdaBasicExecutionRole"
}
  1. Within the above code, you may customise the next components:
  • area: Specify the AWS area the place you need to create the Lambda operate.
  • runtime: Specify the runtime setting in your Lambda operate (e.g., nodejs14.x, python3.8, and many others.).
  • filename: Replace the trail to your Lambda operate code. Be certain that the code is packaged in a ZIP file.

You can even modify the setting variables part (KEY and VALUE) in line with your necessities.

  1. Save the lambda.tf file.

  2. Initialize the Terraform configuration by operating the next command in your terminal:

terraform init
  1. As soon as the initialization is full, you may create the Lambda operate by operating the next command:
terraform apply

Terraform will analyze the configuration, immediate for affirmation, after which create the required assets, together with the Lambda operate.

Possibility 2 – Inline Lambda Supply

An inline Lambda operate signifies that the operate code is outlined instantly within the Terraform configuration file as a substitute of referencing an exterior code file. Right here’s an instance of how one can create an inline Lambda operate utilizing Terraform:

  1. Comply with steps 1 and a pair of from the earlier reply to arrange your Terraform configuration.
  2. Within the lambda.tf file, add the next code:
supplier "aws" {
  area = "us-east-1"  # Substitute together with your desired AWS area
}

useful resource "aws_lambda_function" "my_lambda" {
  function_name = "my-lambda-function"
  position          = aws_iam_role.lambda_role.arn
  handler       = "index.handler"
  runtime       = "nodejs14.x"  # Substitute together with your desired runtime

  // Outline the inline Lambda operate code
  inline_code = <<-EOT
    exports.handler = async operate(occasion, context) {
      console.log("Good day, inline Lambda operate!");
      return {
        statusCode: 200,
        physique: "Good day from inline Lambda!"
      };
    };
  EOT

  // Substitute with the suitable values in your Lambda operate
  setting {
    variables = {
      KEY   = "VALUE"
      KEY2  = "VALUE2"
    }
  }
}

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

useful resource "aws_iam_role_policy_attachment" "lambda_policy_attachment" {
  position       = aws_iam_role.lambda_role.title
  policy_arn = "arn:aws:iam::aws:coverage/service-role/AWSLambdaBasicExecutionRole"
}
  1. Customise the code as wanted, such because the area, function_name, runtime, and setting variables.
  2. Save the lambda.tf file.
  3. Initialize the Terraform configuration by operating terraform init in your terminal.
  4. As soon as the initialization is full, create the Lambda operate by operating terraform apply.

Terraform will analyze the configuration, immediate for affirmation, and create the inline Lambda operate utilizing the code offered within the inline_code block.

That’s it! You may have now created an inline Lambda operate utilizing Terraform.

[ad_2]