AWS CloudFormation itself is free to use, meaning there are no additional charges for using the service. However, you will incur costs for the AWS resources that you create and manage through CloudFormation.
Key takeaways:
AWS CloudFormation allows managing AWS resources through code, reducing manual setup and enhancing automation.
CloudFormation uses templates (written in YAML or JSON) to define AWS resources, ensuring quick, consistent, and repeatable deployments.
It simplifies complex infrastructure management by automating resource creation and supporting version control for easier changes and rollbacks.
CloudFormation templates can be reused across AWS regions to replicate infrastructure and ensure consistency.
CloudFormation helps track, control, and optimize infrastructure changes, streamlining the deployment process and reducing errors.
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.
For 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 Management 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 makes it 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 this 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 the new S3 bucket to open the dashboard of the S3 bucket we just provisioned with CloudFormation.
Want to learn more? Check out the "Getting to Know AWS CloudFormation"
offered by Educative to learn more about CoudFormation and get hands-on experience with using this service. Cloud Lab Cloud Labs provide hands-on access for learners to interact with cloud services, with zero pain from payments, setup, or cleanup, all right here in your Educative account.
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.
Havenāt found what you were looking for? Contact Us
Free Resources