gpt4 book ai didi

amazon-web-services - 使 VPC 创建成为可选

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

我正在尝试在我的 cloudformation 脚本中设置 VPC 创建条件。例如,如果参数中提供了一个VPC id,那么我想创建这个VPC中的所有资源,否则创建一个新的。

当我想重用现有 VPC,但我无法推导出我的资源之一所需的子网时,问题就开始了。所以我想,我必须将它们作为参数提供。但如果我将它们作为参数提供,在我想创建一个新的 VPC 的情况下,它会提示,因为子网 ID 列表为空,并且它必须是有效的。

错误为参数验证失败:参数名称 VpcPrivateSubnetIds 的参数值不存在。用户请求回滚。 不可能给出任何虚拟值。有什么想法可以实现这一点吗?

这是我的 CF 脚本:

VpcId:
Type: String
Description: Give the VPC id if you want to use an existing one. Leave empty for creating a new one.

VpcPublicSubnetIds:
Type: List<AWS::EC2::Subnet::Id>
Description: List of 3 public SubnetIds for the given VPC.

VpcPrivateSubnetIds:
Type: List<AWS::EC2::Subnet::Id>
Description: List of 3 private SubnetIds for the given VPC.


Conditions:
CreateVPC: !Equals [ !Ref VpcId, ""]


Resources:
(...)
Properties:
PrivateSubnetIds: !If
- CreateVPC
- !GetAtt VPCStack.Outputs.PrivateSubnets
- !Join [',', [!Select [0, !Ref VpcPrivateSubnetIds], !Select [1, !Ref VpcPrivateSubnetIds], !Select [2, !Ref VpcPrivateSubnetIds]]]
PublicSubnetIds: !If
- CreateVPC
- !GetAtt VPCStack.Outputs.PublicSubnets
- !Join [',', [!Select [0, !Ref VpcPublicSubnetIds], !Select [1, !Ref VpcPublicSubnetIds], !Select [2, !Ref VpcPublicSubnetIds]]]

最佳答案

一种解决方案是将 subnetId 参数视为字符串,然后将其保留为空。 (但当 VPC 存在时,用户必须手动输入子网 ID 列表)。

如果列表不为空(要使用现有的 VPC),请使用 Cloudformation custom resource lambda 将字符串(逗号分隔)转换为列表并返回到 cloudformation 以用于资源创建。所以你的堆栈看起来像这样

Parameters:
VpcId:
Type: String
Description: Give the VPC id if you want to use an existing one. Leave empty for creating a new one.
VpcPublicSubnetIds:
Type: String
Description: List of 3 public SubnetIds for the given VPC.
Default: ''
VpcPrivateSubnetIds:
Type: String
Description: List of 3 private SubnetIds for the given VPC.
Default: ''
Conditions:
CreateVPC: !Equals [ !Ref VpcId, ""]
CreateList: !Not [!Equals [ !Ref VpcId, ""]]
Resources:
CreateList:
Type: AWS::CloudFormation::CustomResource
Condition: CreateList
Properties:
ServiceToken:<some token>
Public: !Ref VpcPublicSubnetIds
Private: !Ref VpcPrivateSubnetIds
SomeResource:
Properties:
PrivateSubnetIds: !If
- CreateVPC
- !GetAtt VPCStack.Outputs.PrivateSubnets
- !GetAtt CreateList.PrivateSubnetIds
PublicSubnetIds: !If
- CreateVPC
- !GetAtt VPCStack.Outputs.PublicSubnets
- !GetAtt CreateList.PublicSubnetIds

请注意,我已验证此脚本,因此您可能需要进行一些更正。

关于amazon-web-services - 使 VPC 创建成为可选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55360803/

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