Amazon CloudFormation 是否为将 RDS 实例部署到多可用区配置中的不同环境做好了准备?

Does Amazon CloudFormation make provision for deploying RDS instances to different environments in a Multi-AZ configuration?

我正在尝试使用 CloudFormation 模板在不同环境中创建 Amazon RDS 实例。 Prod中有Multi-AZ需求,其他环境则不需要Multi-AZ。这需要 CloudFormation 中的条件函数。

基于 RDS CloudFormation docs and using the if condition in CloudFormation,模板中应包含以下内容:

Conditions:
  IsProd: !Equals [ !Ref EnvironmentType, prod ]
...
Resources:
  MyRDSInstance:
    Properties:
      ...
      AvailabilityZone:
        !If [ IsProd, AWS::NoValue, af-south-1a ]
      ...
      MultiAZ: !If [ IsProd, true, false ]

IsProd 计算为:

You can't set the AvailabilityZone parameter if the MultiAZ parameter is set to true.

但是,当我尝试部署 prod RDS 实例时,我在创建堆栈时在 CloudFormation 中仍然遇到以下错误,这导致资源无法创建:

Requesting a specific availability zone is not valid for Multi-AZ instances. (Service: AmazonRDS; Status Code: 400; Error Code: InvalidParameterCombination; Request ID: e6177fe4-4a4b-4db3-ba66-5f0e0f7218eb; Proxy: null)

我怀疑这是 AWS 中的一个错误,因为最近对源代码进行了更改,即使它与 CDK 相关而不是 CloudFormation:

会不会是 CloudFormation 现在没有提供 AWS::NoValue 伪参数?如果这是源代码中的错误,是否有任何方法可以解决这个问题,以便我仍然可以仅在 prod 环境中实现多可用区?

所以我尝试在我这边复制相同的内容,但就我而言,我能够成功创建 RDS 资源。我附上我使用的模板供您参考。

AWSTemplateFormatVersion: 2010-09-09
Description: >-
  Description": "AWS CloudFormation Sample Template for creating an Amazon RDS DB instance: 
  Sample template showing how to create a DB instance with Enhanced Monitoring enabled. 
  **WARNING** This template creates an RDS DB instance. You will be billed for the AWS 
  resources used if you create a stack from this template.
Parameters:
  IsMultiAZ:
    Type: String
    Default: false
    AllowedValues: [true,false]
    Description: Please enter either "true" or "false"
  DBInstanceID:
    Default: mydbinstance
    Description: My database instance
    Type: String
    MinLength: '1'
    MaxLength: '63'
    AllowedPattern: '[a-zA-Z][a-zA-Z0-9]*'
    ConstraintDescription: >-
      Must begin with a letter and must not end with a hyphen or contain two
      consecutive hyphens.
  DBName:
    Default: mydb
    Description: My database
    Type: String
    MinLength: '1'
    MaxLength: '64'
    AllowedPattern: '[a-zA-Z][a-zA-Z0-9]*'
    ConstraintDescription: Must begin with a letter and contain only alphanumeric characters.
  DBInstanceClass:
    Default: db.m5.large
    Description: DB instance class
    Type: String
    ConstraintDescription: Must select a valid DB instance type.
  DBAllocatedStorage:
    Default: '50'
    Description: The size of the database (GiB)
    Type: Number
    MinValue: '20'
    MaxValue: '65536'
    ConstraintDescription: must be between 20 and 65536 GiB.
  DBUsername:
    NoEcho: 'true'
    Description: Username for MySQL database access
    Type: String
    MinLength: '1'
    MaxLength: '16'
    AllowedPattern: '[a-zA-Z][a-zA-Z0-9]*'
    ConstraintDescription: must begin with a letter and contain only alphanumeric characters.
  DBPassword:
    NoEcho: 'true'
    Description: Password MySQL database access
    Type: String
    MinLength: '8'
    MaxLength: '41'
    AllowedPattern: '[a-zA-Z0-9]*'
    ConstraintDescription: must contain only alphanumeric characters.
Conditions:
  CheckIsMultiZone:
     !Equals [!Ref IsMultiAZ, true]

Resources:
  MyDB:
    Type: 'AWS::RDS::DBInstance'
    Properties:
      DBInstanceIdentifier: !Ref DBInstanceID
      DBName: !Ref DBName
      DBInstanceClass: !Ref DBInstanceClass
      AllocatedStorage: !Ref DBAllocatedStorage
      Engine: MySQL
      EngineVersion: "8.0.16"
      MasterUsername: !Ref DBUsername
      MasterUserPassword: !Ref DBPassword
      MultiAZ: !Ref IsMultiAZ
      AvailabilityZone: !If [CheckIsMultiZone, !Ref AWS::NoValue, "us-east-1a"]

如您所见,我使用了与您相同的概念。您能否在最后测试此模板,看看它是否有效。我在您的模板中发现的一个问题是您使用的是 AWS::NoValue,而正确的格式是 !Ref AWS::NoValue,如我的模板所示。我相信这是你的问题。您可以查看示例 here .