Description: Kopiax CloudFormation for Marketplace 

Metadata: 
  AWS::CloudFormation::Interface: 
    ParameterGroups: 
      - 
        Label: 
          default: "Network Configuration"
        Parameters: 
          - VpcId
          - SubnetId
          - IpAddress
      - 
        Label: 
          default: "Tag"
        Parameters: 
          - owner 

Parameters:
  VpcId:
    Type: AWS::EC2::VPC::Id 
  SubnetId:
    Type: AWS::EC2::Subnet::Id 
  IpAddress:
    Type: String
    Description: Your own IP Address to allow access to Kopiax instance
    AllowedPattern: (\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})
    ConstraintDescription: must be a valid IP address of the form x.x.x.x. 
  owner:
    Type: String 
    Default: kopiax
    Description: Tagging your resources for Kopiax

Mappings: 
  RegionMap: 
    us-east-1: 
      ImageId: ami-082097bf2efdc3777
    us-west-1: 
      ImageId: ami-xxxxxxxxxxxxxxxxx
    eu-west-1: 
      ImageId: ami-xxxxxxxxxxxxxxxxx
    ap-southeast-1: 
      ImageId: ami-xxxxxxxxxxxxxxxxx
    us-east-2: 
      ImageId: ami-xxxxxxxxxxxxxxxxx
    us-west-2: 
      ImageId: ami-xxxxxxxxxxxxxxxxx
    af-south-1: 
      ImageId: ami-xxxxxxxxxxxxxxxxx
    ap-east-1: 
      ImageId: ami-xxxxxxxxxxxxxxxxx  
    ap-southeast-3: 
      ImageId: ami-xxxxxxxxxxxxxxxxx
    ap-south-1: 
      ImageId: ami-xxxxxxxxxxxxxxxxx
    ap-northeast-3: 
      ImageId: ami-xxxxxxxxxxxxxxxxx
    ap-northeast-2: 
      ImageId: ami-xxxxxxxxxxxxxxxxx 
    ap-southeast-2: 
      ImageId: ami-xxxxxxxxxxxxxxxxx
    ap-northeast-1: 
      ImageId: ami-xxxxxxxxxxxxxxxxx
    ca-central-1: 
      ImageId: ami-xxxxxxxxxxxxxxxxx
    eu-central-1: 
      ImageId: ami-xxxxxxxxxxxxxxxxx 
    eu-west-2:
      ImageId: ami-xxxxxxxxxxxxxxxxx 
    eu-south-1:
      ImageId: ami-xxxxxxxxxxxxxxxxx   
Resources:
  IAMProfile:
    Type: AWS::IAM::InstanceProfile
    DependsOn:
      - IAMRole
    Properties: 
      Roles:
        - !Ref IAMRole 
  IAMRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument: 
        Statement:
          - Effect: Allow
            Action: sts:AssumeRole
            Principal:
              Service:
                - ec2.amazonaws.com
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/AmazonEC2ReadOnlyAccess
      Tags:
        - Key: owner
          Value: !Ref owner
  SecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties: 
      GroupDescription: Security Group for the Kopiax  
      VpcId: !Ref VpcId
      SecurityGroupIngress:
      - IpProtocol: -1
        CidrIp: !Sub '${IpAddress}/32'
      Tags:
        - Key: owner
          Value: !Ref owner
  ElasticIP:
    Type: AWS::EC2::EIP
    Properties:
      Domain: vpc
      InstanceId: !Ref EC2Instance
      Tags:
        - Key: owner
          Value: !Ref owner
  EC2Instance:
    Type: AWS::EC2::Instance
    DependsOn:
      - SecurityGroup 
      - IAMProfile
    Properties:
      ImageId: !FindInMap
        - RegionMap
        - !Ref 'AWS::Region'
        - ImageId
      InstanceType: "t2.micro"
      Tenancy: "default"
      SubnetId: !Ref SubnetId
      EbsOptimized: false
      SecurityGroupIds: 
        - !Ref SecurityGroup
      SourceDestCheck: true
      BlockDeviceMappings: 
        - DeviceName: "/dev/xvda"
          Ebs: 
            Encrypted: false
            VolumeSize: 10
            VolumeType: "gp3"
            DeleteOnTermination: true
      UserData: 
        Fn::Base64: 
          !Sub |
            Content-Type: multipart/mixed; boundary="//"
            MIME-Version: 1.0

            --//
            Content-Type: text/cloud-config; charset="us-ascii"
            MIME-Version: 1.0
            Content-Transfer-Encoding: 7bit
            Content-Disposition: attachment; filename="cloud-config.txt"

            #cloud-config
            cloud_final_modules:
            - [scripts-user, always]

            --//
            Content-Type: text/x-shellscript; charset="us-ascii"
            MIME-Version: 1.0
            Content-Transfer-Encoding: 7bit
            Content-Disposition: attachment; filename="userdata.txt"

            #!/bin/bash
            cd /home/ec2-user/kopiax
            pm2 start server.js
      Tags:
        - Key: owner
          Value: !Ref owner
        - Key: Name
          Value: !Ref owner

Outputs:
  KopiaxPublicDNS:
    Value: !Sub "${EC2Instance.PublicDnsName}:8080"
    Description: Kopiax Public DNS address
