Customized S3 Trigger to Invoke Lambda Function through CloudFormation
Tags: cloudformation, s3, lambdafunction, aws
Published: 2022-11-27 12:09:55

“ I have checked the documents of AWS to create a s3 trigger to invoke lambda function . So I found that it can be possible using cloudformation stack to automate purpose. Pricing of S3 bucket as per storage size and  lambda function costs cheaper.”

Lambda is a compute service that lets you run code without provisioning or managing servers. Lambda runs your code on a high-availability compute infrastructure and performs all of the administration of the compute resources, including server and operating system maintenance, capacity provisioning and automatic scaling, code monitoring and logging. With Lambda, you can run code for virtually any type of application or backend service.

S3 events occur when someone modifies the content of an s3 bucket. Altering the content can be achieved by either creating, removing or updating a file. S3 events are asynchronous, which means your lambda function only has to handle the event and does not have to respond with anything. There is no client involved like with API Gateway, so nobody is waiting for your function to send a response. S3 will send an event that triggers your lambda function but it won’t check what it did afterward.

In this post, you will get to know how to customize s3 trigger to invoke lambda function through cloudformation. Here I have created a s3 bucket, iam role and s3 trigger with lambda function through a cloudformation stack with customized parameters.

#Prerequisites
You’ll need an AWS Identity and Access Management for this post. [Getting started with AWS Identity and Access Management](https://aws.amazon.com/iam/getting-started/) provides instructions on how to create an IAM role with required permission policy.

You’ll need an Amazon Simple Storage Service for this post. [Getting started with Amazon Simple Storage Service](https://aws.amazon.com/s3/getting-started/) provides instructions on how to create a bucket in simple storage service.

#Architecture Overview
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mc1ikg2goijepikhfhvg.png)
The architecture diagram shows the overall deployment architecture with data flow, amazon s3, lambda function, iam role and cloudformation.

#Solution overview
The blog post consist of the following phases:
1. Create of Lambda Function and S3 Trigger On It through Cloudformation
2. Output of Stack as Lambda Function and S3 Trigger

I have a s3 bucket and IAM role as below →
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wa5bcv5ecp4604oochwp.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tduy1t66wm8e7jmmo86o.png)

##Phase 1: Create of Lambda Function and S3 Trigger On It through Cloudformation
1. Open the cloudformation console and create a stack for the lambda function and s3 trigger on it. In the template file various parameters are defined: lambda function code and configuration, Iam role configuration and s3 trigger with lambda invoke permission configuration.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dd029gew9otmgoes1el0.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z0kgelmiz707aa20anwe.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/57qn0drsov50c3ucr38j.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/543qo3nhtd0a958cke9d.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zlxac5b9hu4w8sitipvt.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r1v29b5qlpdeclf701vf.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uia2ivypje4ubrvfm2hz.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0aj4j4qkwrsgnsw1f38z.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dq6udg76ba2uszqfwowm.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pala4fiyavvayoqtwmkg.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h1y7wcg592pziyfdkyrk.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h2qmqvcz7y3zzg8zj52j.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4zcldv1m113ydnk6io2u.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q74eix3cs4541jdg0hvw.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hs1knotp4hnjsr7hrwmx.png)

##Phase 2: Output of Stack as Lambda Function and S3 Trigger 
1. Open the lambda console and check the created lambda functions. Also check the s3 trigger on lambda.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5xhyngbt4f048qa99kz8.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bczu8es4j0ma2iho9398.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vyu2w59yq1clr7h52tw4.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41cyu46m7jvnun9jak4s.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u21x5bfjenbzcs36fehz.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4pyvbxxzbtwp15g4rbu9.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51at0hpei4cmwdtc5bi6.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8p79cijwom8n2wzp3sso.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1i9g4ixyyhkzxcc893o2.png)

#Clean-up
Delete S3 Bucket, IAM Role and Cloudformation Stack.

#Pricing
I review the pricing and estimated cost of this example. 

Cost of Data Transfer = $0.0

Cost of Lambda Function = $0.0

Cost of S3 = $0.575

Total Cost = $0.575

#Summary
In this post, I showed “how to customize s3 trigger to invoke lambda function through cloudformation”.

For more details on AWS Lambda, Checkout Get started AWS Lambda, open the [AWS Lambda console](https://ap-south-1.console.aws.amazon.com/lambda/home?region=ap-south-1#/discover). To learn more, read the [AWS Lambda documentation](https://docs.aws.amazon.com/lambda/?id=docs_gateway).

Thanks for reading!

Connect with me: [Linkedin](https://www.linkedin.com/in/gargee-bhatnagar-6b7223114)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38sdi4nsp89fnui8h7dk.png)