CloudFormation
- Infrastructure as code. Free.
- You can schedule the destruction and re-creation of the environment
- The templates have to be uploaded in S3
- You can’t edit the current version, we need to re-upload a new version.
- When you upload a new template, you can see the changes in Change Sets.
- Cloudformation knows the right order for resource creation.
- Rollbacks
- If the update fails, the service is automatically rolled back to the previous working state.
- You need to enable it in stack creation options
Cloudformation Drift Detection
- Detects changes that happened manually on the resources!
- Stack actions -> Detect drift
Stacks
- You create Stacks. They contain Events (creation events), Resources, Templates etc
- Nested stacks are stacks contained in other stacks.
- To create a nested stack, specify the AWS::CloudFormation::Stack
- StackSets help you update infrastructure in different regions and Accounts.
Components
- Resources – Mandatory
- Parameters
- Mappings
- Outputs
- Conditionals
- Metadata
Parameters
- Define them in the Parameters section
- Use them with !Ref. This can be used to reference parameters or resources.
- Pseudo Parameters: Some default parameters like AWS::AccountId etc
Mappings
- Hard-Coded variables
- Matches a key to a corresponding set of named values
- !FindInMap [MapName, TopLevelKey, SecondLevelKey]
Outputs
- You can export output and import it into another stack
- You cannot delete a stack if it has referenced output to other stacks
- Export: to export it and !ImportValue to import an output
Conditions
- !Equals [!Ref ParameterName, value]
- Used by the Condition: ConditionName
Intrinsic Functions
- !Ref – Reference parameters of resources. Returns the resources ids
- !GetAtt. Get parameter attributes. Unlike Ref, this can return any attribute of the resource, not only the id. Example: !GetAtt EC2Instance.AvailabilityZone
- !FindInMap[ MapName, TopLevelKey, SecondLevelKey ] – Return Mapping value
- !ImportValue – Import value that has been exported
- !Join – Joins a comma-separated list with “:”
- !Sub – Substitute values in strings
- Conditions – And , Equals, If, Not , Or