[Tuning] First Time AWS Cloudformation Stack Creation by User #5036
+39
−25
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull Request
Issue link(s):
Summary - What I changed
AWS CloudFormation is an infrastructure-as-code service that lets you define resources (EC2, IAM, RDS, S3, etc.) in a template and then deploy them as stacks. For the proper execution of this rule, the key distinction is between APIs that create resources vs those that just define or stage configuration.
CreateStack → Provisions a single stack in the current account/Region. The template is executed immediately, and resources are created.
CreateStackSet → Defines a blueprint for multi-account or multi-Region deployments. By itself, it does not create any resources; it’s just a container until instances are launched.
CreateStackInstances → Uses an existing StackSet to provision stacks in target accounts/Regions. This is the point at which resources are actually created in those environments.
This rule logic had an error, as it was only meant to capture the actual creation of resources (stacks) for the first time by a user or a role, but instead it also captured the configuration building API
CreateStackSet
. Because of this I've replacedCreateStackSet
API withCreateStackInstances
API, and keptCreateStack
API.Another important note is that
CreateStackInstances
API when called triggers a series of events includingAssumeRole
of a trusted and established CloudFormation role and then that role executesCreateStack
across the different regions or accounts. This is a new_terms rule so it would ideally only trigger for that first time occurance ofCreateStackInstances
by the user, but not theCreateStack
by the role (assuming it's an established role in the environment which is dedicated for Cloudformation usage). You can see this behavior captured in the screenshot below.For this tuning:
CreateStackSet
API call as this only creates a blueprint for creating stack instances but does not actually create the resourcesCreateStackInstances
API call which is used to create resources defined in the StackSetHow To Test
You can use this script for testing. It runs through a lot more CloudFormation API calls than just the ones that trigger this rule as I plan to use this same script for additional API activity I'll be adding coverage for. To manually test you can follow AWS documentation for
CreateStack
which is the simplest, orCreateStackSet
+CreateStackInstances
which is a bit more complex. My script covers both these scenarios.