Open In App

AWS Lambda Deployments with AWS CloudFormation

Last Updated : 17 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

AWS CloudFormation allows to create AWS resources with help of templates. It provisions and deploy all the resources required for the entire application described as a Stack. Templates can be updated and replicated as required to reduce the overhead of implementing and provisioning infrastructure. let’s learn how we can provision lambda deployments with AWS CloudFormation.

What Is CloudFormation?

CloudFormation is a service provided by AWS that allows to deploy and manage cloud resources through templates. Templates are JSON or YAML files containing configuration details for various cloud resources. Generally, templates contain configuration for the entire application stack. CloudFormation allows you to create templates only once and use them multiple times as required.

What Are AWS Lambda Deployments?

AWS Lamda Deployments are deployment of applications on AWS lambda. It involves creation of lambda functions, uploading application code to functions along with dependencies. Lambda Functions are serverless components that execute code only when the particular trigger is invoked. the trigger for functions has to configured as requirement.

Primary Terminologies Of AWS Lambda And CloudFormation

  • AWS CloudFormation: AWS service to provision infrastructure with the help of configurable templates.
  • Templates: It is a JSON or YAML file containing specification for resources to be created.
  • Stack: Contains related AWS resources to be created using CloudFormation.
  • Lambda Functions: Lambda Functions are serverless components that execute code without pre provisioning of infrastructure or servers.
  • Function Trigger: It is an event that invokes lambda function and executes it. For e.g. HTTP Trigger, Message queues etc.

AWS Lambda Deployments with AWS CloudFormation: A Step-By-Step Guide

To create lambda deployments first create and design lambda functions and other required resources as a part of deployment. For this article we will deploy a sample lambda function as a deployment.

Step 1: Design Handler

  • Implement a handler for sample lambda function.
exports.handler = async (event) => {
console.log('Received event:', JSON.stringify(event, null, 2));
return 'Hello World!';
};

Step 2: Create Template

Now let’s create a CloudFormation template with lambda function configuration.

  • Create a YAML file locally.
  • Now lets add configuration for lambda function. We will add minimum required configuration along with handler described earlier.
ExLambdaFunction:
Type: 'AWS::Lambda::Function'
Properties:
Handler: index.handler
Role: !GetAtt LambdaExecutionRole.Arn
Code:
ZipFile: |
exports.handler = async (event) => {
console.log('Received event:', JSON.stringify(event, null, 2));
return 'Hello World!';
};
Runtime: nodejs20.x
  • The above configuration specifies LambdaExecutionRole which will be used for pushing logs to CloudWatch.
  • We have also specified code with handler mentioned above.
  • Now let’s add configuration for LambdaExecutionRole.
LambdaExecutionRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: 'sts:AssumeRole'
Policies:
- PolicyName: LambdaExecutionPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'logs:CreateLogGroup'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource: 'arn:aws:logs:*:*:*'
  • LambdaExecutionRole contains permissions to create logs , logstream and push logs to it. We have attached lambdaExecutionPolicy to it.
  • Finally save the file and proceed with stack creation. Complete File will look like below.
AWSTemplateFormatVersion: '2010-09-09'
Description: 'AWS CloudFormation template for deploying a Lambda function'

Resources:
ExLambdaFunction:
Type: 'AWS::Lambda::Function'
Properties:
Handler: index.handler
Role: !GetAtt LambdaExecutionRole.Arn
Code:
ZipFile: |
exports.handler = async (event) => {
console.log('Received event:', JSON.stringify(event, null, 2));
return 'Hello World!';
};
Runtime: nodejs20.x

LambdaExecutionRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: 'sts:AssumeRole'
Policies:
- PolicyName: LambdaExecutionPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'logs:CreateLogGroup'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource: 'arn:aws:logs:*:*:*'

Step 3: Deploy The CloudFormation Stack

  • On AWS CloudFormation overview page click on create stack.

Deploying The CloudFormation Stack

  • Now specify the existing template. In the next option select Upload a template File. Choose YAML file from device and upload.

Uploading The Yaml File

  • Click On Next.
  • On Next page specify name for CloudFormation stack and specify other parameters if any.

Specifying Stack Details

  • Now on next step configure other advanced options for stack. For this article leave everything to default and click on next.
  • Finally review the details and click on create.
  • Before creation acknowledge the checkbox as template is going to create a IAM Role.

Reviewing And Creating The  CloudFormation

Step 4: Monitoring Stack Creation

  • Now the resource provisioning will be initiated.

Monitoring The Stack Creation

  • On successful creation you will see CREATE_COMPLETE as below. If you get any other error check events for more details.

Creating LambdaFunctions

Step 5: Test Lambda Function

  • Go to AWS Lambda under functions you should see created Lambda function for CloudFormation stack.

Testing The Lambda Function

  • Scroll down to the code tab of lambda function to see the handler that was created through template.
  • Now click on test to test the lambda function. Event creation page should open. Give any name to event and click save.

Configuring Test Event

  • Once the event is created click on test. You should see response as below.

Testing The Lambda Function

Step 6: Updating The Stack

  • Update the yaml file to update the stack. For now we will change handler to print “Hello Folks!”. The file should look like below.
AWSTemplateFormatVersion: '2010-09-09'
Description: 'AWS CloudFormation template for deploying a Lambda function'

Resources:
ExLambdaFunction:
Type: 'AWS::Lambda::Function'
Properties:
Handler: index.handler
Role: !GetAtt LambdaExecutionRole.Arn
Code:
ZipFile: |
exports.handler = async (event) => {
console.log('Received event:', JSON.stringify(event, null, 2));
return 'Hello Folks!';
};
Runtime: nodejs20.x

LambdaExecutionRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: 'sts:AssumeRole'
Policies:
- PolicyName: LambdaExecutionPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'logs:CreateLogGroup'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource: 'arn:aws:logs:*:*:*'
  • Now go to stack overview page and click on update.

Updating The LambdaFunction

  • Specify replace existing template and reupload the updated template file. proceed while leaving everything else as default.

Updating The Stack with Replacing Template

  • Finally click on update. It should start updation. Monitor the updation events as below .

Updating The Events In The LambdaFunction

  • If update successful you should see below output.

Updating The LambdaFunction Events

  • Once stack is updated, test the updated lambda function as mentioned in previous step. You should see output as below.

Test The Updated Lambda Function

Conclusion

Thus, we have deployed lambda deployment with the help of CloudFormation template. CloudFormation can be used to provision whole resource stack for entire application. Above template can be further modified to add additional resources as required.

AWS Lambda Deployments With AWS CloudFormation – FAQ’s

What Are Other Alternatives For Cloudformation?

Terraform, Pulumi , Chef and Ansible are Infrastructure as a code alternative which can be used instead of CloudFormation.

What Are Some Features Of Cloudformation?

Below are some features of CloudFormation?

  • Infrastructure as a code
  • Templates
  • Stack Managements
  • Automated Resource Provisioning
  • Rollback Protection

What Are Other Ways To Deploy Lambda Deployments?

We can use AWS SDK, AWS CLI, AWS Serverless application model for deployments lambda workloads.

What Are Some Features Of AWS Lambda Deployments?

Below are some features of AWS Lambda Deployments:

  • Serverless Computing
  • Automatic Scaling
  • Pay as per use model
  • High Availability

What Are Nested Stacks In Cloudformation?

AWS CloudFormation supports nested stacks, which allow you to create stacks that contain other stacks as resources. Use nested stacks to encapsulate reusable components or shared infrastructure patterns and manage them as a single unit.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads