service: 'click-staging-ec2'

provider:
  name: aws
  runtime: python3.7
  profile: 'click-test-cpt'
  stackName: 'click-staging-ec2'
  region: 'af-south-1'
  stage: ${self:provider.profile}
  deploymentBucket:
    name: ${self:custom.ServerlessBucketName}
    serverSideEncryption: AES256
    blockPublicAccess: true

plugins:
  - serverless-parameters
custom:
  CustomerName: click
  StageName: staging
  # Combination of profile and function name used as bucket name:
  ResourceName: serverless-framework # Only lowercase letters and numbers allowed. Used to populate bucket name.
  ServerlessBucketName: '${self:custom.ResourceName}-${self:provider.stage}-${self:provider.region}'

  # CloudFormation parameters templates
  parameters:
    paramCustomerName:
      Description: Enter the Customer Name, used to tag everything
      Type: String
      Default: '${self:custom.CustomerName}'

    ## EC2
    InstanceType:
      Description: WebServer EC2 instance type
      Type: String
      Default: t3.micro
      AllowedValues:
        - t3.micro
      ConstraintDescription: must be a valid EC2 instance type.

    KeyName:
      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.
      Default: tcf-test-ec2-key-01

    AmazonImageID:
      Description: The ID of the Amazon machine image (AMI) - Windows Server 2019
      Type: String
      Default: ami-080467c693d304c8e

resources:

  # CloudFormation resource templates

  #Conditions:

  Resources:

  ## EC2

    # Create a managed policy for S3 access

    # S3AccessPolicy:
    #   Type: AWS::IAM::ManagedPolicy
    #   Properties:
    #     Description: Policy for S3 access
    #     Path: /
    #     PolicyDocument:
    #       Version: "2012-10-17"
    #       Statement:
    #         - Effect: Allow
    #           Action: 's3:ListAllMyBuckets'
    #           Resource: '*'
    #         - Effect: Allow
    #           Action: 's3:ListBucket'
    #           Resource: 'arn:aws:s3:::*'
    #         - Effect: Allow
    #           Action: 's3:GetObject'
    #           Resource: 'arn:aws:s3:::*/*'
    #         - Effect: Allow
    #           Action: 's3:PutObject'
    #           Resource: 'arn:aws:s3:::*/*'


    # Create role for EC2 instance and attach policies

    EC2AccessRole:
      Type: AWS::IAM::Role
      Properties:
        AssumeRolePolicyDocument:
          Version: "2012-10-17"
          Statement:
            - Effect: "Allow"
              Principal:
                Service:
                  - "ec2.amazonaws.com"
              Action:
                - "sts:AssumeRole"
        Path: "/"
        ManagedPolicyArns:
          # - !Ref S3AccessPolicy
          - arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore

    # Create EC2 Instance Profile and attach the role

    EC2InstanceProfile:
      Type: AWS::IAM::InstanceProfile
      Properties:
        InstanceProfileName: '${self:custom.ResourceName}-${self:provider.stage}-ec2-instance-profile'
        Path: /
        Roles:
          - !Ref EC2AccessRole

    # Create Security Group for the EC2

    EC2SecurityGroup:
      Type: 'AWS::EC2::SecurityGroup'
      Properties:
        GroupDescription: Enable SSH access (default)
        # SecurityGroupIngress:
        #   - IpProtocol: tcp
        #     FromPort: 22
        #     ToPort: 22
        #     CidrIp: 0.0.0.0/0
#            SourceSecurityGroupId:
        VpcId:
          Fn::ImportValue: !Sub '${self:custom.CustomerName}-${self:custom.StageName}-VPC'
        Tags:
          - Key: Name
            Value: !Join
              - '-'
              - - '${self:custom.CustomerName}'
                - '${self:custom.StageName}'
                - 'EC2'
                - 'SecurityGroup'

    # Create KMS EC2 encryption key

    ec2Key:
      Type: AWS::KMS::Key
      Properties:
        KeyPolicy:
          Version: '2012-10-17'
          Id: key-ec2
          Statement:
            - Sid: 'Enable IAM User Permissions'
              Effect: 'Allow'
              Principal:
                AWS: !Join
                  - ''
                  - - 'arn:aws:iam::'
                    - !Ref 'AWS::AccountId'
                    - ':root'
              Action: 'kms:*'
              Resource: '*'

    # KMS RDS encryption key alias

    ec2Alias:
      Type: AWS::KMS::Alias
      Properties:
        AliasName: alias/ec2
        TargetKeyId:
          Ref: ec2Key

    # Create EC2 instance

    EC2Instance1:
      Type: AWS::EC2::Instance
      Properties:
        InstanceType: !Ref InstanceType
        SubnetId:
          Fn::ImportValue: !Sub '${self:custom.CustomerName}-${self:custom.StageName}-PrivateSubnetA1'
        SecurityGroupIds:
          - !Ref EC2SecurityGroup
        IamInstanceProfile: !Ref EC2InstanceProfile
        KeyName: !Ref KeyName
        ImageId: !Ref AmazonImageID
        "BlockDeviceMappings": [
          {
            "DeviceName": "/dev/sda1",
            "Ebs": {
              "VolumeType": "gp3",
              "VolumeSize": "30",
              "Iops": "3000",
              "DeleteOnTermination": "true",
              "Encrypted": "true",
              "KmsKeyId": !Ref ec2Key
            }
          }
        ]
        Tags:
          - Key: Name
            Value: !Join
              - '-'
              - - '${self:custom.CustomerName}'
                - '${self:custom.StageName}'
                - 'EC2Instance'
                - '1'
          - Key: backup
            Value: daily

  ## Outputs

#-----------------------------------------------------------------------#
