Updated March 21, 2023
Introduction to AWS CloudFormation Templates
AWS CloudFormation is a service that manages and sets different AWS resources together so that the time is taken to perform these can be decreased, and time focusing on different applications in Amazon Web Services can be increased. In order to achieve this, a template is used that contains all the resources that the user needs. These templates are known as CloudFormation templates. With the help of these templates, AWS CloudFormation configures and provisions those resources for the user. The structure and working of the template are described in the next section.
Different CloudFormation Templates
Amazon CloudFormation template is a formatted text file in YAML or JSON language. These templates can be either created with the help of a console or by writing a script manually. In the console, the resources will be dragged and dropped by the user. Once it is completed, a JSON or YAML script will be generated automatically, and the user can edit it if they want. To modify or edit the template, the user can use any text editor tool or AWS CloudFormation designer. In order to write, a certain format has to be followed with the following objects. The main objects of the CloudFormation template are described below.
1. Format: The version of the AWS CloudFormation template is defined here.
Example:
Code:
{
"AWSTemplateFormatVersion": "2019-09-09"
}
2. Description: Any comments or descriptions about the template can be noted in this object.
Example:
Code:
{
"Description": "An Amazon Redshift cluster is created within a Virtual Private Cloud”
}
3. Metadata: Further information about the template is defined in JSON or YAML Language.
Example:
Code:
{
"Metadata":
{
"AWS::CloudFormation::Interface": {}
}
4. Parameters: Customization of templates can be done using the parameters. It is by giving custom values to the template when the stack is created or updated.
Example:
Code:
"Parameters" :
{
"KeyName": {}
"InstanceType" : {}
"DBName" : {}
"DBUser" : {}
"DBPassword" : {}
}
5. Mappings: Based on a value in the conditional parameter, the user will be allowed to map the key to it. Moreover, users can retrieve values from a map using an intrinsic function, “Fn:: FindInMap.”
Example:
Code:
{
"AWSInstanceType2Arch" :
{
"t1.micro" : { "Arch" : "HVM64" },
.
.
"t2.large" : { "Arch" : "HVM64" },
"m1.small" : { "Arch" : "HVM64" },
.
"m1.large" : { "Arch" : "HVM64" }
}
6. Resources: In the Resources section in the AWS CloudFormation template, it is possible to declare resources such as AWS Simple Storage Services bucket (S3), AWS Lambda. These resources can be created and specified in the stack also.
Example:
Code:
"Resources" :
{
"WebServerSecurityGroup" :
{
"Type" : " ",
"Properties" :
{
"GroupDescription" : "",
"SecurityGroupIngress" :[]
}
}
}
7. Output: The output section contains the values that need to be imported to other stacks or returned ones while you view your own stack properties.
Example:
Code:
"Outputs" :
{
"Description" : "”
“Value”: “”
}
}
Now, let us combine all these sections and see how a template will be looking.
Sample Template
Code:
{
"AWSTemplateFormatVersion": "2019-09-09"
"Description": "An Amazon Redshift cluster is created within a Virtual Private Cloud”
"Metadata": {
"AWS::CloudFormation::Interface": {}
}
"Parameters" : {
"KeyName": {}
"InstanceType" : {}
"DBName" : {}
"DBUser" : {}
"DBPassword" : {}
}
"Mappings" :
{
"AWSInstanceType2Arch" :
{
"t1.micro" : { "Arch" : "HVM64" },
.
.
"t2.large" : { "Arch" : "HVM64" },
"m1.small" : { "Arch" : "HVM64" },
.
"m1.large" : { "Arch" : "HVM64" }
}
"Resources" :
{
"WebServerSecurityGroup" :
{
"Type" : " ",
"Properties" :
{
"GroupDescription" : "",
"SecurityGroupIngress" :[]
}
}
}
"Outputs" :
{
"Description" : "”
“Value”: “”
}
}
Once the template is created, the user can upload the template to the stack.
Configuring CloudFormation Stack
A stack in AWS is a collection of resources that a single unit can manage. The resources can be created, deleted, and updated by creating, deleting, and updating stacks.
Now, let us see how we are launching this stack and use the CloudFormation template inside it.
1. Signup for the AWS Account if you don’t have one. If already present, log in using the credentials.
2. Open the console of AWS CloudFormation using the URL https://console.aws.amazon.com/cloudformation.
3. If you are having a new CloudFormation account, click Create New Stack. Else, Create Stack.
4. Select Upload a template to Amazon S3 from Choose a template. Upload your template by selecting Choose File or providing a URL.
5. Please note that if the CloudFormation template is stored in the S3 bucket, the user must have access to that one, and the regions of S3 Bucket and Stack should be the same.
6. Give an appropriate Stack name in the Specify Details section to your CloudFormation Stack.
7. Provide the name of EC2 Keypair in the KeyName
8. Make sure the EC2 Keypair and Stack are in the same region.
9. Click Next.
10. A page appears with some optional input fields like a tag. Tags help in identifying the stacks since it contains key-value pairs. But, now we are not creating any tags.
11. Review the details and select Create.
12. The progress of Stack creating can be seen now in the Events tab. If the creation of the stack is on-going, CREATE_IN_PROGRESS status will be shown, and if the creation is completed, CREATE_COMPLETED status will be shown.
13. Once the Stack is created, the user can start using the resources. However, in order to avoid additional charges for unwanted services, it is advised to delete the stacks and their resources.
To delete the stack, the following steps can be used.
- Go to the CloudFormation console and select the Stack you have created.
- Click Delete Stack.
- Click Yes, Delete when the confirmation message appears.
Conclusion
CloudFormation Templates helps in provisioning and configuring the resources for the user so that time taken to perform operations on multiple resources can be decreased. Focusing on different applications in Amazon Web Services can be increased. The creation and using of these templates are discussed in the above section.
Recommended Articles
This is a guide to AWS CloudFormation Templates. Here we discuss the introduction and different CloudFormation templates along with examples. You can also go through our other suggested articles to learn more –