AWSTemplateFormatVersion: 2010-09-09

Description: Nginx Web App Server - Express App Monitor - CloudWatch

Parameters:

  SSHKeyName:
    Description: EC2 KeyPair for SSH
    Type: 'AWS::EC2::KeyPair::KeyName'

  InstanceType:
    Description: Nginx Web App Server - EC2 Instance
    Type: String
    Default: t4g.medium
    AllowedValues:
      - t4g.micro
      - t4g.small
      - t4g.medium
      - t4g.large

  SSHLocation:
    Description: 'The IP address range that can be used to SSH to the EC2 instances'
    Type: String
    MinLength: '9'
    MaxLength: '18'
    Default: 0.0.0.0/0
    AllowedPattern: '(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})'
    ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.

Mappings:
  AWSInstanceType2Arch:
    t4g.micro:
      Arch: HVM64
    t4g.small:
      Arch: HVM64
    t4g.medium:
      Arch: HVM64
    t4g.large:
      Arch: HVM64
  AWSRegionArch2AMI:
    us-east-1:
      HVM64: ami-0f48d15c9efb5f63d
    us-east-2:
      HVM64: ami-0f48d15c9efb5f63d
    us-west-1:
      HVM64: ami-0f48d15c9efb5f63d
    us-west-2:
      HVM64: ami-0f48d15c9efb5f63d

Resources:
  NginxWebServerInstance:
    Type: 'AWS::EC2::Instance'
    Metadata:
      AWS::CloudFormation::Init:
        configSets:
          InstallAndRun:
            - Install
        Install:
          packages:
            yum:
              nginx: []
          services:
            sysvinit:
              nginx:
                enabled: 'true'
                ensureRunning: 'true'
    Properties:
      ImageId: !FindInMap
        - AWSRegionArch2AMI
        - !Ref 'AWS::Region'
        - !FindInMap
          - AWSInstanceType2Arch
          - !Ref InstanceType
          - Arch
      InstanceType: !Ref InstanceType
      IamInstanceProfile: CloudWatchAgentServerRole
      SecurityGroups:
        - !Ref WebServerSecurityGroup
      KeyName: !Ref SSHKeyName
      UserData: !Base64
        'Fn::Join':
          - ''
          - - |
              #!/bin/bash -xe
            - |
              echo 'Nginx status monitor web application to CloudWatch logs will be up and running soon!'
            - |
              curl -o- https://nginx-app-server-cw-00.s3.us-west-2.amazonaws.com/install-nginx-node-status-monitor.sh | bash
            - |+

  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
        - IpProtocol: tcp
          FromPort: '22'
          ToPort: '22'
          CidrIp: !Ref SSHLocation

Outputs:
  NginxAppURL:
    Description: nginx app server url
    Value: !Join
      - ''
      - - 'http://'
        - !GetAtt
          - NginxWebServerInstance
          - PublicDnsName