AWS CloudFormation is a powerful tool provided by Amazon Web Services that allows you to use a simple text file to model and provision all the resources needed for your applications across all regions and accounts. This file is the template, and the format can be either JSON or YAML. Here’s how to set up basic configurations using CloudFormation, along with specific examples of templates.
CloudFormation templates are structured data files that define the resources you need to deploy for your application. Here’s a simple example that creates an Amazon EC2 instance.
YAML Format
AWSTemplateFormatVersion: '2010-09-09'
Description: Simple EC2 Instance Example
Resources:
MyEC2Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-0c55b159cbfafe1f0
InstanceType: t2.micro
SecurityGroups:
- !Ref MySecurityGroup
KeyName: my-key-pair
MySecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Allow SSH access from anywhere
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
JSON Format
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Simple EC2 Instance Example",
"Resources": {
"MyEC2Instance": {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId": "ami-0c55b159cbfafe1f0",
"InstanceType": "t2.micro",
"SecurityGroups": [
{ "Ref": "MySecurityGroup" }
],
"KeyName": "my-key-pair"
}
},
"MySecurityGroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": "Allow SSH access from anywhere",
"SecurityGroupIngress": [
{
"IpProtocol": "tcp",
"FromPort": 22,
"ToPort": 22,
"CidrIp": "0.0.0.0/0"
}
]
}
}
}
}
You can deploy this template directly from the AWS Management Console, or use the AWS CLI.
aws cloudformation create-stack --stack-name MyEC2Stack --template-body file://mytemplate.yaml
Replace mytemplate.yaml
with the path to your template file.
aws cloudformation describe-stacks
to get details about the stack and its status.