CloudFormation is essentially an AWS tool that allows us to deal with the infrastructure as code on the AWS cloud. Dealing with the infrastructure as code (IaC) means that we can define instructions in the form of code to create and configure different resources in the infrastructure.
Note: Follow the “What are the advantages of using infrastructure in code?” Answer to learn further about IaC and its advantages.
With AWS CloudFormation, we can maintain a highly secure, manageable, and scalable infrastructure. By dealing with our AWS resources through code, we can spend less time managing them and more time focusing on the applications deployed on them.
To progress with this Answer, we must have a working AWS account with permissions for AWS CloudFormation and AWS S3.
We can define the AWS resources we want in a stack and how to configure them by defining them in a code file called a template. We can provide this template to CloudFormation, which evaluates the resources and their configurations defined in the template to automatically begin provisioning them on the AWS cloud. With CloudFormation, we don’t have to create each AWS resource individually using the AWS console or API ourselves.
The following are some scenarios where the CloudFormation service can be useful in managing the AWS Cloud infrastructure as code:
Simplify infrastructure management: We can use a CloudFormation template to provision and configure multiple resources simultaneously.
Easily replicate AWS resources across AWS regions: We can easily replicate our application in multiple AWS regions by reusing the same CloudFormation template, ensuring consistency and saving time in resource provisioning.
Track and control changes in infrastructure: We can efficiently manage and track changes to our infrastructure by using CloudFormation templates. These templates allow easy version control and rollback to previous configurations if needed.
Automation: As previously discussed, CloudFormation allows us to deal with infrastructure as code and, hence, automate the creation of resources. This way, it’s easier to manage the infrastructure with no overhead of manually creating resources.
Before provisioning and configuring any AWS cloud resources in a CloudFormation stack, we must understand and learn to work with CloudFormation templates. Templates are the blueprints for our AWS infrastructure, describing all the AWS resources we want to create and configure. Templates can be version-controlled, just like any other code, making it easier to roll back any changes in case of deployment failure without compromising the entire infrastructure.
Templates are written in a declarative language, and JSON and YAML are currently supported. For the Answer, we’ll use YAML for the provided CloudFormation templates.
The CloudFormation template has several sections. Furthermore, each supported AWS resource has its syntax and parameters that we must follow. To simplify things, we’ll only explore the most commonly used sections of the CloudFormation template, which is enough to start working with templates for the commonly used general CloudFormation stacks.
Here’s the basic skeleton code format for a CloudFormation template in JSON and YAML that covers the first basic layer of the template with the most common sections:
AWSTemplateFormatVersion: "2010-09-09"# Adding a Comment in a YAML templateDescription: >This is a sample descriptionin a YAML template that has multiline valuesupport.Parameters:<Set of Parameters Formatted as List of YAML Objects>Resources:<Set of Resources Formatted as List of YAML Objects>Outputs:<Set of Outputs Formatted as List of YAML Objects>
Here’s a brief explanation of the most common sections that we can see in both the JSON and YAML CloudFormation templates:
AWSTemplateFormatVersion
(optional): This section identifies the capabilities of the template. Its default value is the latest template format version.
Description
(optional): This section contains the textual description of the template. It must be written right after the template format version section.
Parameters
(optional): In this section, we specify a single or list of YAML/JSON objects representing parameters that allow us to input custom values into the CloudFormation template when creating or updating a stack. It can be beneficial to have parameters within our template, especially when updating a stack, as we only need to input a new parameter value once that can be referenced in the Resources
and Outputs
sections and avoid changing the value at multiple instances in the template.
Resources
(required): This section is the most important and the only required section of a CloudFormation template. It specifies the resources we want to create. This section supports the following fields:
Logical ID: This field is a unique alphanumeric ID that references the corresponding AWS resource in a CloudFormation stack and within other sections in the template itself. The logical ID is different from the physical ID of an AWS resource, which is the actual ID of the resource.
Type: This field represents the type of AWS resource that we want to create. The resource type field has the following format: service-provider::service-name::resource-type
. For example, if we want to create a Lambda function, we’ll write AWS::Lambda::Function
as the resource type.
Properties: This field is where we can specify any additional properties for the AWS resource.
Outputs
(optional): This section allows us to return the desired values related to the stack. The returned values are listed under properties.
Now that we’ve explored the basic anatomy of a CloudFormation template and how to create one let’s see an example of a template we can use to create a simple S3 bucket. We’ll go over creating this template step by step.
Note: To learn more about the AWS S3 service, follow the “What is Amazon S3, and how is it used?” Answer.
Let’s begin creating our CloudFormation template by providing the valid template format version at the start of it:
AWSTemplateFormatVersion: "2010-09-09"
To keep things simple, let’s just provide the required Resources
section and the template format version. We’ll define our S3 resource under this section. The Resources
section is defined in the template below on line 3:
AWSTemplateFormatVersion: "2010-09-09"Resources:
Next, we define a unique logical ID that will be used to reference the S3 bucket within the stack on line 4:
AWSTemplateFormatVersion: "2010-09-09"Resources:NewS3Bucket:
Let’s define the resource type that specifies what AWS resource we want to provision. Previously, we discussed that the format of the resource type should be in the service-provider::service-name::resource-type
form.
Since we want to create an S3 bucket, we’ll provide AWS as the service provider, S3 as the service name, and bucket as the resource type. Based on this information, the format of the resource type would be AWS::S3::Bucket
, as seen on line 5 in the template below. Just this template would be able to create a valid stack with a randomly named S3 bucket provisioned within it.
AWSTemplateFormatVersion: "2010-09-09"Resources:NewS3Bucket:Type: AWS::S3::Bucket
Now that we’ve finalized a CloudFormation template let’s see how we can provision a stack with an S3 bucket.
Now that we’ve finalized a CloudFormation template, we need to use it to provision a stack with an S3 bucket. To do so, we need to perform the following steps in the AWS Management Console:
Download the template.yaml
file for provisioning the S3 bucket:
AWSTemplateFormatVersion: "2010-09-09"Resources:NewS3Bucket:Type: AWS::S3::Bucket
Log in to the AWS Management Console using your AWS credentials.
On the AWS Management Console, search for “CloudFormation” and click the “CloudFormation” service from the search results. This takes us to the CloudFormation dashboard.
In the sidebar, click the “Stacks” option.
Click the “Create stack” drop-down menu button and select the “With new resources (standard)” option to create a stack with new AWS resources.
In the “Prerequisite – Prepare template” section, keep the default “Choose an existing template” option as selected under the “Prepare template” menu.
In the “Specify template” section, perform the following steps:
Under the “Template source” menu, select the “Upload a template file” option.
Click the “Choose file” button and upload the previously downloaded template.yaml
file.
Click the “Next” button to proceed to the next page.
Enter my-stack
as the stack name.
Click the “Next” button to proceed to the next page.
We do not need to make any changes to this page.
Click the “Next” button to proceed to the next page.
Review the settings for the new stack and ensure everything follows the instructions above.
Finally, click the “Submit” button to start the creation process for the stack.
We can view the stack once it has been successfully created, which we can verify when the status changes to CREATE_COMPLETE
. Click the “Resources” tab to view the S3 bucket resource created, and click the “Physical ID” link of NewS3Bucket
to open the dashboard of the S3 bucket we just provisioned with CloudFormation.
In summary, AWS CloudFormation stands out as an essential tool for efficient infrastructure management in the cloud. Allowing us to define and automate the provisioning of AWS resources through code ensures a more streamlined, scalable, and manageable deployment process. This empowers teams to focus more on innovation and less on infrastructure setup and maintenance complexities.
Free Resources