Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not create AWS::ECS::Service via cloudformation yaml, got Model validation failed

During creation of AWS::ECS::Service via cloudformation i got the error: Model validation failed

The error is related to #HealthCheckGracePeriodSeconds and some other properties. Error detail is: expected type: Number, found: String.

In yaml it is already a number. It's not clear to me whats going wrong. Already tried to desclare it as string or as parameter with type Number.

I need some hint because i am stuck in the muck at this point.

Error is:

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
    )

Definition in template.yaml is:

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]
like image 424
Sma Ma Avatar asked Oct 07 '20 14:10

Sma Ma


People also ask

How do I validate AWS CloudFormation template?

If it isn't, CloudFormation checks if the template is valid YAML. If both checks fail, CloudFormation returns a template validation error. You can validate templates locally by using the --template-body parameter, or remotely with the --template-url parameter.

What happens when CloudFormation stack creation fails?

If stack creation fails, go to the CloudFormation Resources list in the AWS Management Console to find the log group. Note that if stack creation fails before any instances are launched, a log group might not be created. By default, AWS deletes CloudWatch log groups if stack creation fails.

Does ECS use CloudFormation?

Amazon ECS supports creating clusters, task definitions, services, and task sets in AWS CloudFormation. The following examples demonstrate how to create resources with these templates using the AWS CLI.

How do you debug a CloudFormation stack error?

You can view logs, such as /var/log/cloud-init. log or /var/log/cfn-init. log , to help you debug the instance launch. You can retrieve the logs by logging in to your instance, but you must disable rollback on failure or else AWS CloudFormation deletes the instance after your stack fails to create.


1 Answers

The error was caused because SecurityGroups and Subnets resulted in a wrong format.

To extract subnets and securitygroups the FindInMap function was used. It is necessary that this result is a list. This can be achieved using the Split function.

The wrong format unfortunately leads to a completely misleading error message.

Declare mappings like this:

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

Use !Split combined with !FindInMap to get a list:

SecurityGroups: !Split [",", !FindInMap [ Env2SecurityGroups, !Ref AWS::AccountId, securitygroup] ]
Subnets: !Split [",", !FindInMap [ Env2PublicSubnets, !Ref AWS::AccountId, subnets] ]
like image 194
Sma Ma Avatar answered Oct 19 '22 20:10

Sma Ma