Tutorial:
We will set up EC2 which is linked to provisioned subnets and security groups.
The stack contains 2 web server and 1 bastion server which are associated with provisioned security groups, and deployed on provisioned subnets.
In below template, "SUBNETS" and "SecurityGroup" are defined as we can specify and refer provisioned stacks.
On outputs, these EC2 resources are exported for future usage.
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "EC2 instances.",
"Parameters": {
"SUBNETS": {
"Type": "String"
},
"SecurityGroup": {
"Type": "String"
}
},
"Resources": {
"WebServer01": {
"Type": "AWS::EC2::Instance",
"Properties": {
"InstanceType" : "t2.micro",
"ImageId": "ami-0c00780768a0dad61",
"KeyName": "webserver_key",
"Tags": [
{
"Key": "Name",
"Value": "WebServer01"
}
],
"NetworkInterfaces": [
{
"DeviceIndex": "0",
"GroupSet": [
{
"Fn::ImportValue": {
"Fn::Sub": "${SecurityGroup}-WebServerSecurityGroup"
}
}
],
"SubnetId": {
"Fn::ImportValue": {
"Fn::Sub": "${SUBNETS}-Subnet01"
}
}
}
]
},
"Metadata": {
"AWS::CloudFormation::Designer": {
"id": "fc33911e-cd3b-496b-9975-a77684964fee"
}
}
},
"WebServer02": {
"Type": "AWS::EC2::Instance",
"Properties": {
"InstanceType" : "t2.micro",
"ImageId": "ami-0c00780768a0dad61",
"KeyName": "webserver_key",
"Tags": [
{
"Key": "Name",
"Value": "WebServer02"
}
],
"NetworkInterfaces": [
{
"DeviceIndex": "0",
"GroupSet": [
{
"Fn::ImportValue": {
"Fn::Sub": "${SecurityGroup}-WebServerSecurityGroup"
}
}
],
"SubnetId": {
"Fn::ImportValue": {
"Fn::Sub": "${SUBNETS}-Subnet02"
}
}
}
]
},
"Metadata": {
"AWS::CloudFormation::Designer": {
"id": "e0d44d1a-18b1-47db-92b2-71a9376bce33"
}
}
},
"Bastion": {
"Type": "AWS::EC2::Instance",
"Properties": {
"InstanceType" : "t2.micro",
"ImageId": "ami-0c00780768a0dad61",
"KeyName": "webserver_key",
"Tags": [
{
"Key": "Name",
"Value": "Bastion"
}
],
"NetworkInterfaces": [
{
"AssociatePublicIpAddress": "true",
"DeviceIndex": "0",
"GroupSet": [
{
"Fn::ImportValue": {
"Fn::Sub": "${SecurityGroup}-WebServerSecurityGroup"
}
}
],
"SubnetId": {
"Fn::ImportValue": {
"Fn::Sub": "${SUBNETS}-Subnet01"
}
}
}
]
},
"Metadata": {
"AWS::CloudFormation::Designer": {
"id": "f646646c-aa3f-4fec-9252-9eb16f19266a"
}
}
}
},
"Outputs" : {
"Bastion" : {
"Description" : "EC2 instance exported values",
"Value" : { "Ref" : "Bastion" },
"Export" : { "Name" : {"Fn::Sub": "${AWS::StackName}-Bastion" }}
},
"WebServer01" : {
"Description" : "EC2 instance exported values",
"Value" : { "Ref" : "WebServer01" },
"Export" : { "Name" : {"Fn::Sub": "${AWS::StackName}-WebServer01" }}
},
"WebServer02" : {
"Description" : "EC2 instance exported values",
"Value" : { "Ref" : "WebServer02" },
"Export" : { "Name" : {"Fn::Sub": "${AWS::StackName}-WebServer02" }}
}
}
}
See more detailed information of each element on
here.
Here's a video tutorial for this.