DEV Community

GargeeBhatnagar for AWS Community Builders

Posted on

Customized S3 Trigger to Invoke Lambda Function through CloudFormation

“ 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 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 provides instructions on how to create a bucket in simple storage service.

Architecture Overview

Image description
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
Image description

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 Image description Image description Image description Image description Image description Image description Image description Image description Image description Image description Image description Image description Image description Image description

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 Image description Image description Image description Image description Image description Image description Image description Image description

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. To learn more, read the AWS Lambda documentation.

Thanks for reading!

Connect with me: Linkedin
Image description

Oldest comments (0)