Create VPC and EC2

Explanation of the CloudFormation template

This template creates an EC2 instance within a VPC and allows access to the instance through AWS Systems Manager Session Manager. Below is a detailed description of the sections in the template:

  • AWSTemplateFormatVersion: The version of the template format.
  • Description: A brief description of the template.
  • Parameters: Defines parameters that can be used to customize the deployment.
  • InstanceType: A parameter that specifies the EC2 instance type. The default is t3.small, and only t3.small or t3.medium are allowed.
  • LatestAmiId: A parameter of type AWS::SSM::Parameter::Value<AWS::EC2::Image::Id> that automatically resolves the latest Amazon Linux 2023 AMI from the public parameter /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64 in SSM Parameter Store.
  • Resources: Defines the resources that the template creates.
  • VPC: Creates a VPC (Virtual Private Cloud).
  • Subnet: Creates a subnet within the VPC.
  • InternetGateway: Creates an Internet Gateway to connect the VPC to the Internet.
  • AttachGateway: Attaches the Internet Gateway to the VPC.
  • RouteTable: Creates a Route Table for the VPC.
  • Route: Adds a route in the Route Table to allow traffic to the Internet through the Internet Gateway.
  • SubnetRouteTableAssociation: Associates the subnet with the Route Table.
  • InstanceSecurityGroup: Creates a Security Group for the EC2 instance that allows HTTP on port 80.
  • RoleForEC2: Creates an IAM role with the managed policy AmazonSSMManagedInstanceCore so that the EC2 instance can be managed by Systems Manager.
  • InstanceProfile: Creates an Instance Profile to attach the IAM role to the EC2 instance.
  • WebServerInstance: Creates an EC2 instance using the specified parameters and associates it with the VPC, Subnet, Security Group, and IAM role created above.
  • This template creates a simple environment for deploying an EC2 instance within a VPC and accessing it through Session Manager. By deploying this template, you can create and configure an EC2 environment automatically and consistently.

Create resources with AWS CloudFormation

  1. In the VS Code terminal, view the vpc.yaml template:

    cd ~/CloudFormation-series/day-2
    cat vpc.yaml
    

AWS CloudFormation

  1. In the day-2 folder, deploy the stack with the following command:

    aws cloudformation create-stack \
     --stack-name cfn-vpc-demo \
     --template-body file://vpc.yaml \
     --parameters ParameterKey=InstanceType,ParameterValue=t3.medium \
     --capabilities CAPABILITY_NAMED_IAM
    

AWS CloudFormation

  1. Wait for the stack to be created, then check the CloudFormation console.
    • Check the Events tab:

      aws cloudformation wait stack-create-complete --stack-name cfn-vpc-demo
      

AWS CloudFormation

  1. Check the Resources tab to see the resources that have been created.

AWS CloudFormation

  1. Open the EC2 console, where you will see a newly created instance.
    • Select this instance.
    • Choose Connect.

AWS CloudFormation

  1. On the Connect to instance page, choose the Session Manager tab.
    • Choose Connect.

AWS CloudFormation

  1. A new session opens.
    • Try pinging a website to check Internet connectivity with the command:

      ping google.com -c5
      

Do not use ping amazon.com for this check, because the amazon.com server blocks ICMP, so ping always reports 100% packet loss even when the instance has Internet access.

AWS CloudFormation