- Service Delivery Program
- About Us
In the era of Dev-ops, continuous integration and deployment, AWS Lambda provides you with various features, that will help you automate your needs. Lambda is a compute service from Amazon Web Services, which performs actions on various available services of AWS, based on the program code that you upload for execution. It can be triggered by events or with a scheduled event provided by CloudWatch.
In this blog, we are going to see step-by-step process of
Why use Lambda?
Lambda will be an effective solution, when there are events that are need to be performed in a periodic manner. It will also help in infrastructure automation.
Example Use Cases.
For a scheduled running of a program that triggers the API’s, a dedicated Linux Cron machine is commonly used.
Though the program runs few times a day, the instance must be running throughout the year.
Cost of a (t1.micro) EC2 machine running for a month = $ 14.64
Cost of running the same for a year =$ 14.64 * 12 = $ 175.56
Points to Ponder
Most often, when your Lambda function runs for few minutes, few times a day, it will come under the free tier*.
For Lambda Pricing and free tier information, click the following link.
Creating Lambda Function[showhide type=”qs” more_text=”For Steps to Create A Lambda function, Click Here” less_text=”Hide Details” hidden=”yes”]
Step 2: Click the Get Started Now button to create a Lambda Function.
Step 3: There are various inbuilt Lambda Functions that can be utilized. Also, specify the type of Language you want to choose from. If you want to use your own Lambda Function, click the Skip button in the bottom right corner.
Step 4: Enter the Name you want to provide to Lambda Function along with Description. You will have to choose the method by which you want to use your program code. In our case, we are using the Edit code inline option. You will have to use Upload a .Zip file or Upload a .Zip from Amazon S3 if you have dependencies in your program.
Step 5: You must choose a Lambda Role for authorization, that will perform the actions you want via the code you provide. You must provide necessary permissions by creating a new role in IAM, for Lambda to access SNS, CloudWatch and EC2. Choose VPC if you require. Click Next complete creation of Lambda Function.
Integrating Lambda With CloudWatch
Although, Lambda is basically Event-driven, Lambada can also be Scheduled to take place in a particular time or particular interval of time.
[showhide type=”qp” more_text=”For Steps to Create a Scheduled Event, Click Here” less_text=”Hide Details” hidden=”yes”]
Step 1: To Configure, click the CloudWatch Service from the AWS dashboard.
Step 2: Click Create Rule in the Events Tab to create an event.
Step 3: Click Schedule in the Select event source tab. The Event Selector is the source for invoking the target.
Step 4: Click in the Fixed Rate of tab and also select Interval Period. It may be the frequency in Minutes, Hours or Days. You can also specify a Simple Cron Expression. Click Add target button from the Targets tab.
For more information on how to schedule an event based on specific time, please click the following link
Step 5: From the Targets Section, Click Select target type and select Lambda function. Click Configure details to proceed.
Step 6: Enter the Name and Description and check the status if it is enabled. Click Create rule to complete the rule creation.
Integrating CloudWatch With SNS
CloudWatch can trigger SNS, if an event is completed or when there is a change in the state of EC2 or when an action is performed in Auto Scaling Group or even when an API call.
For example, we are configuring SNS to send Email and Email-Json notification when a Lambda function gets executed or when an event is completed successfully.
[showhide type=”qq” more_text=”For Steps to Create an SNS Topic with Subscriptions, Click Here” less_text=”Hide Details” hidden=”yes”]
Step 3: Enter the Topic name and Display name and click Create Topic button.
Step 4: Click Create Subscription in the Subscriptions tab
Step 5: Select the type of Protocol by which you want to notify. The protocols are
Step 6: Create the Subscription under a specific Topic or manually enter Topic ARN. Select Email from the Protocol drop-down menu.
Step 7: You will receive a PendingConfirmation message. You can confirm either by activating subscription by clicking the link received via e-mail or manually clicking Confirm Subscription or providing Subscription confirmation URL. Click Confirm Subscription.
Configuring CloudWatch to Trigger SNS
[showhide type=”qr” more_text=”For Steps to Configure CloudWatch with SNS, Click Here” less_text=”Hide Details” hidden=”yes”]
Step 1: Click Create Rule in the Events Tab to create an event.
Step 2: Select the Service name from the various available services.
Step 3: Click Any Operation if you want to get notifications for all operations. Click Specific operation(s) option, if you want to notify for a specific action. For example, consider a scenario where when you create an AMI for an EC2 instance, and you want to get notified.
Step 4: From the various events, select CreateImage event and also select SNS Topic in the Targets section. From the drop-down menu, select the SNS topic you have created. Click Configure details to create the rule.
You can Configure input of the Targets for SNS Topic, to get
Step 5: After configuring, we can notice all the rules we have configured. In this case, rules have been created for Creation and De-register of AMI (Image) and Deletion of a snapshot.
You will receive mail with many parameters, that will help you identify how the lambda function got invoked and other key attributes, to the configured mail account. It will help you to analyze your logs and keep track of events.
Thus, whenever Lambda gets triggered in a scheduled manner through CloudWatch, we will receive notification.
Please comment and share if you like the article. Feel free to ask your questions and provide your valuable suggestions below.
Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!