In this blog, you will learn how to create an ec2 using aws cloudformation yaml template.
In AWS, you can deploy any resource as infra as a code using AWS cloudformation templates using YAML or JSON language.
First, you must know the template anatomy for publishing cloudformation template.
The following describes the aws cloudformation yaml fragment.
AWSTemplateFormatVersion: "version date"
Description:
Metadata:
Parameters:
Rules:
Mappings:
Conditions:
Transform:
Resources:
Outputs:
from the above template anatomy , AwsTemplateFormaVersion, Resources are must be defined in the cloud formation template.
the remaining sections are default.
from the below the cloudformation template , you can learn how to create an ec2 instance using aws cloudformaion template using yaml languate.
AWSTemplateFormatVersion: 2010-09-09
Description: deploy a vpc and subnet using aws cloudformation template
Resources:
myactivtyVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16
EnableDnsSupport: 'true'
EnableDnsHostnames: 'true'
Tags:
-
Key: Name
Value: myVPC
myactivitySubnet:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref myactivtyVPC
CidrBlock: 10.0.1.0/16
AvailabilityZone: "eu-west-1a"
Tags:
-
Key: Name
Value: mySubnet
# define a internet gateway using aws cloudformation
myactivityIGW:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
-
Key: Name
Value: myIGW
myVPCGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref myactivtyVPC
InternetGatewayId: !Ref myactivityIGW
#define a route table
myRTB:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref myactivtyVPC
Tags:
-
Key: Name
Value: myRTB
myInternetRoute:
Type: AWS::EC2::Route
DependsOn: myactivityIGW
Properties:
RouteTableId: !Ref myRTB
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref myactivityIGW
myRTBSubnetAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref myactivitySubnet
RouteTableId: !Ref myRTB
#lanuch a ec2 instance
myEc2Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-07355fe79b493752d
InstanceType: t2.micro
KeyName: pubLINUXkey
SubnetId: !Ref myactivitySubnet
Description: deploy a vpc and subnet using aws cloudformation template
Resources:
myactivtyVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16
EnableDnsSupport: 'true'
EnableDnsHostnames: 'true'
Tags:
-
Key: Name
Value: myVPC
myactivitySubnet:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref myactivtyVPC
CidrBlock: 10.0.1.0/16
AvailabilityZone: "eu-west-1a"
Tags:
-
Key: Name
Value: mySubnet
# define a internet gateway using aws cloudformation
myactivityIGW:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
-
Key: Name
Value: myIGW
myVPCGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref myactivtyVPC
InternetGatewayId: !Ref myactivityIGW
#define a route table
myRTB:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref myactivtyVPC
Tags:
-
Key: Name
Value: myRTB
myInternetRoute:
Type: AWS::EC2::Route
DependsOn: myactivityIGW
Properties:
RouteTableId: !Ref myRTB
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref myactivityIGW
myRTBSubnetAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref myactivitySubnet
RouteTableId: !Ref myRTB
#lanuch a ec2 instance
myEc2Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-07355fe79b493752d
InstanceType: t2.micro
KeyName: pubLINUXkey
SubnetId: !Ref myactivitySubnet