[英]How can I instruct an AWS CloudFormation template to create resources in a specific region?
I am new to CloudFormation templates.我是 CloudFormation 模板的新手。 I have basic template in yaml that creates an EC2 Instance.我在 yaml 中有创建 EC2 实例的基本模板。 Every time I create a stack and use this template, the EC2 Instance is ALWAYS created on US East N. Virginia region.每次我创建堆栈并使用此模板时,EC2 实例总是在美国东部弗吉尼亚北部地区创建。 I am trying to change this so that the EC2 Instance resides in US-WEST-2 region.我正在尝试更改此设置,以便 EC2 实例位于 US-WEST-2 区域。 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.相反,我需要在 AWS 控制台中将区域更改为 us-west-2,然后创建一个新堆栈。 Is my understanding correct?我的理解正确吗?
Unfortunately, you can't specify the region in a cloudformation template.不幸的是,您无法在 cloudformation 模板中指定区域。
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
或者,在 aws cli 配置文件~/.aws/config
指定默认区域
[default]
region=eu-west-1
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.我确信我们可以使用参数指定在 CFN 模板中创建堆栈的区域,并且我们确实有活动模板,可以根据参数值在相应的区域中创建我们的堆栈。 The AWS::Region pseudo parameter is a value that AWS CloudFormation resolves as the region where the stack is created. AWS::Region 伪参数是 AWS CloudFormation 将其解析为堆栈创建区域的值。 https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/gettingstarted.templatebasics.html 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"
}
]
}
}
}
}
如果您能够将模板拆分为多个部分,则可以通过一些编排和StackSets一次部署到不同的区域。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.