AWS CloudFormation Explained

AWS CloudFormation Explained

What Is AWS CloudFormation?

AWS CloudFormation is an Amazon service that allows developers to manage and provision AWS resources predictably and consistently. In a nutshell, it's a tool that lets you define AWS infrastructure as code, and deploy it from code files.

The core component of AWS CloudFormation is a template file written in JSON or YAML format. This template describes the resources you need, their dependencies, and the necessary permissions. When you deploy this template, AWS CloudFormation takes care of creating and configuring the resources for you. This eliminates the need to manually create and configure resources, and it gives you a repeatable way to deploy infrastructure.

But CloudFormation goes beyond just initial setup. It can also manage the entire lifecycle of your resources including updates, tweaks, and even clean-up when resources are no longer needed. This makes it a vital part of DevOps workflows, enabling teams to implement Infrastructure as Code (IaC) practices, and thus improving the predictability, efficiency, and scalability of deployments.

Benefits of CloudFormation

These are the main advantages of defining infrastructure as code using CloudFormation

1. Repeatable and Consistent Deployments: With CloudFormation, you can use templates to define your resources, and then reuse these templates to create identical copies of your infrastructure. This can be incredibly useful for deploying test environments or setting up new regions.

2. Simplified Management: AWS CloudFormation consolidates the management of your AWS resources into a single platform. This means you can create, update, and delete a collection of resources as a single unit, which we call a stack.

3. DevOps Friendly: With its support for Infrastructure as Code (IaC), CloudFormation allows you to manage and provision your AWS resources using code. This makes it easy to integrate with CI/CD pipelines and source control systems, thus streamlining your DevOps practices.

4. Safety Controls: CloudFormation provides detailed controls over your resources. With features like rollback on failure and change sets, you can minimize risks during deployment.


Master AWS with Real Solutions and Best Practices. Subscribe to the free newsletter Simple AWS. 3000 engineers and tech experts already have.


AWS CloudFormation Basic Concepts

In order to effectively use AWS CloudFormation, there are several key concepts to understand:

Template

At the heart of AWS CloudFormation is the concept of a template. A template is a JSON or YAML formatted text file which serves as a blueprint for your AWS infrastructure. It defines the AWS resources you want to create and configure.

Templates are declarative, which means you only need to describe your desired state of resources, and AWS CloudFormation takes care of how to achieve that state. A template can be used repeatedly to create identical copies of the same stack (or to replicate the infrastructure with different settings).

Stacks

In AWS CloudFormation, a stack is the basic unit of management and deployment. It represents a collection of AWS resources that you can manage as a single unit. All resources in a stack are defined by the stack's AWS CloudFormation template.

You can create, update, or delete a collection of resources by creating, updating, or deleting stacks. All the resources in a stack are treated as a single unit, so operations and updates are atomic and consistent.

Formatting

As mentioned earlier, AWS CloudFormation templates can be written in either JSON or YAML, both of which are easily readable by humans and machines. While the choice between JSON or YAML mostly comes down to personal preference, YAML tends to be more compact and easier to read, which might be beneficial for larger templates.

Parameters

Parameters are a way to customize your AWS CloudFormation stacks. They allow you to pass custom values to your template at runtime, without the need to modify the template itself.

For instance, you might have a template that sets up an Amazon EC2 instance, and you want to use different instance types (like t3.micro, t3.small, etc.) depending on your needs. You can set up an instance type parameter in your template, and specify the desired instance type when you create or update the stack.

Conditions

Conditions in AWS CloudFormation templates enable you to control the creation of resources or the setting of properties based on different conditions. For example, you might want to create certain resources only in specific AWS regions, or configure properties differently based on the input parameters.

Change sets

Change sets are an important concept that helps you update your AWS CloudFormation stacks safely. Before making any changes to your stack, you can generate a change set, which is a summary of the proposed changes. This lets you see how changes might impact your resources or configuration before implementing them, allowing you to avoid potential disruptions or errors.

Functions

AWS CloudFormation provides a set of intrinsic functions that you can use within your templates to assign values to properties that are not available until runtime. These functions include capabilities such as retrieving the value of an AWS::StackName, working with strings, or including other files. They can help create more dynamic and flexible templates.

Anatomy of a CloudFormation Template

An AWS CloudFormation template is a formatted text file in JSON or YAML, which provides a user-friendly, human-readable format for specifying your infrastructure components. Here, we'll analyze the key sections found within a typical CloudFormation template:

AWS Template Format Version

This optional field defines the AWS CloudFormation template version that the template conforms to. It helps you ensure that the template is compatible with the correct AWS CloudFormation syntax.

AWSTemplateFormatVersion: "version date"

Description

A text string that describes the template. This field can help you and others understand the purpose and function of your template.

Description: String

Metadata

The optional Metadata section includes details about the template itself, such as the template file format version. It can also define some AWS-specific functionalities.

Metadata:
  Instances:
    Description: "Information about the instances"

Parameters

As described in the previous section, the Parameters section defines values that can be passed to your template at runtime. Parameters make your templates flexible and customizable.

Parameters:
  InstanceType:
    Description: "WebServer instance type"
    Type: "String"

Mappings

The Mappings section lets you map keys to corresponding named values that you can use in your template. Mappings can be used to determine values based on the region in which the stack is run, the instance type, or any other value.

Mappings:
  RegionMap:
    us-east-1:
      "32": "ami-6411e20d"

Conditions

Conditions define whether certain resources are created or whether certain resource properties are assigned a value during stack creation or update. For example, you could compare whether a value is equal to another value.

Conditions:
  CreateProdResources: !Equals [!Ref EnvironmentType, prod]

Resources

The Resources section is the only required section. It declares the AWS resources that you want to include in the stack, such as an Amazon EC2 instance or an Amazon S3 bucket.

Resources:
  MyEC2Instance:
    Type: "AWS::EC2::Instance"

Outputs

The Outputs section declares output values that you can import into other stacks or that you can easily check by using the AWS CloudFormation console, AWS CLI, or APIs.

Outputs:
  WebsiteURL:
    Value: !GetAtt [ WebServerInstance, PublicDnsName ]

How to Create a CloudFormation Template

Creating a CloudFormation template involves the following steps:

  1. Identify the AWS resources needed: To begin, you need to identify all the AWS resources that are needed for your project. This could be EC2 instances, S3 buckets, IAM Roles, or any other AWS service.

  2. Start with a blank file: You can create a new blank file using any text editor of your choice. This file can be in either YAML or JSON format.

  3. Write your template: AWS CloudFormation templates can have all of the sections we saw earlier, but the only required section is "Resources". This is where you define what AWS resources you want to create and configure.

    A basic CloudFormation template in YAML format looks like this:

     Resources:
         MyS3Bucket:
             Type: 'AWS::S3::Bucket'
    
  4. Validate your template: It's always a good idea to validate your CloudFormation template to catch any syntax errors. You can use the AWS Management Console, AWS CLI, or AWS SDKs to validate your template.

    To validate the template using AWS CLI:

     aws cloudformation validate-template --template-body file://template.json
    

How to Deploy a CloudFormation Template

Deploying a CloudFormation template involves the following steps:

  1. Log into AWS Console: Go to the AWS Management Console and enter your credentials to log in.

  2. Open the CloudFormation service: On the AWS Management Console, type 'CloudFormation' into the service search bar and then choose 'CloudFormation' to open the service console.

  3. Create a stack: In the CloudFormation console, choose 'Create stack', and then choose 'With new resources (standard)'.

  4. Specify the template: Choose 'Template is ready'. In the 'Template source' section, choose 'Upload a template file'. Choose 'Choose file', and then select your CloudFormation template.

  5. Specify stack details: For 'Stack name', use a unique name. Specify additional parameters under 'Parameters', and choose 'Next'.

  6. Configure stack options: You can optionally choose tags for your stack and set advanced options. When you're done, choose 'Next'.

  7. Review and create the stack: Review your settings, acknowledge that AWS CloudFormation might create IAM resources, and then choose 'Create stack'.

How to Update a CloudFormation Stack

Updating a CloudFormation stack involves the following steps:

  1. Open the CloudFormation service: Like in the previous section, got to the CloudFormation console on AWS.

  2. Select the stack: On the 'Stacks' page in the CloudFormation console, select the stack that you want to update.

  3. Choose to update the stack: Choose 'Actions', and then choose 'Update stack'.

  4. Provide the updated template: You can provide the updated template in several ways: directly in the AWS Management Console, by uploading a file, or specifying an Amazon S3 URL. Choose 'Next' after you've provided the updated template.

  5. Specify stack details: For 'Stack name', the existing name of the stack is displayed. You can't change the stack name. Specify additional parameters under 'Parameters', and choose 'Next'.

  6. Configure stack options and review: Configure any stack options, then review your settings. Acknowledge that AWS CloudFormation might create IAM resources with custom names, and then choose 'Update stack'.

Note that the AWS Management Console, AWS CLI, AWS SDKs, and APIs provide similar features for managing stacks. However, the console is typically the most straightforward method to use for stack management.

Advanced CloudFormation

Beyond the foundational use of CloudFormation that this blog post covers, there are several advanced features that can enhance and customize your CloudFormation stacks. These features include update rollback, stack policies, and, notably, AWS CloudFormation Hooks.

What Are AWS CloudFormation Hooks?

CloudFormation Hooks are scripts that run either before or after a particular lifecycle event, such as the creation, update, or deletion of a stack resource. They can be used to manage dependencies, perform custom validations, or implement sophisticated control logic.

Hooks are defined in the "Metadata" section of the resource definition within the CloudFormation template. Hooks are written as AWS Lambda functions, and you can define hooks in any runtime supported by AWS Lambda.

Here's an example of how you can define a hook:

Resources:
  MyInstance:
    Type: 'AWS::EC2::Instance'
    Metadata:
      'AWS::CloudFormation::Init':
        config:
          hooks:
            BeforeInstall:
              - location: s3://mybucket/myscript.sh
                timeout: '60'

In this example, the script "myscript.sh" is downloaded from an S3 bucket and executed before the 'Install' event of the CloudFormation lifecycle. If this script fails, the CloudFormation stack operation fails and rolls back.

Alternatives to CloudFormation

While AWS CloudFormation is a robust and comprehensive tool for managing AWS resources, several alternatives might better suit your needs based on your use case, the complexity of your infrastructure, and your personal preference.

  1. Terraform: Terraform, an open-source tool developed by HashiCorp, allows you to define and provision infrastructure using a declarative configuration language. Terraform is cloud-agnostic, meaning you can use it with multiple cloud providers simultaneously. It also has an extensive plugin system for integrating with other services.

  2. AWS CDK (Cloud Development Kit): The AWS CDK is a software development framework to define cloud infrastructure as code and provision it through AWS CloudFormation. With AWS CDK, you can leverage the expressiveness of modern programming languages, like TypeScript, Python, and Java, to define your infrastructure.

  3. Pulumi: Similar to AWS CDK, Pulumi lets you create, deploy, and manage cloud infrastructure using real programming languages, including JavaScript, TypeScript, Python, Go, and .NET. Pulumi supports multiple clouds in addition to AWS.

  4. Serverless Framework: For serverless applications, the Serverless Framework is a popular choice. It provides a simple and intuitive developer experience, with a focus on building and deploying serverless architectures.

Each of these tools has its strengths and trade-offs. The choice depends on your specific needs and circumstances. However, AWS CloudFormation remains a powerful choice due to its deep integration with AWS services and its wide adoption in the industry.

And there you have it! Now you should have a basic understanding of what AWS CloudFormation is and how it operates. From its fundamental concepts to the details of creating, deploying, and updating a CloudFormation stack, you have the tools to get started with CFN.


Master AWS with Real Solutions and Best Practices.
Join over 3000 devs, tech leads, and experts learning real AWS solutions with the Simple AWS newsletter.

  • Analyze real-world scenarios

  • Learn the why behind every solution

  • Get best practices to scale and secure them

Simple AWS is free. Start mastering AWS!

If you'd like to know more about me, you can find me on LinkedIn or at www.guilleojeda.com

Did you find this article valuable?

Support Guillermo Ojeda by becoming a sponsor. Any amount is appreciated!