This application was designed to give customers greater insight into their AWS API usage by generating custom CloudWatch Metrics based on CloudTrail logs.
VERSION: 0.1.0 AUTHORS: Joe Hsieh, Ho Ming Li, Jeremy Wallace
Here is the data flow:
- Amazon CloudTrail is configured to send API logs to Amazon CloudWatch Logs.
- AWS Lambda is triggered by new records that are written to the CloudWatch Log Stream.
- AWS Lambda aggregates the number of API requests and publishes custom Amazon CloudWatch Metrics.
Below are two different ways of configuring your AWS environment to collect metrics on API usage using this lambda function. You could configure the AWS environment with the command line, or through the web console.
- Follow the guide here to send CloudTrail logs to CloudWatch Logs.
- Create a role for the Lambda function:
aws iam create-role --role-name apitrackerrole
nano lambdapolicy.json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"cloudwatch:PutMetricData"
],
"Resource": [
"*"
]
}
]
}
aws iam create-policy --policy-name putMetricsPolicy --policy-document file://lambdapolicy.json
aws iam attach-role-policy --role-name apitrackerrole --policy-arn <POLICY_ARN>
- Clone this repository and zip up the nodejs directory.
cd nodejs
npm install
cd ../
zip -r apitracker.zip nodejs
- At a command prompt, run the following command, where role-arn is the Lambda execution role set up in the first step, found in the IAM console under Roles:
aws lambda create-function \
--function-name apitracker \
--zip-file file://apitracker.zip \
--role apitrackerrole \
--handler app.handler \
--runtime nodejs
- Grant CloudWatch Logs the permission to execute your function. At a command prompt, run the following command and substitute account 123456789123 with your own and change the log-group to be the log group you want to process:
aws lambda add-permission \
--function-name "apitracker" \
--statement-id "apitracker" \
--principal "logs.us-east-1.amazonaws.com" \
--action "lambda:InvokeFunction" \
--source-arn "arn:aws:logs:us-east-1:123456789123:log-group:CloudTrail/logs:*" \
--source-account "123456789123"
- Create a subscription filter. At a command prompt, run the following command and substitute account 123456789123 with your own and change the log-group-name to be the log group you want to process:
aws logs put-subscription-filter \
--log-group-name CloudTrail/logs \
--filter-name apitracker \
--filter-pattern "" \
--destination-arn arn:aws:lambda:us-east-1:123456789123:function:apitracker
Please follow the instructions below to configure API tracker.
- Create an AWS account and sign in to the AWS Management Console.
- Under Management Tools, click on CloudTrail.
- Click Trails on the left menu bar.
- Click the blue Add New Trail button.
- Name the trail and pick an S3 bucket location to store the logs. Press create.
- Once created, click on your newly created CloudTrail.
- In the section CloudWatch Logs, click configure.
- If you do not have a CloudWatch Log Stream created, name one here and press continue.
- Review the IAM policy and press Allow.
- Click back to the CloudTrail page and ensure that CloudWatch Logs has been configured.
- Locally on your machine, run npm install in the nodejs directory (cloudwatch-api-tracker).
- Create a new zip file based on the contents of this directory (cloudwatch-api-tracker).
- Open the Lambda Console page.
- Create a new lambda function.
- Select the Blank Function blueprint.
- Add a CloudWatch Logs Trigger.
- Name the filter and use the CloudTrail Log Group that you created above.
- Press Next. Name the Lambda function.
- Upload the ZIP file that you created above.
- Set the handler to app.handler.
- Create a new IAM role for the Lambda function with the following IAM policy.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"cloudwatch:PutMetricData"
],
"Resource": [
"*"
]
}
]
}
Congratulations! You have set up API tracker. You will now start to see metrics in CloudWatch.
- Create a CloudWatch Alarm on a particular API.
- Create a CloudWatch Dashboard with the most commonly used APIs.