How to Create a Lambda in Terraform


To create an AWS Lambda function using Terraform, you need to define the necessary resources in a Terraform configuration file. Here’s an example of how you can create a Lambda function using Terraform:

Option 1 - Seperate Lambda Source

  1. Create a new directory for your Terraform configuration and navigate to it in your terminal.
  2. Create a new file with a .tf extension, such as lambda.tf, and open it in a text editor.
  3. In the lambda.tf file, add the following code:
provider "aws" {
  region = "us-east-1"  # Replace with your desired AWS region
}

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

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

  // Replace with the appropriate values for your Lambda function
  environment {
    variables = {
      KEY   = "VALUE"
      KEY2  = "VALUE2"
    }
  }
}

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

resource "aws_iam_role_policy_attachment" "lambda_policy_attachment" {
  role       = aws_iam_role.lambda_role.name
  policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
  1. In the above code, you can customize the following parts:
  • region: Specify the AWS region where you want to create the Lambda function.
  • runtime: Specify the runtime environment for your Lambda function (e.g., nodejs14.x, python3.8, etc.).
  • filename: Update the path to your Lambda function code. Ensure that the code is packaged in a ZIP file.

You can also modify the environment variables section (KEY and VALUE) according to your requirements.

  1. Save the lambda.tf file.

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

terraform init
  1. Once the initialization is complete, you can create the Lambda function by running the following command:
terraform apply

Terraform will analyze the configuration, prompt for confirmation, and then create the necessary resources, including the Lambda function.

Option 2 - Inline Lambda Source

An inline Lambda function means that the function code is defined directly in the Terraform configuration file instead of referencing an external code file. Here’s an example of how you can create an inline Lambda function using Terraform:

  1. Follow steps 1 and 2 from the previous answer to set up your Terraform configuration.
  2. In the lambda.tf file, add the following code:
provider "aws" {
  region = "us-east-1"  # Replace with your desired AWS region
}

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

  // Define the inline Lambda function code
  inline_code = <<-EOT
    exports.handler = async function(event, context) {
      console.log("Hello, inline Lambda function!");
      return {
        statusCode: 200,
        body: "Hello from inline Lambda!"
      };
    };
  EOT

  // Replace with the appropriate values for your Lambda function
  environment {
    variables = {
      KEY   = "VALUE"
      KEY2  = "VALUE2"
    }
  }
}

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

resource "aws_iam_role_policy_attachment" "lambda_policy_attachment" {
  role       = aws_iam_role.lambda_role.name
  policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
  1. Customize the code as needed, such as the region, function_name, runtime, and environment variables.
  2. Save the lambda.tf file.
  3. Initialize the Terraform configuration by running terraform init in your terminal.
  4. Once the initialization is complete, create the Lambda function by running terraform apply.

Terraform will analyze the configuration, prompt for confirmation, and create the inline Lambda function using the code provided in the inline_code block.

That’s it! You have now created an inline Lambda function using Terraform.