AWSTemplateFormatVersion: 2010-09-09
Description: >-
  AWS CloudFormation Sample Template to create lab resources. Creates an EC2
  Instance in a new VPC.

  **WARNING** You will be billed for the AWS resources created if you create a
  stack from this template.

  Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.

  Licensed under the Apache License, Version 2.0 (the "License"). You may not
  use this file except in compliance with the License. A copy of the License is
  located at

      https://www.apache.org/licenses/LICENSE-2.0

  or in the "license" file accompanying this file. This file is distributed  on
  an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  or implied. See the License for the specific language governing permissions
  and limitations under the License.
Parameters:
  AvailabilityZone:
    Type: 'AWS::EC2::AvailabilityZone::Name'
    Description: The Availability Zone in which resources are launched.
  LatestAmiId:
    Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
    Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
Resources:
  VPC:
    Type: 'AWS::EC2::VPC'
    Properties:
      CidrBlock: 10.0.0.0/16
      Tags:
        - Key: Name
          Value: WA-Lab-VPC
  Subnet:
    Type: 'AWS::EC2::Subnet'
    Properties:
      AvailabilityZone: !Ref AvailabilityZone
      CidrBlock: 10.0.0.0/24
      VpcId: !Ref VPC
      MapPublicIpOnLaunch: 'true'
      Tags:
        - Key: Name
          Value: WA-Lab-Subnet
  InternetGateway:
    Type: 'AWS::EC2::InternetGateway'
    Properties:
      Tags:
        - Key: Name
          Value: WA-Lab-InternetGateway
  VPCGatewayAttachment:
    Type: 'AWS::EC2::VPCGatewayAttachment'
    Properties:
      InternetGatewayId: !Ref InternetGateway
      VpcId: !Ref VPC
  RouteTable:
    Type: 'AWS::EC2::RouteTable'
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: WA-Lab-RouteTable
  Route:
    Type: 'AWS::EC2::Route'
    DependsOn: VPCGatewayAttachment
    Properties:
      RouteTableId: !Ref RouteTable
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref InternetGateway
  RouteTableAssociation:
    Type: 'AWS::EC2::SubnetRouteTableAssociation'
    Properties:
      RouteTableId: !Ref RouteTable
      SubnetId: !Ref Subnet
  InstanceSecurityGroup:
    Type: 'AWS::EC2::SecurityGroup'
    Properties:
      GroupDescription: Inbound HTTP access
      SecurityGroupIngress:
        - CidrIp: 0.0.0.0/0
          FromPort: '80'
          IpProtocol: tcp
          ToPort: '80'
      SecurityGroupEgress:
        - CidrIp: 0.0.0.0/0
          IpProtocol: '-1'
      VpcId: !Ref VPC
  Instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      ImageId: !Ref LatestAmiId
      InstanceType: t2.micro
      SubnetId: !Ref Subnet
      SecurityGroupIds:
        - !Ref InstanceSecurityGroup
      UserData: !Base64
        'Fn::Join':
          - ''
          - - |
              #!/bin/bash -x
            - |
              yum install httpd -y
            - >
              echo '<h1>AWS Backup is Awesome!!</h1>' >>
              /var/www/html/index.html
            - |
              systemctl start httpd.service
            - systemctl enable httpd.service
      Tags:
        - Key: Name
          Value: WA-Lab-Instance
        - Key: workload
          Value: myapp
Outputs:
  InstanceID:
    Description: EC2 Instance ID
    Value: !Ref Instance
  ApplicationURL:
    Description: URL for the application running on the EC2 Instance
    Value: !Join
      - ''
      - - 'http://'
        - !GetAtt
          - Instance
          - PublicIp
