My first AWS lambda function using Terraform

Anand Bhaskaran
3 min readJan 17, 2022
Lambda (Python) + Terraform

What is a Lamda function?

AWS Lambda is a serverless, event-driven compute service that lets you run code for virtually any type of application or backend service without provisioning or managing servers. You can trigger Lambda from over 200 AWS services and software as a service (SaaS) applications and only pay for what you use.

What is Terraform?

Terraform is an open-source infrastructure as a code software tool that provides a consistent CLI workflow to manage hundreds of cloud services.

Prerequisites :

  • An AWS account
  • AWS credentials on your computer
  • Terraform Installed on your computer

Step 1: Create your lambda function

Let's create a new file called lambda.py and add the following code:

import jsondef handler(event, context):
print("Received event: " + json.dumps(event, indent=2))

This function gets the entire event object and just pretty print it.

Step 2: Find a name for your s3 bucket to store the lambda function

  • Navigate to S3 in your AWS console
  • Find a bucket name that is available.
    NOTE: Do not create a bucket yet. Lets create it with terraform

Step 3: Create first terraform file

  • Create a new file named main.tf
The file structure in my computer
  • Paste contents of the main.tf from here: https://github.com/anandbhaskaran/lambda-terraform/blob/main/my-first-lambda/main.tf
  • In this file, change the AWS region and the bucket name (from step2)
  • I will create a new Medium story explaining the contents of the file but this is what it does in the nutshell:
    * Creates the lambda bucket
    * Zip the lambda function and upload it in S3
    * Creates an IAM Role and Policy to execute the lambda function
    * Creates the AWS lambda function

Step 4: Create the infrastructure

  • Open the command prompt from the folder that contains the main.tffile
  • Execute the command terraform init
  • Just in a few seconds, terraform will install all its dependencies
  • Now execute terraform apply
    * It will show the list of resources to be created
    * Type yes to confirm
    * Just in a second, terraform will create all the resources for you

Step 5: Verify the lambda function

  • Navigate to the S3 and you will see the bucket is created and it contains a Zip file of your lambda function
  • Navigate to Lambda and you will see the function that you created
  • Lets now go to the test tab:
Test tab on the newly created Lambda function
  • Feel free to adapt the body of the execution and click Test
  • Just in a sec, you will see the output
The output of the lambda execution

Step 6: Destroy the infrastructure

  • In the terminal type terraform destroy
  • This command will delete all the created infrastructure

You can also find the entire source code in this repository: https://github.com/anandbhaskaran/lambda-terraform/tree/main/my-first-lambda

--

--

Anand Bhaskaran

I am a software engineer and a investment enthusiast