Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering AWS CloudFormation

You're reading from   Mastering AWS CloudFormation Plan, develop, and deploy your cloud infrastructure effectively using AWS CloudFormation

Arrow left icon
Product type Paperback
Published in May 2020
Publisher Packt
ISBN-13 9781789130935
Length 300 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Karen Tovmasyan Karen Tovmasyan
Author Profile Icon Karen Tovmasyan
Karen Tovmasyan
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Section 1: CloudFormation Internals
2. CloudFormation Refresher FREE CHAPTER 3. Advanced Template Development 4. Section 2: Provisioning and Deployment at Scale
5. Validation, Linting, and Deployment of the Stack 6. Continuous Integration and Deployment 7. Deploying to Multiple Regions and Accounts Using StackSets 8. Configuration Management of the EC2 Instances Using cfn-init 9. Section 3: Extending CloudFormation
10. Creating Resources outside AWS Using Custom Resources 11. Dynamically Rendering the Template Using Template Macros 12. Generating CloudFormation Templates Using AWS CDK 13. Deploying Serverless Applications Using AWS SAM 14. What's Next? 15. Assessments 16. Other Books You May Enjoy

Drift detection

CloudFormation as a service often refers to the term state. The state is basically inventory information that contains a pair of values: the logical resource name and the physical resource ID.

CloudFormation uses its state to understand which resources to create or update. If we create a stack with a resource with a logical name foo, change the property of this resource (foo) in a template, and run an update, then CloudFormation will change the corresponding physical resource in the account.

CloudFormation has a set of limitations. For example, it will not update the stack if we do not introduce changes to it. If we perform manual changes to the resource, then CloudFormation will change them only when we make changes to the template.

Developers had to rethink their way of managing the infrastructure once they started using CloudFormation, but we will get to that in the later chapters. For now, we would like to show you a feature that doesn't solve problems of manual intervention, but at least notifies us when they happen. This feature is called drift detection.

For this example, we will use the same template (Dummy IAM Role) as we did in the previous section:

$ aws cloudformation create-stack \
                     --stack-name iamrole \
                     --template-body file://IamRole.yaml \
                     --capabilities CAPABILITY_IAM

After a while, we see our stack created:

Figure 1.2 – CloudFormation console

Figure 1.2 – CloudFormation console

Note the link on the right called Drifts. If we follow that link, we will see the Drifts menu and under that Drift status: NOT_CHECKED. At the time of writing, we will have to run drift detection manually, so we need to run drift detection on our stack. After a while, we will see that everything is all right:

  1. I'm going to run Detect stack drifts and verify that my stack is compliant:
    Figure 1.3 – CloudFormation console – drifts

    Figure 1.3 – CloudFormation console – drifts

  2. Now what we will do is add an extra policy to our role and rerun drift detection:
    $ ROLENAME=$(aws cloudformation describe-stack-resources --stack-name iamrole --query "StackResources[0].PhysicalResourceId" --output text)
    $ aws iam attach-role-policy --role-name $ROLENAME --policy-arn "arn:aws:iam::aws:policy/AdministratorAccess"
  3. We can now detect drift again:
    Figure 1.4 – CloudFormation console – drift detected

    Figure 1.4 – CloudFormation console – drift detected

  4. If we check our IamRole resource and click on View drift details, we will see what exactly has been changed and differs from CloudFormation's state:
Figure 1.5 – CloudFormation console – actual modification

Figure 1.5 – CloudFormation console – actual modification

Now we have two options: either roll back the change to the resource manually or add any dummy property to the template and run update-stack.

We've learned about CloudFormation drifts, how to run its drift detection, and the actions that must be taken afterward. But don't worry—we will revisit drifts again in the following chapters.

You have been reading a chapter from
Mastering AWS CloudFormation
Published in: May 2020
Publisher: Packt
ISBN-13: 9781789130935
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image