AWS Classroom Series – 27/May/2020

Novice Exercise Solution

  • Refer the below Section
{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "This template is written for learning and it creates s3 bucket and ec2 instance",
    "Parameters": {
        "keypair": {
            "Description": "key pair for ec2 instance",
            "Type": "AWS::EC2::KeyPair::KeyName"
        },
        "instancetype": {
            "Type": "String",
            "AllowedValues": [
                "t2.micro",
                "t2.nano"
            ]
        }
    },
    "Resources": {
        "mynetwork": {
            "Type": "AWS::EC2::VPC",
            "Properties": {
                "CidrBlock": "10.10.0.0/16",
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "my network"
                    }
                ]
            }
        },
        "subnet1": {
            "Type": "AWS::EC2::Subnet",
            "Properties": {
                "AvailabilityZone": "us-west-2a",
                "VpcId": {
                    "Ref": "mynetwork"
                },
                "CidrBlock": "10.10.0.0/24",
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "subnet1"
                    }
                ]
            }
        },
        "subnet2": {
            "Type": "AWS::EC2::Subnet",
            "Properties": {
                "AvailabilityZone": "us-west-2b",
                "VpcId": {
                    "Ref": "mynetwork"
                },
                "CidrBlock": "10.10.1.0/24",
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "subnet2"
                    }
                ]
            }
        },
        "subnet3": {
            "Type": "AWS::EC2::Subnet",
            "Properties": {
                "AvailabilityZone": "us-west-2c",
                "VpcId": {
                    "Ref": "mynetwork"
                },
                "CidrBlock": "10.10.2.0/24",
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "subnet2"
                    }
                ]
            }
        },
        "myigw": {
            "Type": "AWS::EC2::InternetGateway",
            "Properties": {
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "myigw"
                    }
                ]
            }
        },
        "AttachGateway": {
            "Type": "AWS::EC2::VPCGatewayAttachment",
            "Properties": {
                "VpcId": {
                    "Ref": "mynetwork"
                },
                "InternetGatewayId": {
                    "Ref": "myigw"
                }
            }
        },
        "myroutetable": {
            "Type": "AWS::EC2::RouteTable",
            "Properties": {
                "VpcId": {
                    "Ref": "mynetwork"
                },
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "my rt"
                    }
                ]
            }
        },
        "routeName": {
            "Type": "AWS::EC2::Route",
            "Properties": {
                "RouteTableId": {
                    "Ref": "myroutetable"
                },
                "DestinationCidrBlock": "0.0.0.0/0",
                "GatewayId": {
                    "Ref": "myigw"
                }
            }
        },
        "routeTableAssocName": {
            "Type": "AWS::EC2::SubnetRouteTableAssociation",
            "Properties": {
                "SubnetId": {
                    "Ref": "subnet1"
                },
                "RouteTableId": {
                    "Ref": "myroutetable"
                }
            }
        },
        "InstanceSecurityGroup": {
            "Type": "AWS::EC2::SecurityGroup",
            "Properties": {
                "GroupDescription": "Allow ssh to client host",
                "VpcId": {
                    "Ref": "mynetwork"
                },
                "SecurityGroupIngress": [
                    {
                        "IpProtocol": "tcp",
                        "FromPort": 22,
                        "ToPort": 22,
                        "CidrIp": "0.0.0.0/0"
                    }
                ]
            }
        },
        "myweb": {
            "Type": "AWS::EC2::Instance",
            "Properties": {
                "KeyName": {
                    "Ref": "keypair"
                },
                "ImageId": "ami-003634241a8fcdec0",
                "InstanceType": {
                    "Ref": "instancetype"
                },
                "NetworkInterfaces": [
                    {
                        "AssociatePublicIpAddress": "true",
                        "DeviceIndex": "0",
                        "GroupSet": [
                            {
                                "Ref": "InstanceSecurityGroup"
                            }
                        ],
                        "SubnetId": {
                            "Ref": "subnet1"
                        }
                    }
                ],
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "myweb"
                    }
                ]
            }
        }
    }
}
  • The above template has only one restriction i.e. it works only in us-west-2, if you want to fix it make imageid/amiid as parameter

Leave a ReplyCancel reply

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

Please turn AdBlock off
Animated Social Media Icons by Acurax Responsive Web Designing Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version
%%footer%%