(pipelines): Malformed YAML during stage deployment
Not 100% sure if this is a CDK, CodePipeline or CloudFormation bug.
It appears that CloudFormation bug (https://github.com/aws/aws-cdk/issues/11910) that has blocked our deployments for a few weeks was fixed! 🎉
However, trying to deploy the same stack to our first application stage now results in the following:
Template format error: YAML not well-formed. (line 5034, column 88) (Service: AmazonCloudFormation; Status Code: 400; Error Code: ValidationError; Request ID: 6c5ea7db-4315-4f03-9b58-97af912c627a)
Which is odd - if I synthesize that stack locally, the YAML produced is only ~3000 lines long. Not really sure what I can do on my side to pinpoint the syntax error, if there is one.
Reproduction Steps
- Add a few resources (a composite alarm, a few normal ones, some metrics) to a stack, that already has a lot of monitoring resources
- Push the change, triggering the CDK pipeline
What did you expect to happen?
The stack to be deployed successfully in all application stages.
What actually happened?
The prepare step for the stack in the first stage fails with the error:
Template format error: YAML not well-formed. (line 5034, column 88) (Service: AmazonCloudFormation; Status Code: 400; Error Code: ValidationError; Request ID: 6c5ea7db-4315-4f03-9b58-97af912c627a)
Environment
- CDK CLI Version : 1.82.0
- Framework Version: 1.82.0
- Node.js Version: 14.13.0
- OS : Ubuntu 20.04
- Language (Version): TypeScript
Other
- Region:
eu-north-1 - Pipeline execution ID:
f1bed18e-5916-44ef-acf0-edb3215d8c74 - CloudFormation request ID:
6c5ea7db-4315-4f03-9b58-97af912c627a
This is :bug: Bug Report
If you go into the artifacts of the CodePipeline and you look at the YAML template there, what does it look like?
You will find them by going to the Synth CodeBuild project, finding the latest successful build and clicking build details:

Download that file, rename it to have a .zip extension, extract it, then look at the YAML file inside and see if that is malformed in any way.
NOTE: This may require you to extend permissions on the KMS Key that's used to encrypt the CodePipeline artifact bucket, because by default your IAM User/Role probably won't be allowed to read those CodePipeline artifacts otherwise. Add kms:Decrypt to the statement that has your account ID in it:

I assume this error means your template is bigger than 51200 bytes which is the max size in codepipeline.
You have to split your template
Thx for the detailed instructions, @rix0rrr!
@hoegertn That's strange - although my template is 156KB, I also have another 110KB one that has been deploying successfully. Does CodePipeline directly call *Stack CFN APIs with the template in the body, without using an S3 location? (which has a 1MB limit)
The line CFN is complaining about is the AssertDescription one in this block, which doesn't make a lot of sense...
"Rules": {
"CheckBootstrapVersion": {
"Assertions": [
{
"Assert": {
"Fn::Not": [
{
"Fn::Contains": [
[
"1",
"2",
"3"
],
{
"Ref": "BootstrapVersion"
}
]
}
]
},
"AssertDescription": "CDK bootstrap stack version 4 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
}
]
}
}
I can try splitting it up though. Are there any best practices of how to do this in CDK? I'm considering of moving the heavier bits into a NestedStack.
see https://docs.aws.amazon.com/codepipeline/latest/userguide/action-reference-CloudFormation.html under TemplatePath. CodePipeline uses the direct call instead of S3 apparently.
I hit this exact error some days ago. The error message looked familiar but is completely useless for this underlying error.
For splitting you do not need to use NestedStacks you can just create multiple "normal" stacks.
This is pretty dang nasty.
Putting all of your infra in a single nested stack will solve it though. That way the "parent" stack is < 50kB and we defer for the actual template to our usual asset system.
I also have another 110KB one that has been deploying successfully
Has this been deploying successfully via CDK Pipelines?
The line CFN is complaining about is the AssertDescription one in this block, which doesn't make a lot of sense...
"Rules": {
Well... this is a somewhat non-standard CloudFormation feature (originally designed for Service Catalog). It's still weird that it would affect you here, since afaik we put this into all templates and it's normally fine.
But it could still totally be the rule we're synthesizing here?
OK, it appears that the size was the issue - at least extracting the bulkiest set of resources into a nested stack (just to have a slightly nicer hierarchy/resource dependencies in my case) did the trick.
Has this been deploying successfully via CDK Pipelines?
Yes. I have no idea how the 110KB stack deploys though, while a 156KB one doesn't...
Hm. Maybe CloudFormation raised their service call limits from 50kB (old limit) to 100-something kB (new limit) ? And the docs just haven't caught up yet?
EDIT: Hmm not documented: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-limits.html
Yup, that was my thought. Feel free to resolve this , if you think it's technically not a CDK issue, even though the CFN limit isn't clear.
But a more descriptive error would be amazing in this case! :) And if possible, using a TemplateURL for CloudFormation calls to allow for bigger templates (although I can see how this could be more complicated, due to S3 cross account permissions).
Putting all of your infra in a single nested stack will solve it though. That way the "parent" stack is < 50kB and we defer for the actual template to our usual asset system.
That is actually a great idea and use case for nested stacks. Currently, I try to avoid them.
OK, it appears that the size was the issue - at least extracting the bulkiest set of resources into a nested stack (just to have a slightly nicer hierarchy/resource dependencies in my case) did the trick.
Has this been deploying successfully via CDK Pipelines?
Yes. I have no idea how the 110KB stack deploys though, while a 156KB one doesn't...
Are you sure this stack is deployed using the CloudFOrmation actions and not using cdk deploy?
Hm. Maybe CloudFormation raised their service call limits from 50kB (old limit) to 100-something kB (new limit) ? And the docs just haven't caught up yet?
EDIT: Hmm not documented: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-limits.html
I hit it some days ago with 70K so no raise here.
Yup, that was my thought. Feel free to resolve this , if you think it's technically not a CDK issue, even though the CFN limit isn't clear.
But a more descriptive error would be amazing in this case! :) And if possible, using a
TemplateURLfor CloudFormation calls to allow for bigger templates (although I can see how this could be more complicated, due to S3 cross account permissions).
I am in contact next week with the CodePipeline team to discuss this. This is ridiculous in my opinion.
I've a feeling this might yet be something else than simply template size. Keeping this open so Thorsten has a place to share his findings next week.
Experiencing the same validation error asking me to switch to version 4. Running cdk bootstrap doesn't make any changes.
Putting all of your infra in a single nested stack will solve it though. That way the "parent" stack is < 50kB and we defer for the actual template to our usual asset system.
That is actually a great idea and use case for nested stacks. Currently, I try to avoid them.
Yeah... the biggest downside I saw in this case is loss of useful snapshots for nested stacks (when changing the nested stack contents, the only snapshot change is the asset hash/location of the nested stack template).
Is there something in @aws-cdk/assert I can use to recursively cover nested stacks in my snapshots?
Is there something in
@aws-cdk/assertI can use to recursively cover nested stacks in my snapshots?
Not out of the box I don't think. You can build something though.
So the error could not be traced internally, but the limitation of the template size was indeed just a docs bug. So bigger templates are allowed.
Whenever somebody hits this issue again please ping me and we will dive into the generated output to track it down and give the CFN team some reproducible snippets.
Can this ticket be set to "need investigation" or sth like that?
@hoegertn we have just hit this problem. We are deploying via CdkPipelines. The prepare step fails:

This is curious, since during prepare the pipeline just creates a CFN Changeset. I can download the artifact and create the changeset manually however:

As you can see the template is 56,1 kB. This is the location corresponding to the Error-Message:

Seems to me the issue might be lying in the underlying Codepipeline Action for Cloudformation. :thinking: