Import Free-Tier EC2 instance configuration in CloudFormation

Page content

Step 1. Create the CloudFormation YAML file

AWSTemplateFormatVersion: '2010-09-09'
Description: My Free Tier

Parameters:
  KeyPair:
    Description: Select KeyPair Name.
    Type: AWS::EC2::KeyPair::KeyName

Resources:
  DefaultVPC:
    Type: 'AWS::EC2::VPC'
    DeletionPolicy: Retain
    Properties:
      CidrBlock: '172.31.0.0/16'
      Tags:
        - Key: 'Name'
          Value: 'atlex00-default-VPC'

  DefaultSubnet:
    Type: 'AWS::EC2::Subnet'
    DeletionPolicy: Retain
    Properties:
      CidrBlock: '172.31.0.0/20'
      Tags:
        - Key: 'Name'
          Value: 'atlex00-default-subnet'
      VpcId: !Ref DefaultVPC

  FreeEC2Instance:
    Type: 'AWS::EC2::Instance'
    DeletionPolicy: Delete
    Properties:
      ImageId: "ami-05d34d340fb1d89e5"
      InstanceType: t2.micro
      SubnetId: !Ref DefaultSubnet
      BlockDeviceMappings:
        - DeviceName: '/dev/xvda'
          Ebs:
            VolumeType: 'gp2'
            VolumeSize: 8
      Tags:
        - Key: 'Name'
          Value: 'free-tier'
      SecurityGroupIds:
        - !Ref mySecurityGroup
      KeyName: !Ref KeyPair
  mySecurityGroup:
    Type: "AWS::EC2::SecurityGroup"
    DeletionPolicy: Delete
    Properties:
      GroupDescription: "This is my-security-group, generated by aws cli"
      VpcId: !Ref DefaultVPC
      Tags:
        - Key: 'Name'
          Value: 'my-security-group'
      SecurityGroupIngress:
      - IpProtocol: tcp
        FromPort: '22'
        ToPort: '22'
        CidrIp: {{ my_home_IP }}/32
        Description: "SSH from home"

Step 2. Import from CloudFront

I did it from browser. After uploading the template, I need to specify which resouces in definition corresponds to which real resources.

After CloudFront imported the configuration, you can see that several tags are added to the resouces, like aws:cloudformation:stack-id, aws:cloudformation:stack-name, and aws:cloudformation:logical-id.