0
votes

I have existing infrastructure in us-east-1 region which needed to be cloned exactly to us-east-2 region. Used AWS CloudFormer to generate the JSON template from existing us-east-1 region, replaced all the us-east-1 with us-east-2 and started creating the stack but getting errors saying "Resource creation cancelled", specifically for all the EC2 instances

A snapshot of the template (only EC2 instance):

"instancei071dd59b": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "DisableApiTermination": "false",
        "InstanceInitiatedShutdownBehavior": "stop",
        "ImageId": "ami-1a41b377",
        "InstanceType": "t2.medium",
        "KeyName": "MyServer",
        "Monitoring": "false",
        "Tags": [
          {
            "Key": "MyServer OS",
            "Value": "Windows Server"
          },
          {
            "Key": "Name",
            "Value": "MyServer_WEB_TEST_2"
          }
        ],
        "Volumes": [
          {
            "Device": "xvdb",
            "VolumeId": {
              "Ref": "volumevol9124b841"
            }
          }
        ],
        "NetworkInterfaces": [
          {
            "DeleteOnTermination": "true",
            "DeviceIndex": 0,
            "SubnetId": {
              "Ref": "subnet24031c0f"
            },
            "PrivateIpAddresses": [
              {
                "PrivateIpAddress": "172.31.53.184",
                "Primary": "true"
              }
            ],
            "GroupSet": [
              {
                "Ref": "sgMyServerWEB"
              }
            ],
            "AssociatePublicIpAddress": "true"
          }
        ]
      }
    },
"volumevol9124b841": {
      "Type": "AWS::EC2::Volume",
      "Properties": {
        "AvailabilityZone": "us-east-2b",
        "Size": "30",
        "SnapshotId": "snap-95288b92",
        "VolumeType": "gp2"
      }
    }
2

2 Answers

1
votes

Before going with cloudformation template you will need to make sure you have following things in place :

  1. Move your instance AMI to us-east-2 region then replace the snapshot id and AMI id in your template
  2. Create a security group replace the security group id in your template
  3. Replace subnet ID in your CF template with the one in us-east-2 region

The reason you will have to do this is every resource on AWS has unique IDs which cannot be replicated, if you want to replicate same you will need different Ids for that you need to create seperate resources and use them in your template.

If your doing this for a single instance only then you might do it manually by exporting AMI to us-east-2 region.

1
votes

For collecting AMI ID in different region, I'd recommend to use the image name instead the AMI ID as the key.

To build resources to be placed in different regions, definitely is better to use CloudFormation. In this case you can use the lambda cli2cloudformation (https://github.com/lucioveloso/cli2cloudformation).

Using it, you can get the AMI ID across all regions and whatever other information that you are able to get using CLI.

To collect the AMI ID, create a lambda with cli2cloudformation and inside your template, create a custom resource as bellow:

"imageIdNameBased": {
    "Type": "Custom::cli2cfnLambda",
    "Properties": {
      "ServiceToken": "arn:aws:lambda:eu-west-1:123456789012:function:cli2cfnLambda",
      "CliCommandCreate": "ec2 describe-images --filters 'Name=name,Values=amzn-ami-hvm-2017.03.0.20170417-x86_64-gp2' --query 'Images[0]'"
    }
}

In this case, I'm getting the AMI ID to the Image named 'amzn-ami-hvm-2017.03.0.20170417-x86_64-gp2'. You can change to your image name.

After that, you can retrieve it in any point of your CloudFormation stack.

"Fn::GetAtt" : ["imageIdNameBased", "ImageId"]