Deploying a Lambda function using AWS CloudFormation is a powerful way to manage your infrastructure as code and automate the provisioning of resources in AWS. In this detailed article, we’ll walk you through the steps required to deploy a Lambda function using AWS CloudFormation.
Prerequisites:
Before we begin, make sure you have the following prerequisites:
AWS Account: You’ll need an AWS account to access the AWS Management Console and create resources.
AWS CLI: Install the AWS Command Line Interface (CLI) and configure it with your AWS credentials.
AWS CloudFormation Template: Prepare a CloudFormation template in YAML or JSON format that defines the resources you want to create, including the Lambda function.
Step 1: Create a Lambda Function
Before deploying the Lambda function with CloudFormation, you need to create the function itself. You can do this either through the AWS Management Console or by using the AWS CLI. Here’s a basic example using the AWS CLI:
aws lambda create-function \ --function-name MyLambdaFunction \ --runtime python3.8 \ --handler index.handler \ --role arn:aws:iam::YOUR_ACCOUNT_ID:role/YourLambdaRole \ --zip-file fileb://lambda-code.zip
This command creates a Lambda function named MyLambdaFunction
using Python 3.8 as the runtime. Replace YOUR_ACCOUNT_ID
with your AWS account ID and provide the appropriate IAM role for the Lambda function. Make sure to replace lambda-code.zip
with your Lambda deployment package.
Step 2: Create a CloudFormation Template
Now, let’s create a CloudFormation template to define the Lambda function and any other resources you may need. Below is a simple example of a CloudFormation template that deploys a Lambda function:
AWSTemplateFormatVersion: '2010-09-09'
Description: Lambda Function Deployment
Resources:
MyLambdaFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: MyLambdaFunction
Handler: index.handler
Role: arn:aws:iam::YOUR_ACCOUNT_ID:role/YourLambdaRole
Code:
S3Bucket: my-lambda-deployment-bucket
S3Key: lambda-code.zip
Runtime: python3.8
Save this template to a file, such as lambda-cloudformation.yaml
. Replace YOUR_ACCOUNT_ID
, my-lambda-deployment-bucket
, and lambda-code.zip
with appropriate values.
Step 3: Deploy the CloudFormation Stack
Now that you have your CloudFormation template, it’s time to deploy it to create the Lambda function and any associated resources.
Open a terminal window and run the following command to create a CloudFormation stack:
aws lambda create-function \
–function-name MyLambdaFunction \
–runtime python3.8 \
–handler index.handler \
–role arn:aws:iam::YOUR_ACCOUNT_ID:role/YourLambdaRole \
–zip-file fileb://lambda-code.zip
This command creates a CloudFormation stack named MyLambdaStack
using the template you provided. The --capabilities CAPABILITY_IAM
flag is required because it creates an IAM role for the Lambda function.
Wait for the stack creation to complete. You can monitor the progress in the AWS Management Console or by running:bashCopy codeaws cloudformation wait stack-create-complete --stack-name MyLambdaStack
Step 4: Test the Lambda Function
After the CloudFormation stack is created successfully, you can test your Lambda function by invoking it manually or configuring an event source (e.g., an S3 bucket or an API Gateway).
Step 5: Clean Up (Optional)
If you no longer need the resources created by the CloudFormation stack, you can delete the stack and its resources:
aws cloudformation delete-stack --stack-name MyLambdaStack aws cloudformation wait stack-delete-complete --stack-name MyLambdaStack
This will delete the Lambda function, IAM role, and any other resources defined in the CloudFormation template.
Conclusion
In this article, you learned how to deploy a Lambda function using AWS CloudFormation. This approach allows you to define and manage your infrastructure as code, making it easier to version control and automate your AWS resources. As your projects grow, you can expand your CloudFormation templates to include more resources and dependencies.