What is AWS CloudFormation?
CloudFormation is a free service provided by AWS for implementing Infrastructure-as-Code (IaC) solutions. It is easy to build any solution in AWS from the UI — however, when your infrastructure grows, lots of manual effort is needed and manual effort often leads to human error. To make deployments fast and reliable, AWS CloudFormation lets you define your infrastructure in YAML or JSON templates.
Let's talk about the major components of CloudFormation templates that might be useful in your cloud journey.
8 Key Components of CloudFormation Templates
1. Format Version
The Format Version tells CloudFormation which version of the template specification you are using. This is an optional field, but recommended for clarity.
2. Description
As the name states, you can put description text here to explain the purpose and features of the template. This helps team members understand the intent at a glance.
3. Metadata
Also optional, Metadata is mostly used to keep more detailed information about the resources that are going to be provisioned.
"Metadata" : {
"Instances" : {"Description" : "My Instances"},
"Databases" : {"Description" : "My DB"}
}
4. Parameters
Same as parameters in other programming languages, CloudFormation Parameters are used to define custom values at runtime — for example, the EC2 instance type.
"Parameters" : {
"InstanceTypeParameter" : {
"Type" : "String",
"Default" : "t2.micro",
"AllowedValues" : ["t2.micro"],
"Description" : "Enter t2.micro."
}
}
5. Mappings
Mappings is a key-value section that defines accepted values for a particular key. For example, using a Region Name as a key and the desired AWS Region Name as the value.
"Mappings" : {
"Mapping01" : {
"Key01" : { "Name" : "Value01" },
"Key02" : { "Name" : "Value02" }
}
}
6. Conditions
Conditions let you input logic using intrinsic functions. If a condition returns true, resources are created or updated; otherwise, they are skipped.
"Conditions" : {
"Logical ID" : { IntrinsicFunction }
}
7. Resources (Required)
This is the only mandatory section, used to define the actual resources to be deployed.
"Resources" : {
"MyEC2Instance" : {
"Type" : "AWS::EC2::Instance",
"Properties" : {
"ImageId" : "ami-0ff8a91507f77f867"
}
}
}
8. Outputs
This section outputs values after a template stack is executed. Those values can be imported and used by other stacks.
"Export" : {
"Name" : {
"Fn::Join" : [ ":", [ { "Ref" : "AWS::StackName" }, "AccountVPC" ] ]
}
}
Should You Use CloudFormation?
CloudFormation is really useful when you want to deploy AWS resources using automation. However, tools like Terraform can replace it for hybrid-cloud scenarios, as Terraform is platform-independent. Our recommendation depends on your specific cloud strategy.
Looking for a DevOps or Cloud team? contactus@sequencetechno.com for a free consultation.