PreLab/0.0-PreLab-DMSLab_instructor_CFN.yaml [15:210]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Resources:
  DMSLabCFNS3Bucket:
    Type: 'AWS::S3::Bucket'
  LambdaCopySourceRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          -
            Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - sts:AssumeRole
      Path: /
      Policies:
        - PolicyName: LambdaCopySourceRolePolicy
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action: s3:GetObject
                Resource: !Sub arn:aws:s3:::${SourceBucket}/*
              - Effect: Allow
                Action:
                  - s3:PutObject
                  - s3:DeleteObject
                Resource: !Sub arn:aws:s3:::${DMSLabCFNS3Bucket}/*
              - Effect: Allow
                Action: logs:CreateLogGroup
                Resource: !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:*
              - Effect: Allow
                Action:
                  - logs:CreateLogStream
                  - logs:PutLogEvents
                Resource: !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:*

  LambdaCopySource:
    Type: AWS::Lambda::Function
    Properties:
      Handler: index.handler
      Role: !GetAtt LambdaCopySourceRole.Arn
      Code:
        ZipFile: !Sub |
          import os
          import json
          import cfnresponse

          import boto3
          from botocore.exceptions import ClientError

          s3 = boto3.client('s3')

          import logging

          logger = logging.getLogger()
          logger.setLevel(logging.INFO)


          def handler(event, context):
              logger.info("Received event: %s" % json.dumps(event))
              source_bucket = '${SourceBucket}'
              source_prefix = '${SourceKey}'
              bucket = '${DMSLabCFNS3Bucket}'
              prefix = '${SourceKey}'

              result = cfnresponse.SUCCESS

              try:
                  if event['RequestType'] == 'Create' or event['RequestType'] == 'Update':
                      copy_source = {'Bucket': source_bucket, 'Key': source_prefix}
                      s3.copy(copy_source, bucket, prefix)
                  elif event['RequestType'] == 'Delete':
                      s3.delete_object(Bucket=bucket, Key=prefix)
              except ClientError as e:
                  logger.error('Error: %s', e)
                  result = cfnresponse.FAILED

              cfnresponse.send(event, context, result, {})

      Runtime: python3.8
      Timeout: 60

  CopySourceObject:
    Type: "Custom::CopySourceObject"
    Properties:
      ServiceToken: !GetAtt LambdaCopySource.Arn

  DMSInstructorVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
        -
          Key: Name
          Value: DMSLabSourceDB
  RDSSubNet:
    Type: AWS::EC2::Subnet
    Properties:
      CidrBlock: 10.0.1.0/24
      AvailabilityZone: !Select
        - 0
        - Fn::GetAZs: !Ref 'AWS::Region'
      VpcId: !Ref DMSInstructorVPC
      Tags:
        -
          Key: Name
          Value: DMSLabRDS1
  EC2SubNet:
    Type: AWS::EC2::Subnet
    Properties:
      CidrBlock: 10.0.0.0/24
      AvailabilityZone: !Select
        - 0
        - Fn::GetAZs: !Ref 'AWS::Region'
      VpcId: !Ref DMSInstructorVPC
      Tags:
        -
          Key: Name
          Value: DMSLabEC2
  RDSSubNet2:
    Type: AWS::EC2::Subnet
    Properties:
      CidrBlock: 10.0.2.0/24
      AvailabilityZone: !Select
        - 1
        - Fn::GetAZs: !Ref 'AWS::Region'
      VpcId: !Ref DMSInstructorVPC
      Tags:
        -
          Key: Name
          Value: DMSLabRDS2
  DMSLabIGW:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        -
          Key: Name
          Value: DMSLabIGW
  DMSLabDHCPOptions:
    Type: AWS::EC2::DHCPOptions
    Properties:
      DomainName: ec2.internal
      DomainNameServers:
        - AmazonProvidedDNS
  DMSLabRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref DMSInstructorVPC
      Tags:
        -
          Key: Name
          Value: DMSLabRT
  DMSLabInstance:
    Type: AWS::EC2::Instance
    Properties:
      DisableApiTermination: false
      InstanceInitiatedShutdownBehavior: stop
      EbsOptimized: true
      ImageId: !Ref LatestAmiId
      InstanceType: t3.2xlarge
      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 -xe
          yum install -y postgresql
          yum install -y git
          yum update -y
          cd /home/ec2-user
          DIR="aws-database-migration-samples"
          if [ ! -d "$DIR" ]; then
              git clone https://github.com/aws-samples/aws-database-migration-samples.git
          fi
          cd aws-database-migration-samples/PostgreSQL/sampledb/v1/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



PreLab/central-template-environment-setup.yaml [53:244]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Resources:
  DMSLabCFNS3Bucket:
    Type: 'AWS::S3::Bucket'
  LambdaCopySourceRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          -
            Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - sts:AssumeRole
      Path: /
      Policies:
        - PolicyName: LambdaCopySourceRolePolicy
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action: s3:GetObject
                Resource: !Sub arn:aws:s3:::${SourceBucket}/*
              - Effect: Allow
                Action:
                  - s3:PutObject
                  - s3:DeleteObject
                Resource: !Sub arn:aws:s3:::${DMSLabCFNS3Bucket}/*
              - Effect: Allow
                Action: logs:CreateLogGroup
                Resource: !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:*
              - Effect: Allow
                Action:
                  - logs:CreateLogStream
                  - logs:PutLogEvents
                Resource: !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:*
  LambdaCopySource:
    Type: AWS::Lambda::Function
    Properties:
      Handler: index.handler
      Role: !GetAtt LambdaCopySourceRole.Arn
      Code:
        ZipFile: !Sub |
          import os
          import json
          import cfnresponse

          import boto3
          from botocore.exceptions import ClientError

          s3 = boto3.client('s3')

          import logging

          logger = logging.getLogger()
          logger.setLevel(logging.INFO)


          def handler(event, context):
              logger.info("Received event: %s" % json.dumps(event))
              source_bucket = '${SourceBucket}'
              source_prefix = '${SourceKey}'
              bucket = '${DMSLabCFNS3Bucket}'
              prefix = '${SourceKey}'

              result = cfnresponse.SUCCESS

              try:
                  if event['RequestType'] == 'Create' or event['RequestType'] == 'Update':
                      copy_source = {'Bucket': source_bucket, 'Key': source_prefix}
                      s3.copy(copy_source, bucket, prefix)
                  elif event['RequestType'] == 'Delete':
                      s3.delete_object(Bucket=bucket, Key=prefix)
              except ClientError as e:
                  logger.error('Error: %s', e)
                  result = cfnresponse.FAILED

              cfnresponse.send(event, context, result, {})
      Runtime: python3.8
      Timeout: 60
  CopySourceObject:
    Type: "Custom::CopySourceObject"
    Properties:
      ServiceToken: !GetAtt LambdaCopySource.Arn
  DMSInstructorVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
        -
          Key: Name
          Value: DMSLabSourceDB
  RDSSubNet:
    Type: AWS::EC2::Subnet
    Properties:
      CidrBlock: 10.0.1.0/24
      AvailabilityZone: !Select
        - 0
        - Fn::GetAZs: !Ref 'AWS::Region'
      VpcId: !Ref DMSInstructorVPC
      Tags:
        -
          Key: Name
          Value: DMSLabRDS1
  EC2SubNet:
    Type: AWS::EC2::Subnet
    Properties:
      CidrBlock: 10.0.0.0/24
      AvailabilityZone: !Select
        - 0
        - Fn::GetAZs: !Ref 'AWS::Region'
      VpcId: !Ref DMSInstructorVPC
      Tags:
        -
          Key: Name
          Value: DMSLabEC2
  RDSSubNet2:
    Type: AWS::EC2::Subnet
    Properties:
      CidrBlock: 10.0.2.0/24
      AvailabilityZone: !Select
        - 1
        - Fn::GetAZs: !Ref 'AWS::Region'
      VpcId: !Ref DMSInstructorVPC
      Tags:
        -
          Key: Name
          Value: DMSLabRDS2
  DMSLabIGW:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        -
          Key: Name
          Value: DMSLabIGW
  DMSLabDHCPOptions:
    Type: AWS::EC2::DHCPOptions
    Properties:
      DomainName: ec2.internal
      DomainNameServers:
        - AmazonProvidedDNS
  DMSLabRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref DMSInstructorVPC
      Tags:
        -
          Key: Name
          Value: DMSLabRT
  DMSLabInstance:
    Type: AWS::EC2::Instance
    Properties:
      DisableApiTermination: false
      InstanceInitiatedShutdownBehavior: stop
      EbsOptimized: true
      ImageId: !Ref LatestAmiId
      InstanceType: t3.2xlarge
      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 -xe
          yum install -y postgresql
          yum install -y git
          yum update -y
          cd /home/ec2-user
          DIR="aws-database-migration-samples"
          if [ ! -d "$DIR" ]; then
              git clone https://github.com/aws-samples/aws-database-migration-samples.git
          fi
          cd aws-database-migration-samples/PostgreSQL/sampledb/v1/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



