Tutorial: Log the state of an Amazon EC2 instance using EventBridge
You can create an AWS Lambda function that logs a state change for an Amazon EC2 instance. Then you can create a rule that runs your Lambda function whenever there is a state transition or a transition to one or more states that are of interest. In this tutorial, you log the launch of any new instance.
Steps:
Step 1: Create an AWS Lambda function
Create a Lambda function to log the state change events. When you create your rule in Step 2, you specify this function.
To create a Lambda function
Open the AWS Lambda console at https://console.aws.amazon.com/lambda/
. -
Choose Create function.
-
Choose Author from scratch.
-
Enter a name and description for the Lambda function. For example, name the function
LogEC2InstanceStateChange
. -
Leave the rest of the options as the defaults and choose Create function.
-
On the Code tab of the function page, double-click index.js.
-
Replace the existing code with the following code.
'use strict'; exports.handler = (event, context, callback) => { console.log('LogEC2InstanceStateChange'); console.log('Received event:', JSON.stringify(event, null, 2)); callback(null, 'Finished'); };
-
Choose Deploy.
Step 2: Create a rule
Create a rule to run the Lambda function you created in Step 1. The rule runs when you launch an Amazon EC2 instance.
To create the EventBridge rule
Open the Amazon EventBridge console at https://console.aws.amazon.com/events/
. -
In the navigation pane, choose Rules.
-
Choose Create rule.
-
Enter a name and description for the rule. For example, name the rule
TestRule
-
For Event bus, choose the event bus that you want to associate with this rule. If you want this rule to match events that come from your account, select default. When an AWS service in your account emits an event, it always goes to your account’s default event bus.
-
For Rule type, choose Rule with an event pattern.
-
Choose Next.
-
For Event source, choose AWS services.
-
For Event pattern, do the following:
-
For Event source, select EC2 from the drop-down list.
-
For Event type, choose EC2 Instance State-change Notification from the drop-down list.
-
Choose Specific states(s) and choose running from the drop-down list.
-
Choose Any instance
-
-
Choose Next.
-
For Target types, choose AWS service.
-
For Select a target, choose Lambda function from the drop-down list.
-
For Function, select the Lambda function that you created in the Step 1: Create a Lambda function section. In this example, select
LogEC2InstanceStateChange
. -
Choose Next.
-
Choose Next.
-
Review the details of the rule and choose Create rule.
Step 3: Test the rule
You can test your rule by stopping an Amazon EC2 instance using the Amazon EC2 console. Wait a few minutes for the instance to stop, and then check your AWS Lambda metrics on the CloudWatch console to verify that your function ran.
To test your rule by stopping an instance
Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/
. -
Launch an instance. For more information, see Launch Your Instance in the Amazon EC2 User Guide.
-
Stop the instance. For more information, see Stop and Start Your Instance in the Amazon EC2 User Guide.
-
To view the output from your Lambda function, do the following:
Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/
. -
In the navigation pane, choose Logs.
-
Select the name of the log group for your Lambda function (
/aws/lambda/
).function-name
-
Select the name of the log stream to view the data provided by the function for the instance that you stopped.
-
(Optional) When you're finished, terminate the stopped instance. For more information, see Terminate Your Instance in the Amazon EC2 User Guide.
Step 4: Confirm success
If you see the Lambda event in the CloudWatch logs, you've successfully completed this tutorial. If the event isn't in your CloudWatch logs, start troubleshooting by verifying the rule was created successfully and, if the rule looks correct, verify the code of your Lambda function is correct.
Step 5: Clean up your resources
You can now delete the resources that you created for this tutorial, unless you want to retain them. By deleting AWS resources that you are no longer using, you prevent unnecessary charges to your AWS account.
To delete the EventBridge rule(s)
-
Open the Rules page
of the EventBridge console. -
Select the rule(s) that you created.
-
Choose Delete.
-
Choose Delete.
To delete the Lambda function(s)
-
Open the Functions page
of the Lambda console. -
Select the function(s) that you created.
-
Choose Actions, Delete.
-
Choose Delete.