Sai
Sai

Reputation: 2109

How can I instruct an AWS CloudFormation template to create resources in a specific region?

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?

Upvotes: 6

Views: 6076

Answers (3)

DuckMaestro
DuckMaestro

Reputation: 15885

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

Upvotes: 0

Sushant Sonker
Sushant Sonker

Reputation: 152

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"
          }
        ]
      }
    }
  }
}

Upvotes: 0

Dzmitry Bahdanovich
Dzmitry Bahdanovich

Reputation: 1815

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

Upvotes: 7

Related Questions