gpt4 book ai didi

aws-cloudformation - Amazon CloudFormation 是否规定将 RDS 实例部署到多可用区配置中的不同环境?

转载 作者:行者123 更新时间:2023-12-03 07:22:36 25 4
gpt4 key购买 nike

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

基于RDS CloudFormation docs并使用 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计算结果为:

  • false、AvailabilityZone:af-south-1aMultiAZ:false
  • true,AvailabilityZone is removedMultiAZ: true,满足 docs 中指定的要求:

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 伪参数?如果这是源代码中的错误,有什么方法可以解决这个问题,以便我仍然可以仅在生产环境中实现多可用区?

最佳答案

因此,我尝试在最后复制相同的内容,但就我而言,我能够成功创建 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 .

关于aws-cloudformation - Amazon CloudFormation 是否规定将 RDS 实例部署到多可用区配置中的不同环境?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72286019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com