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

标签 aws-cloudformation amazon-ecs

在通过 cloudformation 创建 AWS::ECS::Service 期间,我收到错误:模型验证失败

该错误与#HealthCheckGracePeriodSeconds 和其他一些属性有关。错误详细信息为:预期类型:Number,发现:String

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

我需要一些提示,因为我现在陷入困境。

错误是:

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
    )

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]

最佳答案

导致该错误的原因是 SecurityGroupsSubnets 导致格式错误。

为了提取子网安全组,使用了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

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

SecurityGroups: !Split [",", !FindInMap [ Env2SecurityGroups, !Ref AWS::AccountId, securitygroup] ]
Subnets: !Split [",", !FindInMap [ Env2PublicSubnets, !Ref AWS::AccountId, subnets] ]

关于aws-cloudformation - 无法通过 cloudformation yaml 创建 AWS::ECS::Service,模型验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64246726/

相关文章:

amazon-web-services - 防止 AWS cloudformation 中的回滚

amazon-web-services - 为什么 AWS Cloudformation 在尝试创建此 RecordSetGroup 时显示 "Invalid request"?

amazon-web-services - 用于创建 ECS 服务的 Cloudformation 模板卡在 CREATE_IN_PROGRESS 中

docker - 如何在 Amazon ECS 上找到服务地址 (URL) 和/或对 Mailtrain 实例进行故障排除?

amazon-web-services - 如何在 AWS-ECR 中为 Docker 镜像使用环境变量?

amazon-ec2 - CloudFormation userdata 创建 RedHat 用户

amazon-web-services - AWS SAM 模板无法引用安全组资源(之前在 cloudformation 模板外部创建)

amazon-web-services - CloudFormation 条件 EMR 实例

amazon-web-services - 将 AWS ELB HTTP 请求重定向到 HTTPS

node.js - 当 cpu 为 100% 时,为什么我的负载均衡器不重定向到可用的 ec2?