
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.


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.



Task 4: Verify that the Lambda function worked




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.




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





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.

Step 4: Monitor cloudwatch metrics
In the navigation pane, choose Events, Rules, select the name of the rule that you created, and choose Show metrics for the rule.

