AWSTemplateFormatVersion: 2010-09-09

Description: Apache App Server Install

Parameters:

  SSHKeyName:
    Description: Name of an existing EC2 KeyPair to enable SSH access to the instance
    Type: 'AWS::EC2::KeyPair::KeyName'
    ConstraintDescription: must be the name of an existing EC2 KeyPair.

  InstanceType:
    Description: WebServer EC2 instance type
    Type: String
    Default: t4g.medium
    AllowedValues:
      - t4g.small
      - t4g.medium
      - t4g.large
    ConstraintDescription: must be a valid EC2 instance type.

  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.small:
      Arch: HVM64
    t4g.medium:
      Arch: HVM64
    t4g.large:
      Arch: HVM64
  AWSRegionArch2AMI:
    us-east-1:
      HVM64: ami-0f48d15c9efb5f63d
    us-west-2:
      HVM64: ami-0f48d15c9efb5f63d
    us-west-1:
      HVM64: ami-0f48d15c9efb5f63d
    us-east-2:
      HVM64: ami-0f48d15c9efb5f63d

Resources:
  WebServerInstance:
    Type: 'AWS::EC2::Instance'
    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 'apache status monitor to cloudwatch logs will be up and running soon'
            - |
              curl -o- https://apache-app-server-cw-00.s3.amazonaws.com/install-apache-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:
  WebsiteURL:
    Description: nodejs express monitor apache url
    Value: !Join 
      - ''
      - - 'http://'
        - !GetAtt 
          - WebServerInstance
          - PublicDnsName