Follow these steps in order to create a DynamoDB table, and then observe the drift after you have used the console to make a manual configuration change to the table:
- Paste the following code into a file on your filesystem. Give it a .yaml extension:
AWSTemplateFormatVersion: "2010-09-09"
Resources:
SimpleDynamoDBTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
-
AttributeName: "Id"
AttributeType: "S"
KeySchema:
-
AttributeName: "Id"
KeyType: "HASH"
BillingMode: PAY_PER_REQUEST
Outputs:
TableName:
Description: Drift Detection Example Table
Value: !Ref SimpleDynamoDBTable
- Go to the CloudFormation console, and click Create stack.
- Select Upload a template to Amazon S3, and choose the file that you just created. Click Next, and give the stack a name.
- Click Next, and then Next on the following screen.
- Click Create.
- Once the stack has completed, go to the Outputs tab, and note the name of the table.
- Go to the DynamoDb dashboard, and view the tables.
- Select the table that you just created:
A DynamoDB table
- Now, you are going to introduce drift to your stack, by making a change to the table configuration. Select the Indexes tab.
- Create a new index on an attribute called Name:
Create a DynamoDB index via the console
- Once the status on the new Global Secondary Index (GSI) is active, go back to the CloudFormation dashboard, and select the stack.
- Select Detect drift from the Actions drop-down menu:
Detect drift
- You should be able to refresh the page, and see that the stack's Drift status has changed to DRIFTED.
- Select View drifts results from the Actions menu in order to see a report that describes the detected drift:
A drifted stack
- To demonstrate why the drift is a problem, try to change the billing mode, by updating the stack with the following code, which attempts to add the same index to the table:
AWSTemplateFormatVersion: "2010-09-09"
Resources:
SimpleDynamoDBTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
-
AttributeName: "Id"
AttributeType: "S"
- AttributeName: "Name"
AttributeType: "S"
KeySchema:
-
AttributeName: "Id"
KeyType: "HASH"
BillingMode: PAY_PER_REQUEST
GlobalSecondaryIndexes:
-
IndexName: "Name-index"
KeySchema:
-
AttributeName: "Name"
KeyType: "HASH"
Projection:
ProjectionType: "ALL"
Outputs:
TableName:
Description: Drift Detection Example Table
Value: !Ref SimpleDynamoDBTable
- Choose Actions Update Stack.
- Select Replace the current template, and upload the new file.
- Click Next until you reach the final confirmation screen, and then click Update Stack.
- After a few moments, the stack will fail:
A failed stack update
- Drift detection will not fix the problem for you. It only points out that there is a problem, and it's up to you to fix it.
- Go back to the DynamoDB console, and delete the index.
- Go back to the CloudFormation dashboard, and repeat steps 16-18.
- This time, the stack update will succeed, since the table properties match what CloudFormation expects, based on the prior version of the template.