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
Creating Lambda Function
Integrating Lambda With CloudWatch (Scheduled Event)
Creating SNS Topic and Subscriptions
Integrating CloudWatch With SNSfor Notification
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.
Taking AMIs or Snapshots in a regular interval of time.
Adding 2 servers morning at 8.00am when no of servers running is lesser than 30. etc,.
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
You are responsible for your Linux Cron instance and must take necessary steps for Scaling, Patching, Administration
You must also ensure that, another machine is backing up when cron instance goes down.
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 1: In the AWS Dashboard, Click Lambda to get into the Lambda Console.
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.
Although AWS Lambda can perform actions in various regions, CloudWatch and Lambda are region-specific.
So make sure that CloudWatch Rule is in the same region, with the created 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 1: Select SNS from the AWS services dashboard for creation of SNS Topic. Step 2: Select Create Topic from SNS Home tab
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
Email – Normal Mail format
Email-JSON – You will get the data in Key-Value pairs
Amazon SQS – You can forward the output to Amazon Queue for further operations
HTTP, HTTPS – You can use these protocols for web page operations such as POST etc.
Application – You can forward the data to an application.
AWS Lambda – You can also invoke Lambda Function.
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.
CloudWatch is region-specific. So make sure that CloudWatch Rule is in the same region, with the created SNS Topics.
If Lambda performs events in various regions, CloudWatch and SNS have to be created for every region to get all notifications
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
The Matched event from the Event Selector.
Part of the matched event that you can specify.
Constant (JSON text) , wherein which we will always get the text we have provided.
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.