【问题标题】:How to get AWS Availability zone in Cloud formation dynamically?如何动态获取 Cloud Formation 中的 AWS 可用区?
【发布时间】:2021-06-29 11:43:05
【问题描述】:

最近我开始探索 AWS CloudFormation。我对使用 Fn::GetAZs 动态获取可用区有疑问吗?我希望可用区不被硬编码。 YAML 格式的代码如下:-

AWSTemplateFormatVersion: 2010-09-09
Description: Hardcoded Value of Availability zone
Parameters: 
 MyKeyName:
  Description: Arshan Select the key Name from the boelow 
  Type: AWS::EC2::KeyPair::KeyName
 MyZone:
  Description: Select the MyAZone
  Type: String
  Default: us-east-1
  AllowedValues:
  - us-east-1
  - us-east-2
Resources:
 DevEC2Instance:
  Type: AWS::EC2::Instance
  Properties:
   InstanceType: t2.micro
   ImageId: ami-04aa88aebb9fefd83
   KeyName: !Ref MyKeyName 
   SecurityGroup: !Ref SSHSecurityGroup
   AvailabilityZone: !Ref MyZone
 SSHSecurityGroup:
  Type: AWS::EC2::SecurityGroup 
  Properties: 
   GroupDescription: My Sg
   SecurityIngress:
    IpProtocol: tcp
    ToPort: 22
    FromPort: 22
    Cidr: 0.0.0.0/0
   SecurityEgress:
    IpProtocol: tcp 
    ToPort: 8080
    FromPort: 8080
    Cidr: 0.0.0.0/0

【问题讨论】:

  • 您的问题是什么?使用AvailabilityZone: !Select [ 0, !GetAZs ]
  • @jarmod 我的问题是我想使用 Fn::GetAZs,返回一个可用区的值,然后从中选择任何可用区
  • 您在使用 Fn::GetAZs 时遇到什么问题?

标签: amazon-web-services amazon-s3 amazon-ec2 aws-lambda amazon-cloudformation


【解决方案1】:

如果您想在参数MyZone 中使用Fn::GetAZs 来定义允许的可用区,那么您不能这样做。 CloudFormation 不支持这样的事情。您的 MyZone 必须硬编码您想要允许的 AZ。

Fn::GetAZs 稍后可以在您的资源中使用,但不能在MyZone 参数中使用。

【讨论】:

  • 感谢您的帮助。现在我在资源部分使用 Fn::GetAzs,即 AvailabilityZone: !Select - 0 - !GetAZs Ref: 'AWS::Region'
  • @Deepak 没问题。如果答案有帮助,我们将不胜感激。
猜你喜欢
  • 2015-01-03
  • 1970-01-01
  • 2017-08-18
  • 1970-01-01
  • 2018-03-05
  • 1970-01-01
  • 2018-07-29
  • 2018-02-02
  • 1970-01-01
相关资源
最近更新 更多