amazon-web-services - 需要 Properties 键的条件逻辑语法

标签 amazon-web-services aws-cloudformation

我需要帮助来弄清楚 Cloudformation 部分的语法,该部分处理为私有(private)子网路由表创建默认路由。模板的逻辑是堆栈创建者在启动时选择 dev 或 prod。如果选择 prod,则会创建 2 个 NAT 网关,每个可用区一个。如果选择 dev,则会创建单个 NAT 实例。在默认专用路由的资源定义中,我需要能够在使用 InstanceID: 和 GatewayID: 之间进行选择,具体取决于选择的选项。

我尝试过的方法不起作用:

  DefaultPrivateRoute1:
    Type: AWS::EC2::Route
    Properties:
      !If
      - IsProd
      -
        GatewayId: !Ref NatGateway1
        RouteTableId: !Ref PrivateRouteTable1
        DestinationCidrBlock: 0.0.0.0/0
      - InstanceId: !Ref NATInstance
        RouteTableId: !Ref PrivateRouteTable1
        DestinationCidrBlock: 0.0.0.0/0

测试: prod:卡在 DefaultPrivateRoute1“已启动资源创建”上,然后失败并显示“路由未在预期时间内稳定”

开发人员:有效!

 DefaultPrivateRoute1:
   Type: AWS::EC2::Route
   Properties:
     !If
     - IsProd
     -
       GatewayId: !Ref NatGateway1
     - InstanceId: !Ref NATInstance
       RouteTableId: !Ref PrivateRouteTable1
       DestinationCidrBlock: 0.0.0.0/0

测试: prod:在 DefaultPrivateRoute1 上失败,“必须指定 DestinationCidrBlock 和 DestinationIpv6CidrBlock 之一,且不能为空”

开发人员:有效!

  DefaultPrivateRoute1:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref PrivateRouteTable1
      DestinationCidrBlock: 0.0.0.0/0
      !If
      - IsProd
      -
        GatewayId: !Ref NatGateway1
      - InstanceId: !Ref NATInstance

测试: prod:模板格式错误:YAML 格式不正确。 (第 200 行,第 7 列)(!If 语句后的第一行)

dev:模板格式错误:YAML 格式不正确。 (第 200 行,第 7 列)(!If 语句后的第一行)

有人知道如何格式化它吗?

最佳答案

正确的属性是 NatGatewayId,而不是 GatewayId。

Type: AWS::EC2::Route
Properties:
  !If
  - IsProd
  - NatGatewayId: !Ref NatGateway1
    RouteTableId: !Ref PrivateRouteTable1
    DestinationCidrBlock: 0.0.0.0/0
  - InstanceId: !Ref NATInstanceBastion
    RouteTableId: !Ref PrivateRouteTable1
    DestinationCidrBlock: 0.0.0.0/0

关于amazon-web-services - 需要 Properties 键的条件逻辑语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72465429/

相关文章:

amazon-web-services - 如何使用Elastic Beanstalk将文件夹从主机安装到Docker容器?

java - 从 AWS Lambda 获取超时 AmazonDynamoDBClient dbClient = new AmazonDynamoDBClient();

python - Lambda函数根据标签过滤AWS EC2实例?

amazon-web-services - 当 CloudFormation 的状态为 "pending"时,EC2 实例是否可能处于 "CREATE_COMPLETE"状态?

json - 简单的 StackPolicy 在 CloudFormation 上始终无效

amazon-web-services - AWS 无服务器错误 - 对于表达式 "Stacks[].StackStatus",我们匹配了预期路径 : "ROLLBACK_COMPLETE" at least once

python - 线程正在为 Amazon 服务的 TCP 请求留下打开的文件

python - 带有 C 文件的 AWS Lambda 部署包

amazon-web-services - CloudFormation 条件函数 Fn::Join 的 yaml 语法出现问题: | !Join 指定多个资源 arn 值

docker - AWS 日志驱动程序未将 `tail -f` 拾取到 Docker 镜像 `stdout`