AWS Cloudformation 101
AWS CloudFormation is a crucial AWS service that allows for the creation, updating, and deletion of AWS resources using infrastructure as code, through templates in text file formats.
CloudFormation ensures the secure and repeatable provisioning of resources, eliminating the need for manual actions or custom script development. This service not only manages the stack of resources but can also automatically roll back changes if errors are detected.
Essentially, CloudFormation simplifies the creation and management of a collection of resources in AWS, enabling orderly and predictable provisioning and updating. Additionally, it facilitates infrastructure version control and offers the ability to execute templates via the command line interface (CLI) for efficient management.
It’s important to note that AWS CloudFormation does not impose additional charges for its usage and supports templates in JSON or YAML formats. Furthermore, this service is available in all AWS regions, ensuring global availability.
Cloudformation Overview
- Develop the template: In AWS CloudFormation, a “template” refers to a JSON or YAML file that describes the infrastructure of resources you want to deploy in your AWS environment.
- Submit the template to AWS Cloudformation: It means the process of sending a CloudFormation template (which describes the infrastructure you want to deploy in AWS) for AWS to process and create the resources specified in the template. In this step, you’ll need to provide the location of your CloudFormation template. You can either upload the file directly or provide an S3 URL where the template is stored.
- Template translation to API requests: It refers to the process of converting a CloudFormation template into API calls that can be programmatically executed to create, modify, or delete CloudFormation stacks and resources using the AWS CloudFormation API
- Create the resource and provisioning stack as execution environment: Once you confirm the creation of the stack, CloudFormation will process the template and begin creating the resources in your AWS account according to the template specifications.
Template syntax
Here’s an overview of the syntax for a CloudFormation template:
AWSTemplateFormatVersion: '2010-09-09'
Description: This CloudFormation template creates an EC2 instance and an RDS database.
Parameters:
InstanceType:
Type: String
Description: EC2 instance type
Default: t2.micro
AllowedValues:
- t2.micro
- t2.small
- t2.medium
Resources:
MyEC2Instance:
Type: AWS::EC2::Instance
Properties:
InstanceType: !Ref InstanceType
ImageId: ami-0123456789abcdef0
SecurityGroups:
- !Ref EC2SecurityGroup
EC2SecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Security group for the EC2 instance
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: 0.0.0.0/0
Outputs:
InstanceIP:
Description: IP address of the EC2 instance
Value: !GetAtt MyEC2Instance.PublicIp
- “AWSTemplateFormatVersion“: specifies the CloudFormation template version.
- “Description“: provides a description of the template.
- “Parameters“: define customizable parameters like InstanceType.
- “Resources“: define AWS resources, such as an EC2 instance (MyEC2Instance) and an EC2 security group (EC2SecurityGroup).
- “Properties“: specify the configuration properties for each resource.
- “Outputs“: define the outputs, such as the IP address of the EC2 instance (InstanceIP).
Certainly! In an AWS CloudFormation template in YAML format, in addition to the elements I mentioned earlier (such as Parameters, Resources, and Outputs), you can also use the following elements to control and customize the deployment of your infrastructure:
Mappings:
RegionMap:
us-east-1:
HVM64: "ami-0c55b159cbfafe1f0"
HVMG2: "ami-0a91cd140a1fc148a"
us-west-2:
HVM64: "ami-0c5181c61ed0b1b8f"
HVMG2: "ami-0e3f855cda1d73509"
Conditions:
CreateProdResources: !Equals [ !Ref EnvironmentType, prod ]
Transform: 'AWS::Serverless-2016-10-31'
- “Mappings“: Mappings are used to create lookup tables of values based on keys and names. These tables are commonly used to associate parameters or conditions with specific values based on the deployment context. You can use these mappings in the “Properties” section of resources to automatically select specific values based on the region or context.
- “Conditions“: Conditions allow you to create conditional statements that determine whether a resource should be created or configured based on certain criteria. This is useful when you want the presence or configuration of a resource to depend on a logical condition. You can use this condition in the “Resources” section to control whether a resource is created or not based on the value of `EnvironmentType`.
- “Transforms“: Transforms allow you to extend CloudFormation’s capabilities using transformation templates. These templates define how CloudFormation should process the main template before deployment. For example, you can use transformations to enable the creation of AWS Serverless Application Model (AWS SAM) specific AWS resources or to add custom functions. This transformation enables specific AWS SAM features in the template.
These additional elements (Mappings, Conditions, and Transforms) add greater flexibility and control to your CloudFormation templates, allowing you to create more customized and logic-driven deployments. Each of these elements can be used according to your specific deployment needs.