Lab : AWS Lambda



 In this hands-on activity, you will create an AWS Lambda function. You will also create an Amazon CloudWatch event to trigger the function every minute. The function uses an AWS Identity and Access Management (IAM) role. This IAM role allows the function to stop an Amazon Elastic Compute Cloud (Amazon EC2) instance that is running in the Amazon Web Services (AWS) account.

Task 1: Create a Lambda function

Task 2: Configure the trigger

In this task, you will configure a scheduled event to trigger the Lambda function by setting a CloudWatch event as the event source (or trigger). The Lambda function can be configured to operate much like a cron job on a Linux server, or a scheduled task on a Microsoft Windows server. However, you do not need to have a server running to host it.

Note: A more realistic, schedule-based stopinator Lambda function would probably be triggered by using a cron expression instead of a rate expression. However, for the purposes of this activity, using a rate expression ensures that the Lambda function will be triggered soon enough that you can see the results.

Task 3: Configure the Lambda function

In this task, you will paste a few lines of code to update two values in the function code. You do not need to write code to complete this task.


one of the charts shows you how many times your function has been invoked. There is also a chart that shows the error count and the success rate as a percentage.

Task 4: Verify that the Lambda function worked

Stopped after 1 min

Discuss one of the following points in your blog

  • How could you modify the Lambda function that you created in the activity, to make it more real-world? 
  • Extend the activity by creating your own custom Lamda function

——————————————————————————————————————————–

I am going to log the State of an Amazon EC2 Instance Using CloudWatch Events. logging the state of the EC2 instance is important in order to monitor it’s health perspective in real cloud environment

Step 1: Create an AWS Lambda Function

Create a Lambda function to log the state change events. You specify this function when you create your rule.

Using built in template for this
create basic rule to execute lambda function

Use this script to monitor instance change from the console logs of the running instance :

‘use strict’; exports.handler = (event, context, callback) => { console.log(‘LogEC2InstanceStateChange’); console.log(‘Received event:’, JSON.stringify(event, null, 2)); callback(null, ‘Finished’); };

Step 2: Create a Rule

Create a rule to run your Lambda function whenever you launch an Amazon EC2 instance.

This is the final out of created event pattern . this event pattern will trigger running state of the instance

Step 3: Create a EC2 instance and start

To test your rule, launch an Amazon EC2 instance. After waiting a few minutes for the instance to launch and initialize, you can verify that your Lambda function was invoked.

wait for instance is fully started and running

Step 4: Monitor cloudwatch metrics

In the navigation pane, choose EventsRules, select the name of the rule that you created, and choose Show metrics for the rule.

you can see the lambda function was executed. instance state has been recorded in cloudwatch

Leave a comment