# AWS DocumentDB elastic cluster migration demo
# Initialization version on July 5, 2023 by chuanjin@
AWSTemplateFormatVersion: 2010-09-09
Description: 
  CloudFormation Template to deploy base resources for Amazon EC2
 
Parameters: 
  VPC: 
    Description: Please select the same VPC with DocumentDB
    Type: AWS::EC2::VPC::Id
  Subnet:
    Description: Please select a subnet
    Type: AWS::EC2::Subnet::Id
  AZ:
    Description: Please select the same Available Zone with DocumentDB
    Type: AWS::EC2::AvailabilityZone::Name

Resources: 
  # Add Security Group
  IngressSecurityGroup: 
    Type: AWS::EC2::SecurityGroup 
    Properties: 
      GroupName: "ingress-sg-for-docdb-test"
      GroupDescription: "Security group with ingress rule" 
      VpcId: !Ref VPC 
      SecurityGroupIngress:
      - IpProtocol: tcp
        FromPort: 22
        ToPort: 22
        CidrIp: 0.0.0.0/0
        Description: "Allows SSH access to EC2 Instance"

  NewKeyPair:
    Type: AWS::EC2::KeyPair
    Properties:
      KeyName: NewKeyPair
      Tags: 
        - Key: Name 
          Value: !Sub ${AWS::StackName} Key Pair 

  Ec2Instance: 
    Type: AWS::EC2::Instance
    Properties: 
      InstanceType: m5.2xlarge
      ImageId: !FindInMap 
        - AWSInstanceType2Arch
        - m5
        - AMI
      KeyName: !Ref NewKeyPair
      NetworkInterfaces: 
        - AssociatePublicIpAddress: "true"
          DeviceIndex: "0"
          GroupSet: 
            - !Ref IngressSecurityGroup
          SubnetId: !Ref Subnet
      BlockDeviceMappings:
        - DeviceName: "/dev/sdm"
          Ebs: 
            VolumeType: "gp3"
            Iops: "3000"
            DeleteOnTermination: "false"
            VolumeSize: "200"
      UserData: !Base64 
        'Fn::Sub': |
          #!/bin/bash -xe
          mkfs -t xfs /dev/nvme1n1
          mkdir /backup
          mount /dev/nvme1n1 /backup
      Tags: 
        - Key: Name 
          Value: !Sub ${AWS::StackName} EC2 for DocumentDB test
Mappings:
  AWSInstanceType2Arch:
    m5:
      AMI: ami-03c7d01cf4dedc891
    c5:
      AMI: ami-03c7d01cf4dedc891

Outputs: 
  Ec2Instance:
    Description: "EC2 address"
    Value: !Ref Ec2Instance