4
votes

I am new to CloudFormation templates. I have basic template in yaml that creates an EC2 Instance. Every time I create a stack and use this template, the EC2 Instance is ALWAYS created on US East N. Virginia region. I am trying to change this so that the EC2 Instance resides in US-WEST-2 region. After some research, it appears that this is something that is not specified within the template. Instead, I need to change the region to us-west-2 in AWS console and then create a new stack. Is my understanding correct?

3
Yes, the resources are created in the region you have selected in the web console. It is always like that. When using the command line interface, you can pass in a --region parameter to set the desired region.Milan Cermak

3 Answers

5
votes

Unfortunately, you can't specify the region in a cloudformation template.

You should either pass region as a command line argument

aws --region eu-west-1 cloudformation create-stack --stack-name ...

or, specify the default region in aws cli config file ~/.aws/config

[default]
region=eu-west-1
0
votes

What am I missing here? I am sure we can specify region where the stack is created in CFN template using parameters and we do have active templates which creates our stack in respective region based on the parameter value. The AWS::Region pseudo parameter is a value that AWS CloudFormation resolves as the region where the stack is created. https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/gettingstarted.templatebasics.html

Here is a sub-section of sample template

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Parameters": {
    "InstanceType": {
      "Description": "Instance Type",
      "Type": "String",
      "Default": "t2.xlarge"
    },
    "SubnetUSEAST1": {
      "Description": "Subnet on which Ec2 instance needs to be created",
      "Type": "String",
      "Default": "subnet-xxxxxxxx"
    },
    "SubnetUSWEST2": {
      "Description": "Subnet on which Ec2 instance needs to be created",
      "Type": "String",
      "Default": "subnet-yyyyyyyy"
    }
  },
  "Conditions": {
    "useast1": {
      "Fn::Equals": [
        {
          "Ref": "AWS::Region"
        },
        "us-east-1"
      ]
    },
    "uswest2": {
      "Fn::Equals": [
        {
          "Ref": "AWS::Region"
        },
        "us-west-2"
      ]
    }
  },
  "Resources": {
    "EC2Instance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "InstanceType": {
          "Ref": "InstanceType"
        },
        "NetworkInterfaces": [
          {
            "SubnetId": {
              "Fn::If": [
                "useast1",
                {
                  "Ref": "SubnetUSEAST1"
                },
                {
                  "Ref": "SubnetUSWEST2"
                }
              ]
            },
            "AssociatePublicIpAddress": "false",
            "DeviceIndex": "0"
          }
        ]
      }
    }
  }
}
0
votes

If you are able to split your template into parts, you could deploy to different regions at once via some orchestration and StackSets.