How to provision AWS Lambda function using Terraform

Key takeaways:

  • Terraform automates deployment and management of AWS Lambda functions.

  • Terraform defines Lambda functions using HashiCorp Configuration Language (HCL).

  • Terraform simplifies serverless application management in AWS efficiently and scalably.

AWS Lambda is a powerful serverless computing platform that allows you to run code without provisioning or managing servers. However, manually managing the deployment and configuration of Lambda functions can be time-consuming and error-prone. Terraform, an infrastructure as a code tool, provides a solution to streamline this process. By using Terraform, you can automate the creation, modification, and deletion of Lambda functions, ensuring consistency and reducing the risk of human error.

AWS Lambda

AWS Lambda is a serverless compute service that lets us trigger the execution of code without managing any servers. It is used to run backend processes that do not require any server or to automate any tasks. We can do all this in our favorite programming languages like Python, Java, or C#.

AWS Lambda
AWS Lambda

Terraform

Terraform is an infrastructure as code software that enables us to automate the creation and upgradation of infrastructure. It is created by HashiCorp and uses HashiCorp Configuration Language for configurations which is similar to JSON.

Terraform is used to manage services in many could platforms, including AWS, Google Cloud, and Azure.

Terraform
Terraform

AWS terraform Lambda function

AWS Terraform Lambda function combines Terraform and AWS Lambda, which allows us to deploy and manage AWS Lambda functions using Terraform.

Using Terraform with Lambda functions

Let’s create an AWS lambda function using Terraform.

First, we will install Terraform using the terraform init command. Then, we create a new Lambda function by creating a new file lambda.tf. We define the Lambda function as follows.

Follow these commands to execute the following code:

  1. After clicking run, enter the command terraform init.

  2. After that, enter the command terraform plan.

  3. Finally, enter the command terraform apply —auto-approve.

import json

def lambda_handler(event, context):
    # Print to CloudWatch Logs
    print("Hello from Lambda")
    
    # Return a response
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda')
    }
  • Lines 6–12: This code provisions an AWS Lambda function with the filename my_lambda_function.zip, function name my_lambda_function, runtime python3.12, and handler lambda.lambda_handler. It also creates an AWS IAM role named my_lambda_role with an assume role policy allowing the Lambda service to assume the role.

  • Lines 34–47: An AWS IAM policy named lambda_execution_policy is created with permissions to perform logging actions (logs:CreateLogGroup, logs:CreateLogStream, logs:PutLogEvents) on all resources, which is necessary for the Lambda function to log events to CloudWatch.

  • Lines 53–56: The IAM policy is attached to the IAM role my_lambda_role using the aws_iam_role_policy_attachment resource, which grants the Lambda function the necessary permissions for logging.

Terraform simplifies the deployment and management of Lambda function and other serverless applications in AWS in an efficient and scalable way.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is the memory size of Lambda terraform?

The memory size of a Lambda function in Terraform can be configured using the memory_size argument within the aws_lambda_function resource. It can be set to a value between 128 MB and 10,240 MB (10 GB) in 64 MB increments. This memory allocation affects the function’s computational resources and execution time.  

Additional note: While the memory size determines the computational resources, the ephemeral storage size (configured separately) determines the amount of temporary storage available to your function for file operations, caching, or other data processing tasks.


How do I import a function into AWS Lambda?

To import an existing Lambda function into Terraform, you can use the Terraform import command. Here’s the basic syntax:  

terraform import aws_lambda_function.<function_name> <function_arn>  

Replace <function_name> with the desired name for the resource in your Terraform configuration and <function_arn> with the actual ARN of the Lambda function in AWS.


How do I deploy a function to AWS Lambda?

To deploy a Lambda function using Terraform, you typically follow these steps:

  • Create a Terraform configuration: Define the Lambda function’s configuration, including its code, runtime, memory size, timeout, and other relevant parameters.
  • Initialize Terraform: Run terraform init to initialize the working directory and download the necessary plugins.
  • Plan the deployment: Run terraform plan to review the changes that will be made to your infrastructure.
  • Apply the configuration: Run terraform apply to create or update the Lambda function and its associated resources.

Terraform will handle the deployment process, including uploading the function’s code, configuring triggers, and setting permissions.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved