AWS Classroom Series – 22/May/2020

Cloudformation Template for the following

  • Simple
    • Create S3 bucket
    • Create an EC2 instance
    • Solution:
    {
        "AWSTemplateFormatVersion": "2010-09-09",
        "Description": "This template is written for learning and it creates s3 bucket and ec2 instance",
        "Resources": {
            
            "mys3": {
                "Type": "AWS::S3::Bucket",
                "Properties": {
                    "AccessControl": "PublicRead",
                    "BucketName": "qts3forpracticemay"
                }
            },
            "myec2": {
                "Type": "AWS::EC2::Instance",
                "Properties": {
                    "ImageId": "ami-0470e33cd681b2476",
                    "InstanceType": "t2.micro"
                }
    
            }
            
        }
    
    }
    
    • The above template is not resuable bcoz of names directly used in properties (hard-coded)
    • We can eliminate hard coding by using parameters Refer Here
    • Lets add a parameter for s3 buckets name and ec2 instance type and Image-Id. Refer Here for parameters section.
    • Lets add the string parameters to the template
    {
        "AWSTemplateFormatVersion": "2010-09-09",
        "Description": "This template is written for learning and it creates s3 bucket and ec2 instance",
        "Parameters": {
            "s3bucketname": {
                "Description": "The name of the bucket which you want to create",
                "Type": "String"
            },
            "ec2instancetype": {
                "Description": "Instance type of ec2",
                "Type": "String",
                "Default": "t2.micro"
            },
            "amiid": {
                "Description": "Image Id",
                "Type": "String",
                "Default": "ami-0470e33cd681b2476"
            }
        },
        "Resources": {
            "mys3": {
                "Type": "AWS::S3::Bucket",
                "Properties": {
                    "AccessControl": "PublicRead",
                    "BucketName": "qts3forpracticemay"
                }
            },
            "myec2": {
                "Type": "AWS::EC2::Instance",
                "Properties": {
                    "ImageId": "ami-0470e33cd681b2476",
                    "InstanceType": "t2.micro"
                }
            }
        }
    }
    
    • Lets add the references of the parameters in resources using Ref
    {
        "AWSTemplateFormatVersion": "2010-09-09",
        "Description": "This template is written for learning and it creates s3 bucket and ec2 instance",
        "Parameters": {
            "s3bucketname": {
                "Description": "The name of the bucket which you want to create",
                "Type": "String"
            },
            "ec2instancetype": {
                "Description": "Instance type of ec2",
                "Type": "String",
                "Default": "t2.micro"
            },
            "amiid": {
                "Description": "Image Id",
                "Type": "String",
                "Default": "ami-0470e33cd681b2476"
            },
            "s3accesscontrol": {
                "Description": "S3 Access control",
                "Type": "String",
                "Default": "PublicRead"
            }
        },
        "Resources": {
            "mys3": {
                "Type": "AWS::S3::Bucket",
                "Properties": {
                    "AccessControl": {
                        "Ref": "s3accesscontrol"
                    },
                    "BucketName": {
                        "Ref": "s3bucketname"
                    }
                }
            },
            "myec2": {
                "Type": "AWS::EC2::Instance",
                "Properties": {
                    "ImageId": {
                        "Ref": "amiid"
                    },
                    "InstanceType": {
                        "Ref": "ec2instancetype"
                    }
                }
            }
        }
    }
    
    • Now if we create the stack from cf template the parameters section will be shown in the UI Preview
    • Now lets restrict user from entering anything into parameters by using AllowedValues and using Regular expressions in parameter section AllowedPattern.
    {
        "AWSTemplateFormatVersion": "2010-09-09",
        "Description": "This template is written for learning and it creates s3 bucket and ec2 instance",
        "Parameters": {
            "s3bucketname": {
                "Description": "The name of the bucket which you want to create",
                "Type": "String"
            },
            "ec2instancetype": {
                "Description": "Instance type of ec2",
                "Type": "String",
                "AllowedValues": [
                    "t2.micro",
                    "t2.nano",
                    "t2.small"
                ],
                "Default": "t2.micro"
            },
            "amiid": {
                "Description": "Image Id",
                "Type": "String",
                "Default": "ami-0470e33cd681b2476"
            },
            "s3accesscontrol": {
                "Description": "S3 Access control",
                "Type": "String",
                "AllowedValues": [
                    "Private",
                    "PublicRead",
                    "PublicReadWrite",
                    "AuthenticatedRead",
                    "LogDeliveryWrite",
                    " BucketOwnerRead",
                    "BucketOwnerFullControl"
                ],
                "Default": "PublicRead"
            }
        },
        "Resources": {
            "mys3": {
                "Type": "AWS::S3::Bucket",
                "Properties": {
                    "AccessControl": {
                        "Ref": "s3accesscontrol"
                    },
                    "BucketName": {
                        "Ref": "s3bucketname"
                    }
                }
            },
            "myec2": {
                "Type": "AWS::EC2::Instance",
                "Properties": {
                    "ImageId": {
                        "Ref": "amiid"
                    },
                    "InstanceType": {
                        "Ref": "ec2instancetype"
                    }
                }
            }
        }
    }
    
    Preview Preview
    • Lets add the security group to the AWS Ec2 instance using a AWS Specific parameter AWS::EC2::SecurityGroup::GroupName and key pair
    {
        "AWSTemplateFormatVersion": "2010-09-09",
        "Description": "This template is written for learning and it creates s3 bucket and ec2 instance",
        "Parameters": {
            "s3bucketname": {
                "Description": "The name of the bucket which you want to create",
                "Type": "String"
            },
            "ec2instancetype": {
                "Description": "Instance type of ec2",
                "Type": "String",
                "AllowedValues": [
                    "t2.micro",
                    "t2.nano",
                    "t2.small"
                ],
                "Default": "t2.micro"
            },
            "amiid": {
                "Description": "Image Id",
                "Type": "String",
                "Default": "ami-0470e33cd681b2476"
            },
            "s3accesscontrol": {
                "Description": "S3 Access control",
                "Type": "String",
                "AllowedValues": [
                    "Private",
                    "PublicRead",
                    "PublicReadWrite",
                    "AuthenticatedRead",
                    "LogDeliveryWrite",
                    " BucketOwnerRead",
                    "BucketOwnerFullControl"
                ],
                "Default": "PublicRead"
            },
            "sgname": {
                "Description": "Security Groups",
                "Type": "AWS::EC2::SecurityGroup::GroupName"
            },
            "awskeypair": {
                "Description": "Keypair",
                "Type": "AWS::EC2::KeyPair::KeyName"
            }
        },
        "Resources": {
            "mys3": {
                "Type": "AWS::S3::Bucket",
                "Properties": {
                    "AccessControl": {
                        "Ref": "s3accesscontrol"
                    },
                    "BucketName": {
                        "Ref": "s3bucketname"
                    }
                }
            },
            "myec2": {
                "Type": "AWS::EC2::Instance",
                "Properties": {
                    "ImageId": {
                        "Ref": "amiid"
                    },
                    "InstanceType": {
                        "Ref": "ec2instancetype"
                    },
                    "SecurityGroups": [
                        {
                            "Ref": "sgname"
                        }
                    ],
                    "KeyName": {
                        "Ref": "awskeypair"
                    }
                }
            }
        }
    }
    
    Preview Preview
  • Novice Scenario [Create an Ec2 Instance in a new network (VPC)]:
    • Create a vpc
    • Add 3 subnets to vpc [Refer to previous classes screenshots]
    • Create and attach internet gateway to vpc
    • Create a Route table with route to internet gateway
    • Create subnet associations to new route table for 2 out 3 subnets
    • Create a Key value pair [manual] and security group [manual]
    • Create an ec2 instance in subnet1 and other ec2 instance in subnet2 [last step] Preview Preview

Leave a Reply

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

Please turn AdBlock off
Social Network Widget by Acurax Small Business Website Designers

Discover more from Direct DevOps from Quality Thought

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

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube