AWS Lambda Using Terraform

I am a Lead Engineer from India. Love to blog about serverless and help teams design and develop serverless architecture. An AWS cloud practitioner. Blogs about AWS Services utilising AWS CDK, CloudFormation, Terraform.
Search for a command to run...

I am a Lead Engineer from India. Love to blog about serverless and help teams design and develop serverless architecture. An AWS cloud practitioner. Blogs about AWS Services utilising AWS CDK, CloudFormation, Terraform.
No comments yet. Be the first to comment.
Transformers, the backbone of many state-of-the-art NLP models such as BERT, GPT has revolutionized the way we approach natural language understanding tasks. One key innovation in transformers is their ability to handle entire sequences of tokens sim...

When an User prompts the questions to the LLM, it produces the response based on the training data. The pre-trained LLM data is its parametric data. It might not have an context specific to our query. LLM still produces an answer confidently. It is t...

In the fast-evolving field of artificial intelligence, Large Language Models (LLMs) have rapidly transformed how we approach language processing. These models, such as ChatGPT, Claude, and Bard, have proven their utility across a range of application...

Amazon S3 is a powerful, scalable storage service, but when multiple clients or processes attempt to write to the same S3 object simultaneously, things can get tricky. Since S3 doesn’t natively handle concurrent writes or provide object locking, it’s...

When working with distributed systems in a microservice architecture, we must always architect our services with failure in mind. When the transaction happens between a microservice, we have to handle the failures. The Saga pattern can be used in a f...

Terraform allows infrastructure to be expressed as code, in a language called HCL (HashiCorp Configuration Language). Terraform registry contains various Terraform providers and modules for quickly deploying the common infrastructure configurations.
Providers are the plugins that will implement the resource types. We can find many different cloud providers which include major cloud providers like AWS, Azure, GCP.
Modules are the small, reusable Terraform configurations that let us manage a group of related resources as a single resource.
In this blog post, we will use aws provider and the module terraform-aws-modules/lambda/aws to create lambda and lambda layers.
Basically there are three steps to deliver the infrastructure as Code.
Write the infrastructure as code in configuration files using HashiCorp Configuration Language, then run the command terraform init to initialize the terraform.
Before provisioning the infrastructure, check if a plan matches the expectation using a command terraform plan
Once the plan is verified, apply the changes to get the desired infrastructure components using the command terraform apply
mkdir terraform-provider-module && cd terraform-provider-module
mkdir resources
Create a folder profile-generator-layer in the resources folder
mkdir profile-generator-layer
mkdir nodejs && cd nodejs
touch package.json
npm install aws-sdk faker
Create a folder profile-generator-lambda in the resources folder and add the index.js file with the below content.
mkdir profile-generator-lambda && cd profile-generator-lambda
touch index.js
const faker = require("faker/locale/en_IND");
exports.handler = async (event, context) => {
let firstName = faker.name.firstName();
let lastName = faker.name.lastName();
let phoneNumber = faker.phone.phoneNumber();
let vehicleType = faker.vehicle.vehicle();
let response = {
firstName: firstName,
lastName: lastName,
phoneNumber: phoneNumber,
vehicleType: vehicleType,
};
return {
statusCode: 200,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
profile: response,
}),
};
};
Now, we will write the terraform scripts in HCL language where we will utilize the AWS provider. Create a main.tf file in the root directory of the project and add the below content.
touch main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "3.50.0"
}
}
}
provider "aws" {
# Configuration options
region = var.region
profile = var.aws_profile
shared_credentials_file = var.shared_credentials_file
default_tags {
tags = var.tags
}
}
Each Terraform module must declare which providers it requires so that Terraform can install and use them. Provider requirements are declared in a _requiredproviders block.
AWS providers require configurations like cloud regions, profiles, credential_files to be available before they can be used.
All the values will be provided from the variables parameters.
Let's create a variables.tf in the root directory of the project and add the below content.
touch variables.tf
variable "region" {
description = "Deployment Region"
default = "ap-south-1"
}
variable "aws_profile" {
description = "Given name in the credential file"
type = string
default = "rahul-admin"
}
variable "shared_credentials_file" {
description = "Profile file with credentials to the AWS account"
type = string
default = "~/.aws/credentials"
}
variable "tags" {
description = "A map of tags to add to all resources."
type = map(string)
default = {
application = "Learning-Tutor"
env = "Test"
}
}
Each input variable accepted by a module must be declared using a variable block. The label after the variable keyword is a name for the variable, which must be unique among all variables in the same module.
Now, we will use the module terraform-aws-modules/lambda/aws to create lambda and lambda layer infrastructure.
Create the files lambda.tf and lambda_layer.tf in the root directory of the project and add the below content.
touch lambda.tf lambda_layer.tf
module "profile_generator_lambda" {
source = "terraform-aws-modules/lambda/aws"
version = "2.7.0"
# insert the 28 required variables here
function_name = "profile-generator-lambda"
description = "Generates a new profiles"
handler = "index.handler"
runtime = "nodejs14.x"
source_path = "${path.module}/resources/profile-generator-lambda"
layers = [module.profile_generator_lambda_layer.lambda_layer_arn]
tags = {
Name = "profile-generator-lambda"
}
}
module "profile_generator_lambda_layer" {
source = "terraform-aws-modules/lambda/aws"
create_layer = true
layer_name = "profile-generator-layer"
description = "The Faker Dependency layer"
compatible_runtimes = ["nodejs14.x", "nodejs12.x"]
source_path = "${path.module}/resources/profile-generator-layer"
}
source field is used to indicate the terraform module which we intend to use.
_functionname field is the unique name for our lambda function.
handler field is the Lambda Function entry point for our code.
runtime field is Lambda Function runtime
_sourcepath field is the absolute path to a local file or directory containing our Lambda source code
_layername field is the name of Lambda Layer to create.
layers field is a list of Lambda Layer Version ARNs (maximum of 5) to attach to our Lambda Function.
Let us run the 3 basic commands of terraform to create the resources in AWS.
terraform init
terraform plan -out "plan-out"

terraform apply "plan-out"



As our lambda does not require any input, we can just create an empty JSON and Click on the Test button.

As we can see from the above screen, the lambda function returns a Body with a fake profile and a status code of 200.
In this blog post, we saw how to create a lambda layer and lambda function and how to package them. We also see how to use the Terraform providers and modules. We learned how to use the terraform commands to deploy our infrastructure on AWS. The associated Git Repository is here