API, Automation, AWS, Cloud Computing, Comparision

7 Mins Read

Schedule and Configure Lambda to Notify you..

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.

Cost Comparision

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 m1

 

Cost of running the same for a year =$ 14.64 * 12 = $ 175.56

g

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.

https://aws.amazon.com/lambda/pricing/

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. 1

Step 2:  Click the Get Started Now button to create a Lambda Function.

2

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.

 

3 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.

4

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.

5

[/showhide]

Important Note:

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.

6                        

  

[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.

7

Step 2:  Click Create Rule in the Events Tab to create an event.

9

Step 3:  Click Schedule in the Select event source tab. The Event Selector is the source for invoking the target.

10

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

https://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/ScheduledEvents.html

 

11

Step 5:  From the Targets Section, Click Select target type and select Lambda function. Click Configure details to proceed.

12

Step 6: Enter the Name and Description and check the status if it is enabled. Click Create rule to complete the rule creation.   16

[/showhide]

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.

22               

[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. Capture Step 2:  Select Create Topic from SNS Home tab

17

Step 3:  Enter the Topic name and Display name and click Create Topic button.

18

Step 4:  Click Create Subscription in the Subscriptions tab

19

Step 5:  Select the type of Protocol by which you want to notify. The protocols are

23

Step 6: Create the Subscription under a specific Topic or manually enter Topic ARN. Select Email from the Protocol drop-down menu.

20

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.

21

[/showhide]

Important Note:

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.

9

Step 2:   Select  the Service name from the various available services.

Capture1

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.

Capture2     Capture3

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

Capture4

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.

Capture6

[/showhide]

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.

mThus, 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.  

 

WRITTEN BY CloudThat

SHARE

Comments

  1. Andy

    Jun 19, 2018

    Reply

    Great read, thanks for taking the time.

    • Himanshu Sachdeva

      Jun 22, 2018

      Reply

      Thanks Andy. We are glad you liked it.

    Click to Comment

Get The Most Out Of Us

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!