CloudFormation
Introduction
- Declarative language for deploying resources in AWS
- YAML or JSON
- CloudFormation templates can be visualized using Application Composer
Example
---
Resources:
MyInstance:
Type: AWS::EC2::Instance
Properties:
AvailabilityZone: us-east-1a
ImageId: ami-0a3c3a20c09d6f377
InstanceType: t2.micro
CloudFormation Template Sections
-
Resources
- The only required section in a template
- The resources section represent the AWS components that the CF template will deploy
- Resource type identifiers are in this format:
- service-provider::service-name::data-type-name
-
Parameters
- Provide input to your CF templates
-
Mappings
-
Fixed variables in your CF template used to differentiate between different environment like dev vs prod, regions, AMI types, etc.
-
To access values in a map, use
Fn::FindInMap:{ ... "Mappings" : { "RegionMap" : { "us-east-1" : { "HVM64" : "ami-0ff8a91507f77f867", "HVMG2" : "ami-0a584ac55a7631c0c" }, "us-west-1" : { "HVM64" : "ami-0bdb828fd58c52235", "HVMG2" : "ami-066ee5fd4a9ef77f1" }, "eu-west-1" : { "HVM64" : "ami-047bb4163c506cd98", "HVMG2" : "ami-0a7c483d527806435" }, "ap-southeast-1" : { "HVM64" : "ami-08569b978cc4dfa10", "HVMG2" : "ami-0be9df32ae9f92309" }, "ap-northeast-1" : { "HVM64" : "ami-06cd52961ce9f0d85", "HVMG2" : "ami-053cdd503598e4a9d" } } }, "Resources" : { "myEC2Instance" : { "Type" : "AWS::EC2::Instance", "Properties" : { "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "HVM64" ] }, "InstanceType" : "m1.small" } } } }
-
-
Outputs
- Optional
- Output values can be referenced in other stacks
- Use
FN:ImportValue
-
Conditions
- Control the creation of resources or outputs based on a condition
Intrinsic Functions
- Fn::Ref - Get a references to a value of a paremeter, physical Id of a resource, etc.
- Fn::GetAtt - Get attributes from a resource
- Fn::FindInMap - Retrieve a value from a map
- Fn::ImportValue - Import an output value from another template
- Fn::Base64 - Convert a value to Base64 inside a template
- Condition Functions (Fn::If, Fn::Not, Fn::Equals, etc.)
- etc….
Service Roles
- IAM roles that allow CloudFormation to create/update/delete stack resources