Создание VPC с помощью скрипта CloudFormation.

Всем привет, сегодня с помощью скрипта CloudFormation создадим EC2 Inctance.

    AWSTemplateFormatVersion: 2010-09-09
    Description: >-
      AWS CloudFormation Sample Template: Sample template that can be used to  test EC2 updates. 
      **WARNING** This template creates an Amazon Ec2 Instance. 
      You will be  billed for the AWS resources used if you create a stack from this template.
    Parameters:
      KeyName:
        Description: Name of an existing EC2 KeyPair to enable SSH access to the instance
        Type: String
        Default: prod_key
        AllowedValues:
            - prod_key
            - dev_key
      InstanceType:
        Description: WebServer EC2 instance type
        Type: String
        Default: t2.nano
        AllowedValues:
          - t1.micro
          - t2.nano
          - t2.micro
          - t2.small
          - t2.medium
          - t2.large
          - m1.small
          - m1.medium
          - m1.large
          - m1.xlarge
          - m2.xlarge
          - m2.2xlarge
          - m2.4xlarge
          - m3.medium
          - m3.large
          - m3.xlarge
          - m3.2xlarge
          - m4.large
          - m4.xlarge
          - m4.2xlarge
          - m4.4xlarge
          - m4.10xlarge
          - c1.medium
          - c1.xlarge
          - c3.large
          - c3.xlarge
          - c3.2xlarge
          - c3.4xlarge
          - c3.8xlarge
          - c4.large
          - c4.xlarge
          - c4.2xlarge
          - c4.4xlarge
          - c4.8xlarge
          - g2.2xlarge
          - g2.8xlarge
          - r3.large
          - r3.xlarge
          - r3.2xlarge
          - r3.4xlarge
          - r3.8xlarge
          - i2.xlarge
          - i2.2xlarge
          - i2.4xlarge
          - i2.8xlarge
          - d2.xlarge
          - d2.2xlarge
          - d2.4xlarge
          - d2.8xlarge
          - hi1.4xlarge
          - hs1.8xlarge
          - cr1.8xlarge
          - cc2.8xlarge
          - cg1.4xlarge
        ConstraintDescription: must be a valid EC2 instance type.
    Mappings:
      AWSInstanceType2Arch:
        t1.micro:
          Arch: HVM64
        t2.nano:
          Arch: HVM64
        t2.micro:
          Arch: HVM64
        t2.small:
          Arch: HVM64
        t2.medium:
          Arch: HVM64
        t2.large:
          Arch: HVM64
        m1.small:
          Arch: HVM64
        m1.medium:
          Arch: HVM64
        m1.large:
          Arch: HVM64
        m1.xlarge:
          Arch: HVM64
        m2.xlarge:
          Arch: HVM64
        m2.2xlarge:
          Arch: HVM64
        m2.4xlarge:
          Arch: HVM64
        m3.medium:
          Arch: HVM64
        m3.large:
          Arch: HVM64
        m3.xlarge:
          Arch: HVM64
        m3.2xlarge:
          Arch: HVM64
        m4.large:
          Arch: HVM64
        m4.xlarge:
          Arch: HVM64
        m4.2xlarge:
          Arch: HVM64
        m4.4xlarge:
          Arch: HVM64
        m4.10xlarge:
          Arch: HVM64
        c1.medium:
          Arch: HVM64
        c1.xlarge:
          Arch: HVM64
        c3.large:
          Arch: HVM64
        c3.xlarge:
          Arch: HVM64
        c3.2xlarge:
          Arch: HVM64
        c3.4xlarge:
          Arch: HVM64
        c3.8xlarge:
          Arch: HVM64
        c4.large:
          Arch: HVM64
        c4.xlarge:
          Arch: HVM64
        c4.2xlarge:
          Arch: HVM64
        c4.4xlarge:
          Arch: HVM64
        c4.8xlarge:
          Arch: HVM64
        g2.2xlarge:
          Arch: HVMG2
        g2.8xlarge:
          Arch: HVMG2
        r3.large:
          Arch: HVM64
        r3.xlarge:
          Arch: HVM64
        r3.2xlarge:
          Arch: HVM64
        r3.4xlarge:
          Arch: HVM64
        r3.8xlarge:
          Arch: HVM64
        i2.xlarge:
          Arch: HVM64
        i2.2xlarge:
          Arch: HVM64
        i2.4xlarge:
          Arch: HVM64
        i2.8xlarge:
          Arch: HVM64
        d2.xlarge:
          Arch: HVM64
        d2.2xlarge:
          Arch: HVM64
        d2.4xlarge:
          Arch: HVM64
        d2.8xlarge:
          Arch: HVM64
        hi1.4xlarge:
          Arch: HVM64
        hs1.8xlarge:
          Arch: HVM64
        cr1.8xlarge:
          Arch: HVM64
        cc2.8xlarge:
          Arch: HVM64
      AWSRegionArch2AMI:
        us-east-1:
          HVM64: ami-0ff8a91507f77f867
          HVMG2: ami-0a584ac55a7631c0c
        us-west-2:
          HVM64: ami-a0cfeed8
          HVMG2: ami-0e09505bc235aa82d
        us-west-1:
          HVM64: ami-0bdb828fd58c52235
          HVMG2: ami-066ee5fd4a9ef77f1
        eu-west-1:
          HVM64: ami-047bb4163c506cd98
          HVMG2: ami-0a7c483d527806435
        eu-west-2:
          HVM64: ami-f976839e
          HVMG2: NOT_SUPPORTED
        eu-west-3:
          HVM64: ami-0ebc281c20e89ba4b
          HVMG2: NOT_SUPPORTED
        eu-central-1:
          HVM64: ami-0c115dbd34c69a004
          HVMG2: ami-06223d46a6d0661c7
        ap-northeast-1:
          HVM64: ami-06cd52961ce9f0d85
          HVMG2: ami-053cdd503598e4a9d
        ap-northeast-2:
          HVM64: ami-0a10b2721688ce9d2
          HVMG2: NOT_SUPPORTED
        ap-northeast-3:
          HVM64: ami-0d98120a9fb693f07
          HVMG2: NOT_SUPPORTED
        ap-southeast-1:
          HVM64: ami-08569b978cc4dfa10
          HVMG2: ami-0be9df32ae9f92309
        ap-southeast-2:
          HVM64: ami-09b42976632b27e9b
          HVMG2: ami-0a9ce9fecc3d1daf8
        ap-south-1:
          HVM64: ami-0912f71e06545ad88
          HVMG2: ami-097b15e89dbdcfcf4
        us-east-2:
          HVM64: ami-0b59bfac6be064b78
          HVMG2: NOT_SUPPORTED
        ca-central-1:
          HVM64: ami-0b18956f
          HVMG2: NOT_SUPPORTED
        sa-east-1:
          HVM64: ami-07b14488da8ea02a0
          HVMG2: NOT_SUPPORTED
        cn-north-1:
          HVM64: ami-0a4eaf6c4454eda75
          HVMG2: NOT_SUPPORTED
        cn-northwest-1:
          HVM64: ami-6b6a7d09
          HVMG2: NOT_SUPPORTED
    Resources:
      WebServerInstance:
        Type: 'AWS::EC2::Instance'
        Metadata:
          Comment: Install a simple PHP application
          'AWS::CloudFormation::Init':
            configSets:
              app_install:
              - enable_extras
              - install_apps
              - configure
              - run_apps
            enable_extras:
              commands:
              enable_with_extra_packages_php:
                  command: amazon-linux-extras enable php7.4
                  cwd    : "/root"
                  DependsOn: "/root"
              enable_with_extra_packages_nginx:
                  command: amazon-linux-extras enable nginx1
                  cwd    : "/root"
                  DependsOn: "/root"
            configure:  
              users:
                mkgeka:
                  groups:
                    - "wheel"
                  uid: "1010"
                  homeDir: "/home/mkgeka2/"
              files:
                /root/install.sh:
                  content: !Sub | 
                    #!/bin/bash -xe
                    touch 321.txt
                    touch 1234.txt
                    ifconfig > if.txt
                    id > id.txt
                  mode: '000644'
                  owner: root
                  group: root
                /etc/cfn/cfn-hup.conf:
                  content: !Sub |
                    [main]
                    stack= ${AWS::StackId}
                    region=${AWS::Region}
                    interval=5
                  group: root
                  mode: '000400'
                  owner: root
                /etc/cfn/hooks.d/cfn-auto-reloader.conf:
                  content: !Sub |
                    [cfn-auto-reloader-hook]
                    triggers=post.update
                    path=Resources.WebServerInstance.Metadata.AWS::CloudFormation::Init
                    action=/opt/aws/bin/cfn-init -v --stack ${AWS::StackId} --resource WebServerInstance --configsets app_install --region ${AWS::Region}
                  group: root
                  mode: '000400'
                  owner: root
                /etc/environment:
                  content: !Sub |
                    LANG=en_US.utf-8
                    LC_ALL=en_US.utf-8
                  group: root
                  mode: '006440'
                  owner: root
              commands:
              update_os:
                  command: "yum update -y"
                  cwd    : "/root/"
              run_file:
                  command: "bash -x install.sh"
                  cwd    : "/root/"
            install_apps:
              packages:
                yum:
                  httpd             : []
                  php               : []
                  php-mysqlnd         : []
                  mariadb-server      : []
                  mariadb-libs        : []
                  mariadb             : []
                  nginx             : []
                  nginx-all-modules : []
                  htop              : []
            run_apps:      
              services:
                sysvinit:
                  mariadb:
                    enabled: 'true'
                    ensureRunning: 'true'
                  httpd:
                    enabled: 'true'
                    ensureRunning: 'true'
                  cfn-hup:
                    enabled: 'true'
                    ensureRunning: 'true'
                    files:
                      - /etc/cfn/cfn-hup.conf
                      - /etc/cfn/hooks.d/cfn-auto-reloader.conf
        Properties:
          KeyName: !Ref KeyName
          ImageId: !FindInMap [AWSRegionArch2AMI, !Ref 'AWS::Region', !FindInMap [AWSInstanceType2Arch, !Ref InstanceType, Arch]]
          InstanceType: !Ref InstanceType
          SecurityGroups:
            - !Ref WebServerSecurityGroup
            - !Ref SSHSecurityGroup
          UserData:
            Fn::Base64: !Sub |
              #!/bin/bash -xe

              yum update -y aws-cfn-bootstrap

              # Install the files and packages from the metadata
              /opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource WebServerInstance --configsets app_install --region ${AWS::Region}
                
              # Start up the cfn-hup daemon to listen for changes to the Web Server metadata           
              /opt/aws/bin/cfn-hup || error_exit 'Failed to start cfn-hup' 
              
              # Signal the status from cfn-init
              /opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource WebServerInstance --region ${AWS::Region}

        CreationPolicy:
          ResourceSignal:
            Timeout: PT5M
      WebServerSecurityGroup:
        Type: 'AWS::EC2::SecurityGroup'
        Properties:
          GroupDescription: Enable HTTP access via port 80
          SecurityGroupIngress:
            - IpProtocol: tcp
              FromPort: '80'
              ToPort: '80'
              CidrIp: 0.0.0.0/0
      SSHSecurityGroup:
        Type: 'AWS::EC2::SecurityGroup'
        Properties:
          GroupDescription: Enable SSH access via port 22
          SecurityGroupIngress:
            - IpProtocol: tcp
              FromPort: '22'
              ToPort: '22'
              CidrIp: 0.0.0.0/0
    Outputs:
      WebsiteURL:
        Description: Application URL
        Value: !Sub "http://${WebServerInstance.PublicDnsName}"