Skip to content

(core): Existing Stack fails with CDK "Unexpected token _ in JSON at position 0" #19335

@deuscapturus

Description

@deuscapturus

What is the problem?

If a stack template was created outside of CDK it may contain errors that are permissible by Cloudformation but are not permissible by yaml_cfn.deserialize. When this happens cdk attempts to deserialize the yaml template as JSON 🤷‍♂️ and will result in an error like this: Unexpected token R in JSON at position 0 during deploy.

In my case the existing template had duplicate keys in a resource property resulting in yaml error YAML parse error, this should never happens, try JSON YAMLSemanticError: Map keys must be unique; "Value" is repeated. But this error message is not displayed to me.

Reproduction Steps

Create a new cdk app with file template lib/template.yaml

lib/cdk-stack.ts

import { Stack, StackProps } from 'aws-cdk-lib';
import { CfnInclude } from 'aws-cdk-lib/cloudformation-include'
import { Construct } from 'constructs';

export class CdkYamlProblemStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);
    new CfnInclude(this, "Cloudformation", {
      templateFile: 'lib/template.yaml'
    })
  }
}

lib/template.yaml

Resources:
  S3Bucket:
    Type: "AWS::SSM::Parameter"
    Properties:
      Type: "String"
      Value: "test"
      Value: "duplicate"

Deploy template.yaml with aws cloudformation deploy:

aws cloudformation deploy --stack-name test-cdk-bug --template-file lib/template.yaml

Update template.yaml and remove the duplciate Value key:

lib/template.yaml

Resources:
  S3Bucket:
    Type: "AWS::SSM::Parameter"
    Properties:
      Type: "String"
      Value: "test"

Now deploy with cdk with the same stack name test-cdk-bug

cdk deploy

What did you expect to happen?

I should get a yaml error indicating the problem with the template:

YAML parse error, this should never happens, try JSON YAMLSemanticError: Map keys must be unique; "Value" is repeated

What actually happened?

I get a mysterious error that doesn't inform me of the problem:

Unexpected token R in JSON at position 0

CDK CLI Version

2.15.0

Framework Version

No response

Node.js Version

v14.17.0

OS

Fedora

Language

Typescript

Language Version

No response

Other information

The problem is here: https://github.com/aws/aws-cdk/blob/master/packages/aws-cdk/lib/serialize.ts#L18-L19

Why is cdk attempting to deserislize with JSON as the comments suggest should never happen?

Even if this continues the error in the catch should still be logged out to the user.

Related to #15907

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/coreRelated to core CDK functionalitybugThis issue is a bug.effort/smallSmall work item – less than a day of effortp1

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions