Your AWS CloudFormation stacks should not be drifted from their expected template configuration. A CloudFormation stack is considered to have drifted from its configuration if one or more of its resources have been drifted.
AWS CloudFormation Drift Detection is a feature that helps you identify resources that have drifted away from their expected configurations. Once you have identified the resources that have drifted, you can use the AWS CLI to remediate the drift.Here are the steps to remediate AWS CloudFormation Drift Detection using AWS CLI:
Identify the stack that has drifted by running the following command:
This will show you the current configuration of the resources in the stack. If the resources have been remediated, the expected and actual configurations should match.
Using Python
To remediate AWS CloudFormation drift detection using Python, follow these steps:
for stack in stacks: stack_drift = client.detect_stack_drift(StackName=stack['StackName']) if stack_drift['StackDriftStatus'] == 'DRIFTED': print('Stack {} has drifted'.format(stack['StackName']))
If a stack has drifted, remediate it by updating the stack:
Copy
Ask AI
response = client.update_stack(StackName=stack['StackName'], UsePreviousTemplate=True)print('Stack {} has been remediated'.format(stack['StackName']))
Note: Make sure to test the script thoroughly before running it in a production environment.