AWS Classroom Series- Cloudformation – 03/Oct/2019

Lets Add the compute to Network

  • So far we have this
{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "This is my first cf template",
    "Parameters": {
        "subnet1az": {
            "Description": "Enter subnet1 az",
            "Type": "AWS::EC2::AvailabilityZone::Name"
        },
        "subnet2az": {
            "Description": "Enter subnet1 az",
            "Type": "AWS::EC2::AvailabilityZone::Name"
        },
        "subnet3az": {
            "Description": "Enter subnet1 az",
            "Type": "AWS::EC2::AvailabilityZone::Name"
        }
    },
    "Resources": {
        "myvpc": {
            "Type": "AWS::EC2::VPC",
            "Properties": {
                "CidrBlock": "10.1.0.0/16",
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "From VsCode"
                    }
                ]
            }
        },
        "mysubnet1": {
            "Type": "AWS::EC2::Subnet",
            "Properties": {
                "AvailabilityZone": {
                    "Ref": "subnet1az"
                },
                "VpcId": {
                    "Ref": "myvpc"
                },
                "CidrBlock": "10.1.0.0/24",
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "From VsCode"
                    }
                ]
            }
        },
        "mysubnet2": {
            "Type": "AWS::EC2::Subnet",
            "Properties": {
                "AvailabilityZone": {
                    "Ref": "subnet2az"
                },
                "VpcId": {
                    "Ref": "myvpc"
                },
                "CidrBlock": "10.1.1.0/24",
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "From VsCode"
                    }
                ]
            }
        },
        "mysubnet3": {
            "Type": "AWS::EC2::Subnet",
            "Properties": {
                "AvailabilityZone": {
                    "Ref": "subnet3az"
                },
                "VpcId": {
                    "Ref": "myvpc"
                },
                "CidrBlock": "10.1.2.0/24",
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "From VsCode"
                    }
                ]
            }
        },
        "myigw": {
            "Type": "AWS::EC2::InternetGateway",
            "Properties": {
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "From VsCode"
                    }
                ]
            }
        },
        "AttachGateway": {
            "Type": "AWS::EC2::VPCGatewayAttachment",
            "Properties": {
                "VpcId": {
                    "Ref": "myvpc"
                },
                "InternetGatewayId": {
                    "Ref": "myigw"
                }
            }
        },
        "myrt": {
            "Type": "AWS::EC2::RouteTable",
            "Properties": {
                "VpcId": {
                    "Ref": "myvpc"
                },
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "From VsCode"
                    }
                ]
            }
        },
        "igwroute": {
            "Type": "AWS::EC2::Route",
            "Properties": {
                "RouteTableId": {
                    "Ref": "myrt"
                },
                "DestinationCidrBlock": "0.0.0.0/0",
                "GatewayId": {
                    "Ref": "myigw"
                }
            }
        },
        "subnet1rt": {
            "Type": "AWS::EC2::SubnetRouteTableAssociation",
            "Properties": {
                "SubnetId": {
                    "Ref": "mysubnet1"
                },
                "RouteTableId": {
                    "Ref": "myrt"
                }
            }
        },
        "subnet2rt": {
            "Type": "AWS::EC2::SubnetRouteTableAssociation",
            "Properties": {
                "SubnetId": {
                    "Ref": "mysubnet2"
                },
                "RouteTableId": {
                    "Ref": "myrt"
                }
            }
        },
        "mysubnet3rt": {
            "Type": "AWS::EC2::SubnetRouteTableAssociation",
            "Properties": {
                "SubnetId": {
                    "Ref": "mysubnet3"
                },
                "RouteTableId": {
                    "Ref": "myrt"
                }
            }
        }
    }
}
  • Create a ec2 machine manually
  • To create ec2 machine we need
    • AMI => ubuntu
    • instance type => t2.micro
    • Network, subnet & public ip => subnet => subnet1 with public ip
    • Security group => create a security group
    • key pair => Use an existing key pair with choice to user
  • Lets create a security group resource
{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "This is my first cf template",
    "Parameters": {
        "subnet1az": {
            "Description": "Enter subnet1 az",
            "Type": "AWS::EC2::AvailabilityZone::Name"
        },
        "subnet2az": {
            "Description": "Enter subnet1 az",
            "Type": "AWS::EC2::AvailabilityZone::Name"
        },
        "subnet3az": {
            "Description": "Enter subnet1 az",
            "Type": "AWS::EC2::AvailabilityZone::Name"
        },
        "keypairname": {
            "Description": "keyvaluepair",
            "Type": "AWS::EC2::KeyPair::KeyName"
        }
    },
    "Resources": {
        "myvpc": {
            "Type": "AWS::EC2::VPC",
            "Properties": {
                "CidrBlock": "10.1.0.0/16",
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "Corrected CF"
                    }
                ]
            }
        },
        "mysubnet1": {
            "Type": "AWS::EC2::Subnet",
            "Properties": {
                "AvailabilityZone": {
                    "Ref": "subnet1az"
                },
                "VpcId": {
                    "Ref": "myvpc"
                },
                "CidrBlock": "10.1.0.0/24",
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "Corrected CF"
                    }
                ]
            }
        },
        "mysubnet2": {
            "Type": "AWS::EC2::Subnet",
            "Properties": {
                "AvailabilityZone": {
                    "Ref": "subnet2az"
                },
                "VpcId": {
                    "Ref": "myvpc"
                },
                "CidrBlock": "10.1.1.0/24",
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "Corrected CF"
                    }
                ]
            }
        },
        "mysubnet3": {
            "Type": "AWS::EC2::Subnet",
            "Properties": {
                "AvailabilityZone": {
                    "Ref": "subnet3az"
                },
                "VpcId": {
                    "Ref": "myvpc"
                },
                "CidrBlock": "10.1.2.0/24",
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "Corrected CF"
                    }
                ]
            }
        },
        "myigw": {
            "Type": "AWS::EC2::InternetGateway",
            "Properties": {
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "Corrected CF"
                    }
                ]
            }
        },
        "AttachGateway": {
            "Type": "AWS::EC2::VPCGatewayAttachment",
            "Properties": {
                "VpcId": {
                    "Ref": "myvpc"
                },
                "InternetGatewayId": {
                    "Ref": "myigw"
                }
            }
        },
        "myrt": {
            "Type": "AWS::EC2::RouteTable",
            "Properties": {
                "VpcId": {
                    "Ref": "myvpc"
                },
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "Corrected CF"
                    }
                ]
            }
        },
        "igwroute": {
            "Type": "AWS::EC2::Route",
            "Properties": {
                "RouteTableId": {
                    "Ref": "myrt"
                },
                "DestinationCidrBlock": "0.0.0.0/0",
                "GatewayId": {
                    "Ref": "myigw"
                }
            }
        },
        "subnet1rt": {
            "Type": "AWS::EC2::SubnetRouteTableAssociation",
            "Properties": {
                "SubnetId": {
                    "Ref": "mysubnet1"
                },
                "RouteTableId": {
                    "Ref": "myrt"
                }
            }
        },
        "subnet2rt": {
            "Type": "AWS::EC2::SubnetRouteTableAssociation",
            "Properties": {
                "SubnetId": {
                    "Ref": "mysubnet2"
                },
                "RouteTableId": {
                    "Ref": "myrt"
                }
            }
        },
        "mysubnet3rt": {
            "Type": "AWS::EC2::SubnetRouteTableAssociation",
            "Properties": {
                "SubnetId": {
                    "Ref": "mysubnet3"
                },
                "RouteTableId": {
                    "Ref": "myrt"
                }
            }
        },
        "openssh": {
            "Type": "AWS::EC2::SecurityGroup",
            "Properties": {
                "GroupDescription": "open ssh port for all",
                "SecurityGroupIngress": [
                    {
                        "CidrIp": "0.0.0.0/0",
                        "Description": "open ssh port",
                        "FromPort": "22",
                        "IpProtocol": "tcp",
                        "ToPort": "22"
                    }
                ],
                "VpcId": {
                    "Ref": "myvpc"
                },
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "Corrected CF"
                    }
                ]
            }
        },
        "apache1": {
            "Type": "AWS::EC2::Instance",
            "Properties": {
                "KeyName": {
                    "Ref": "keypairname"
                },
                "ImageId": "ami-06f2f779464715dc5",
                "InstanceType": "t2.micro",
                "SecurityGroupIds": [
                    {
                        "Ref": "openssh"
                    }
                ],
                "SubnetId": {
                    "Ref": "mysubnet2"
                },
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "Corrected CF"
                    }
                ]
            }
        }
    }
}
  • Updating the template is also possible. Whenever you change any field look at update requires. If the value of it is replacement, it means downtime ( a new resource will be created as a result of this)

  • We still have a problem, our ec2 machine is not getting public ip address and this template works only in oregon (as AMI is hardcoded)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner