Skip to main content

Event Source Mapping and SNS Notifications for Your AWS Lambda Functions via the AWS CLI

Hands-On Lab

 

Photo of

Training Architect

Length

00:30:00

Difficulty

Intermediate

Welcome to this Learning Activity for creating Lambda triggers via the AWS CLI toolkit! This Learning Activity provides hands-on experience with creating and customizing Lambda function triggers from the CLI. We are going to receive an EC2 Instance Change notification, and then take an EBS snapshot of the attached root volume. The primary focus will be on the following features within AWS: 1. Lambda Console 2. Lambda AWS CLI 3. Event Source Mappings 4. CloudWatch Logs 5. EC2/EBS AWS Lambda allows you to create functions and only have to worry about managing your code! It's a developers dream. Going through this live activity will allow you to gain hands-on experience managing and configuring Lambda functions from the CLI. Ditch the click!

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Event Source Mapping and SNS Notifications for Your AWS Lambda Functions via the AWS CLI

Introduction

This hands-on lab provides experience with creating and customizing Lambda function triggers from the CLI. We are going to receive an EC2 instance change notification, and then take an EBS snapshot of the attached root volume. The primary focus will be on the Lambda console, Lambda AWS CLI, event source mappings, CloudWatch Logs, and EC2/EBS. AWS Lambda allows you to create functions and only have to worry about managing your code. This lab will give you hands-on experience managing and configuring Lambda functions from the CLI.

Solution

Log in to the live AWS environment using the credentials provided. Make sure you're in the N. Virginia (us-east-1) region throughout the lab.

You can download all the code used in the lesson on GitHub.

Verify Lab Resources and Prepare Environment

  1. In the AWS console, open the following services in new browser tabs (so they are each open at the same time):

    • EC2
    • SNS
    • IAM
    • Lambda
    • CloudWatch
  2. In the EC2 browser tab, verify there are two running instances.

    • The SSH-Host instance is the one with the AWS CLI installed.
    • The SHUT ME OFF instance is the one we will use to test initially.
  3. Open a terminal session and log in to the SSH-Host instance via SSH (either copying its public IP from the AWS console or the lab page):

    ssh cloud_user@<PUBLIC IP>
  4. Check that AWS CLI is installed:

    aws help

    We should then see a man page, which means the AWS CLI is installed. Exit by typing q.

  5. In the AWS console, head to the IAM browser tab.

  6. Click Roles in the left-hand menu.

  7. Click the listed lambda_exec_role_LA to open it.

  8. Copy its Role ARN and paste it into a text file, as we'll need it later.

  9. Head to the SNS browser tab.

  10. Click Topics in the left-hand menu.

  11. Click the ARN of the listed EbsSnapshot topic to open it.

  12. Copy its Topic ARN and paste it into a text file, as we'll need it in a minute.

Create a Lambda Function from the AWS CLI

  1. In the terminal, create the file:

    vim lambda_function.py
  2. Copy the lambda_function.py code on GitHub and paste it into the terminal (making sure to replace <SNS TOPIC ARN> with the SNS topic ARN you copied a minute ago).

  3. Save and exit the file.

  4. Zip the file:

     zip lambda_function.zip lambda_function.py
  5. Verify both files exist:

    ll
  6. Create the function (making sure to replace <IAM ROLE ARN> with the IAM role ARN you copied a minute ago):

    aws lambda create-function 
    --region us-east-1 
    --function-name "EbsSnapshot" 
    --description "Creates a snapshot when instances are going to stop or terminate." 
    --zip-file fileb:///PATH/TO/lambda_function.zip 
    --role <IAM ROLE ARN> 
    --handler lambda_function.lambda_handler 
    --runtime "python3.6" 
    --timeout 10 
    --memory-size 1024
  7. Back in the AWS console, head to Lambda > Functions and verify our EbsSnapshot function exists.

  8. Click the listed EbsSnapshot function to view its details.

Create Your CloudWatch Rule

  1. In the terminal, create the CloudWatch rule:

    aws events put-rule 
    --name "EC2-Stopping" 
    --event-pattern "{"source":["aws.ec2"],"detail-type":["EC2 Instance State-change Notification"],"detail":{"state":["shutting-down","stopping"]}}" 
    --state "ENABLED" 
    --description "Takes EBS Snapshot of volume of instance shutting down." 
    --region us-east-1

Create an Event Target for Your Event Rule

  1. In the browser, head to the CloudWatch tab.

  2. Click Rules in the left-hand menu.

  3. Click our listed EC2-Stopping rule, which we'll then see doesn't have a target.

  4. In the terminal, add an event target (making sure to replace <LAMBDA FUNCTION ARN> with the FunctionArn included in the output when we created our function):

    aws events put-targets 
    --rule "EC2-Stopping" 
    --targets "Id"="1","Arn"="<LAMBDA FUNCTION ARN>" 
    --region us-east-1
  5. Back in the CloudWatch console, refresh to verify our Lambda function is now listed as the target.

  6. In the terminal, add permissions for our rule to invoke Lambda (making sure to replace <CLOUDWATCH RULE ARN> with the RuleArn included in the output when we created our CloudWatch rule):

    aws lambda add-permission 
    --statement-id 'abcd-1234-5678' 
    --action 'lambda:InvokeFunction' 
    --principal events.amazonaws.com 
    --source-arn <CLOUDWATCH RULE ARN> 
    --function-name EbsSnapshot 
    --region us-east-1
  7. Back in the Lambda console, refresh the page. We should see we now have our CloudWatch Events trigger.

Configure Your SNS Subscription

  1. In the SNS browser tab, click Create subscription.
  2. In the dialog, set the following values:
    • Protocol: Email
    • Endpoint: Enter your email address
  3. Check your email.
  4. In the subscription confirmation email, click Confirm subscription.

Invoke Your Function and Check For EBS Snapshots

Stop SHUT ME OFF Instance

  1. In the EC2 console, select the SHUT ME OFF instance and click Actions > Instance State > Stop.
  2. In the dialog, click Yes, Stop.
  3. Click Snapshots in the left-hand menu. You may need to refresh a few times until you see our listed snapshot.
  4. Check your email to make sure you also received an SNS notification.

Terminate SSH-Host Instance

  1. In the terminal, enter logout.
  2. In the AWS console, head to EC2 > Instances.
  3. Select the SSH-Host instance and click Actions > Instance State > Terminate.
  4. In the dialog, click Yes, Terminate.
  5. Click Snapshots in the left-hand menu. You may need to refresh a few times until you see our listed snapshot.
  6. Check your email to make sure you also received an SNS notification.

Conclusion

Congratulations on successfully completing this hands-on lab!