Member-only story
Record Instance Launch History
In this post, we’ll see how we can record instances’ launch history using CloudWatch, Lambda and DynamoDB.
Step1:Create a dynamodb table named instance_history_details with event_id as partition key. Create a GSI with instance_id as partition key and date as sort key and another GSI with instance_state as partition key and date as sort key
Step2: Create a CloudWatch event rule with following event pattern
Step3: Create an IAM role for Lambda with following policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": "dynamodb:PutItem",
"Resource": "arn:aws:dynamodb:us-east-1:xxxx:table/instance_history_details"
}
]
}
Step4: Create a Lambda with Python 3.7 runtime using following code.
https://raw.githubusercontent.com/vinycoolguy2015/awslambda/master/instance_history.py
Specify this lambda function as target of our CloudWatch rule.
Now we are all set. Launch 3 instance, then stop them and finally terminate them.You should see events loggedin our DynamoDB table.