CloudFormation Template Structure
Let’s take a sample CloudFormation template structure in YAML format and get to know each section:
AWSTemplateFormatVersion: "2010-09-09"
Description: Sample CloudFormation template
Parameters:
InstanceTypeParameter:
Type: String
Default: t2.micro
AllowedValues:
- t2.micro
- m1.small
- m1.medium
Description: Enter instance type (default is t2.micro)
Mappings:
RegionMap:
us-east-1:
AMI: ami-0c55b159cbfafe1f0
us-west-1:
AMI: ami-0a10b27219a5094d7
Resources:
MyEC2Instance:
Type: AWS::EC2::Instance
Properties:
InstanceType: !Ref InstanceTypeParameter
ImageId: !FindInMap [RegionMap, !Ref "AWS::Region", AMI]
Outputs:
InstanceID:
Description: Instance ID of the EC2 instance
Value: !Ref MyEC2Instance
1.Parameters
Parameters allow users to customize the behavior of your CloudFormation stack. In this example, InstanceTypeParameter is defined as a parameter of type String with a default value of t2.micro. Users deploying the stack can choose from a list of allowed values, which are t2.micro, m1.small, and m1.medium. This parameter is used to specify the instance type for the EC2 instance.
2.Mappings
Mappings are useful for defining conditional values based on the regions. In this example, the RegionMap mapping maps AWS regions to specific AMIs. So, depending on the region in which the stack is being deployed, the appropriate AMI will be selected for the EC2 instance. !FindInMap is an intrinsic function used to retrieve a value from the specified mapping. !Ref “AWS::Region” returns the current AWS region.
3.Resources
Resources are the actual things you want AWS to create for user In this example, it’s creating EC2 instance EC2 instance named MyEC2Instance with specific settings like the size and location. The instance type is set based on the value of the InstanceTypeParameter parameter, and the image ID is determined based on the region through the mapping.
4.Outputs
Outputs allow users to define certain values that are returned after the stack is created. It is like what to show after the stack creation. In this example, an output named InstanceID provides the instance ID of the EC2 instance created by the stack. !Ref is an intrinsic function that returns the ID of the specified resource, in this case, MyEC2Instance.
5.Intrinsic Functions
Intrinsic functions allow users to perform certain operations within a CloudFormation template. In the provided example, !Ref and !FindInMap are intrinsic functions. they’re like shortcuts for performing certain actions or calculations. Other common intrinsic functions include !Sub for string substitution and !GetAtt for retrieving attributes from resources.
6.Stack Policies
Stack Policies are used to control what actions can be performed on resources within a stack. It acts like rules being applied on the stack. They can be used to prevent resources from being deleted, updated, or replaced during stack updates. Stack Policies are written in JSON format and applied to a stack to achieve desired update behaviors.\
AWS CloudFormation Templates
CloudFormation is a popular Infrastructure as a code (Iac) tool provided by Amazon web services (AWS) that allows users to automate the provisioning of AWS services such as EC2, S3, Lamda, etc.CloudFormation manages the entire lifecycle of your infrastructure, including provisioning, updating, and deleting resources. It handles dependencies between resources and provides features like rollback and drift detection to maintain the desired state of your infrastructure.