Tutorial: Log AWS API Calls Using CloudWatch Events
NoteAmazon EventBridge is the preferred way to manage your events. CloudWatch Events and EventBridge are the same underlying service and API, but EventBridge provides more features. Changes you make in either CloudWatch or EventBridge will appear in each console. For more information, see Amazon EventBridge. |
You can use an AWS Lambda function that logs each AWS API call. For example, you can create a rule to log any operation within Amazon EC2, or you can limit this rule to log only a specific API call. In this tutorial, you log every time an Amazon EC2 instance is stopped.
Prerequisite
Before you can match these events, you must use AWS CloudTrail to set up a trail. If you do not have a trail, complete the following procedure.
To create a trail
-
Open the CloudTrail console at https://console.aws.amazon.com/cloudtrail/
. -
Choose Trails, Create trail.
-
For Trail name, type a name for the trail.
-
For Storage location, in Create a new S3 bucket type the name for the new bucket that CloudTrail should deliver logs to.
-
Choose Create.
Step 1: Create an AWS Lambda Function
Create a Lambda function to log the API call events. Specify this function when you create your rule.
To create a Lambda function
Open the AWS Lambda console at https://console.aws.amazon.com/lambda/
. -
If you are new to Lambda, you see a welcome page. Choose Get Started Now. Otherwise, choose Create a Lambda function.
-
On the Select blueprint page, type
hello
for the filter and choose the hello-world blueprint. -
On the Configure triggers page, choose Next.
-
On the Configure function page, do the following:
-
Type a name and description for the Lambda function. For example, name the function "LogEC2StopInstance".
-
Edit the sample code for the Lambda function. For example:
'use strict'; exports.handler = (event, context, callback) => { console.log('LogEC2StopInstance'); console.log('Received event:', JSON.stringify(event, null, 2)); callback(null, 'Finished'); };
-
For Role, choose Choose an existing role. For Existing role, select your basic execution role. Otherwise, create a new basic execution role.
-
Choose Next.
-
-
On the Review page, choose Create function.
Step 2: Create a Rule
Create a rule to run your Lambda function whenever you stop an Amazon EC2 instance.
To create a rule
Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/
. -
In the navigation pane, choose Events, Create rule.
-
For Event source, do the following:
-
Choose Event Pattern.
-
Choose Build event pattern to match events by service.
-
Choose EC2, AWS API Call via CloudTrail.
-
Choose Specific operation(s) and then type
StopInstances
in the box below.
-
-
For Targets, choose Add target, Lambda function.
-
For Function, select the Lambda function that you created.
-
Choose Configure details.
-
For Rule definition, type a name and description for the rule.
-
Choose Create rule.
Step 3: Test the Rule
You can test your rule by stopping an Amazon EC2 instance using the Amazon EC2 console. After waiting a few minutes for the instance to stop, check your AWS Lambda metrics in the CloudWatch console to verify that your function was invoked.
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 for Linux Instances.
-
Stop the instance. For more information, see Stop and Start Your Instance in the Amazon EC2 User Guide for Linux Instances.
-
Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/
. -
In the navigation pane, choose Events, select the name of the rule that you created, and choose Show metrics for the rule.
-
To view the output from your Lambda function, do the following:
-
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 log stream to view the data provided by the function for the instance that you stopped.
-
-
(Optional) When you are finished, you can terminate the stopped instance. For more information, see Terminate Your Instance in the Amazon EC2 User Guide for Linux Instances.