Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
deuscapturus opened this issue Mar 11, 2022 · 1 comment · Fixed by #19338
Closed
Labels
@aws-cdk/core Related to core CDK functionality bug This issue is a bug. effort/small Small work item – less than a day of effort p1

Comments

@deuscapturus
Copy link

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

@deuscapturus deuscapturus added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Mar 11, 2022
@github-actions github-actions bot added the @aws-cdk/core Related to core CDK functionality label Mar 11, 2022
@rix0rrr rix0rrr added effort/small Small work item – less than a day of effort p1 and removed needs-triage This issue or PR still needs to be triaged. labels Mar 11, 2022
@rix0rrr rix0rrr removed their assignment Mar 11, 2022
@mergify mergify bot closed this as completed in #19338 Mar 11, 2022
mergify bot pushed a commit that referenced this issue Mar 11, 2022
For some reason, we are falling back to reading JSON when loading YAML fails.

Since YAML is a superset of JSON, the only reason errors would occur because the YAML is not valid, in which case we shouldn't do this catch at all.

Fixes #19335.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

TheRealAmazonKendra pushed a commit to TheRealAmazonKendra/aws-cdk that referenced this issue Mar 11, 2022
For some reason, we are falling back to reading JSON when loading YAML fails.

Since YAML is a superset of JSON, the only reason errors would occur because the YAML is not valid, in which case we shouldn't do this catch at all.

Fixes aws#19335.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/core Related to core CDK functionality bug This issue is a bug. effort/small Small work item – less than a day of effort p1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants