无法通过 cloudformation yaml 创建 AWS::ECS::Service,模型验证失败

Sma*_* Ma 5 amazon-ecs aws-cloudformation

AWS::ECS::Service通过 cloudformation创建期间,我收到错误消息:Model validation failed

该错误与#HealthCheckGracePeriodSeconds其他一些属性有关。错误详情是: expected type: Number, found: String

在 yaml 中它已经是一个数字。我不清楚出了什么问题。已经尝试将其声明为字符串或类型为 Number 的参数。

我需要一些提示,因为此时我陷入困境。

错误是:

Model validation failed 
    (
    #/HealthCheckGracePeriodSeconds: expected type: Number, found: String 
    #/DesiredCount: expected type: Number, found: String 
    #/DeploymentConfiguration/MaximumPercent: expected type: Number, found: String 
    #/DeploymentConfiguration/MinimumHealthyPercent: expected type: Number, found: String
    )
Run Code Online (Sandbox Code Playgroud)

template.yaml 中的定义是:

ServiceDefinition:
  Type: AWS::ECS::Service
  Properties:
    ServiceName: !Ref ServiceName
    Cluster: !Ref ClusterName
    TaskDefinition: !Ref TaskDefinition
    DeploymentConfiguration:
      MinimumHealthyPercent: 100
      MaximumPercent: 200
    DesiredCount: 1
    HealthCheckGracePeriodSeconds: 60
    LaunchType: FARGATE
    NetworkConfiguration:
      AwsVpcConfiguration:
        AssignPublicIP: ENABLED
        SecurityGroups: !FindInMap [Env2SecurityGroups, !Ref AWS::AccountId, securitygroup]
        Subnets: !FindInMap [Env2PublicSubnets, !Ref AWS::AccountId, subnets]
Run Code Online (Sandbox Code Playgroud)

Sma*_* Ma 8

该错误是由于格式错误SecurityGroups而引起的。Subnets

提取subnets并使用securitygroups该功能。FindInMap这个结果必须是一个列表。这可以使用该功能来实现Split

不幸的是,错误的格式会导致完全误导性的错误消息。

像这样声明映射:

Mappings
  Env2SecurityGroups:
    '111111111111':
      securitygroup: 'sg-1111111111111111'
    '222222222222':
      securitygroup: 'sg-2222222222222222'
    '333333333333':
      securitygroup: 'sg-3333333333333333'

  Env2PublicSubnets:
    '111111111111':
      subnets: subnet-1111111111111111,subnet-22222222222222222,subnet-33333333333333333
    '222222222222':
      subnets: subnet-1111111111111111,subnet-22222222222222222,subnet-33333333333333333
    '333333333333':
      subnets: subnet-1111111111111111,subnet-22222222222222222,subnet-33333333333333333
Run Code Online (Sandbox Code Playgroud)

使用!Split与 结合!FindInMap来获取列表:

SecurityGroups: !Split [",", !FindInMap [ Env2SecurityGroups, !Ref AWS::AccountId, securitygroup] ]
Subnets: !Split [",", !FindInMap [ Env2PublicSubnets, !Ref AWS::AccountId, subnets] ]
Run Code Online (Sandbox Code Playgroud)

  • 其实我想我现在明白了。看来这个错误是有误导性的。数字转换实际上并不是失败的原因。故障在其他地方,CF 报告错误。这只是 CloudFormation 的另一个问题。该错误不一定与问题有任何关系。 (13认同)
  • 我很好奇您如何解决“预期类型:数字”错误,因为我在设置任务定义时遇到相同的错误。目前尚不清楚这个答案与问题中的错误有何关系。 (3认同)