【问题标题】:How can I instruct an AWS CloudFormation template to create resources in a specific region?如何指示 AWS CloudFormation 模板在特定区域中创建资源?
【发布时间】:2019-06-13 17:01:09
【问题描述】:

我是 CloudFormation 模板的新手。我在 yaml 中有创建 EC2 实例的基本模板。每次我创建堆栈并使用此模板时,总是会在美国东弗吉尼亚州地区创建 EC2 实例。我正在尝试更改此设置,以便 EC2 实例位于 US-WEST-2 区域。经过一些研究,这似乎是模板中未指定的内容。相反,我需要在 AWS 控制台中将区域更改为 us-west-2,然后创建一个新堆栈。我的理解正确吗?

【问题讨论】:

  • 是的,资源是在您在 Web 控制台中选择的区域中创建的。总是这样。使用命令行界面时,可以传入--region参数来设置想要的区域。

标签: amazon-web-services amazon-ec2 amazon-cloudformation aws-cli


【解决方案1】:

很遗憾,您无法在 cloudformation 模板中指定区域。

您应该将区域作为命令行参数传递

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

或者,在 aws cli 配置文件 ~/.aws/config 中指定默认区域

[default]
region=eu-west-1

【讨论】:

    【解决方案2】:

    我在这里缺少什么?我确信我们可以使用参数指定在 CFN 模板中创建堆栈的区域,并且我们确实有活动模板,可以根据参数值在各个区域中创建我们的堆栈。 AWS::Region 伪参数是 AWS CloudFormation 解析为创建堆栈的区域的值。 https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/gettingstarted.templatebasics.html

    这是示例模板的一个子部分

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

    【讨论】:

    • 它没有回答问题。
    【解决方案3】:

    如果您能够将模板拆分为多个部分,则可以通过一些编排和StackSets 一次性部署到不同的区域。

    【讨论】:

      猜你喜欢
      • 2018-03-20
      • 2018-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-12
      • 2019-08-24
      • 2017-07-15
      • 1970-01-01
      相关资源
      最近更新 更多