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

ssm: unable to use serialized YAML string values as content for CfnDocument #28341

Open
chrisrtaylor opened this issue Dec 12, 2023 · 2 comments
Labels
@aws-cdk/aws-ssm Related to AWS Systems Manager bug This issue is a bug. effort/medium Medium work item – several days of effort p3

Comments

@chrisrtaylor
Copy link

Describe the bug

According to the documentation for the content property of CfnDocument, it should be able to take an object representing JSON formatted content or a String if creating a YAML based document. When trying to use a string value for content, the document fails to create

Expected Behavior

I should be able to define a document using a YAML formatted string as content. The following code should synthesize and create the appropriate resource:

    const ssmDoc = new ssm.CfnDocument(this, "TestSsmDoc", {
      content: `---
schemaVersion: "2.2"
description: "Test SSM doc for CDK."
mainSteps:
  - action: "aws:runShellScript"
    name: "test"
    inputs:
      runCommand: |
        echo "Hello, World!"
`,
      name: "TestSsmDocument",
      documentType: "Command",
      documentFormat: "YAML",
    });

Current Behavior

The above code fails to synthesize with the following error:

CfnSynthesisError: Resolution error: Supplied properties not correct for "CfnDocumentProps"
  content: "---\nschemaVersion: \"2.2\"\ndescription: \"Test SSM doc for CDK.\"\nmainSteps:\n  - action: \"aws:runShellScript\"\n    name: \"test\"\n    inputs:\n      runCommand: |\n        echo \"Hello, World!\"\n" should be an 'object'.
    at ValidationResult.assertSuccess (<PROJECT_ROOT>/node_modules/.pnpm/aws-cdk-lib@2.114.1_constructs@10.3.0/node_modules/aws-cdk-lib/core/lib/runtime.js:1:2707)
    at convertCfnDocumentPropsToCloudFormation (<PROJECT_ROOT>/node_modules/.pnpm/aws-cdk-lib@2.114.1_constructs@10.3.0/node_modules/aws-cdk-lib/aws-ssm/lib/ssm.generated.js:1:24518)
    at CfnDocument.renderProperties (<PROJECT_ROOT>/node_modules/.pnpm/aws-cdk-lib@2.114.1_constructs@10.3.0/node_modules/aws-cdk-lib/aws-ssm/lib/ssm.generated.js:1:18952)
    at PostResolveToken.Resources (<PROJECT_ROOT>/node_modules/.pnpm/aws-cdk-lib@2.114.1_constructs@10.3.0/node_modules/aws-cdk-lib/core/lib/cfn-resource.js:1:7779)
    at PostResolveToken.postProcess (<PROJECT_ROOT>/node_modules/.pnpm/aws-cdk-lib@2.114.1_constructs@10.3.0/node_modules/aws-cdk-lib/core/lib/util.js:1:1653)
    at Object.postProcess (<PROJECT_ROOT>/node_modules/.pnpm/aws-cdk-lib@2.114.1_constructs@10.3.0/node_modules/aws-cdk-lib/core/lib/private/resolve.js:1:1205)
    at DefaultTokenResolver.resolveToken (<PROJECT_ROOT>/node_modules/.pnpm/aws-cdk-lib@2.114.1_constructs@10.3.0/node_modules/aws-cdk-lib/core/lib/resolvable.js:1:1483)
    at resolve (<PROJECT_ROOT>/node_modules/.pnpm/aws-cdk-lib@2.114.1_constructs@10.3.0/node_modules/aws-cdk-lib/core/lib/private/resolve.js:1:2711)
    at Object.resolve (<PROJECT_ROOT>/node_modules/.pnpm/aws-cdk-lib@2.114.1_constructs@10.3.0/node_modules/aws-cdk-lib/core/lib/private/resolve.js:1:1079)
    at resolve (<PROJECT_ROOT>/node_modules/.pnpm/aws-cdk-lib@2.114.1_constructs@10.3.0/node_modules/aws-cdk-lib/core/lib/private/resolve.js:1:2990) {
  type: 'CfnSynthesisError'
}

If I wrap the string literal in new String(...), the stack synthesizes, but does so incorrectly. It renders the context as an indexed dictionary of the string array which fails to create the resource with the error Resource handler returned message: "Invalid request provided: Missing "schemaVersion" in the document.:

Resources:
  TestSsmDoc:
    Type: AWS::SSM::Document
    Properties:
      Content:
        "0": "-"
        "1": "-"
        "2": "-"
        "3": "\n"
        "4": s
        "5": c
        "6": h
        "7": e
        "8": m
        "9": a
        "10": V
        "11": e
        "12": r
        "13": s
        "14": i
        "15": o
        "16": "n"
        "17": ":"
        "18": " "
        "19": '"'
        "20": "2"
        "21": "."
        "22": "2"
<snip>

Reproduction Steps

Create a stack with the following stack and attempt to synthesize the stack:

import * as cdk from "aws-cdk-lib";
import { Construct } from "constructs";
import * as ssm from "aws-cdk-lib/aws-ssm";

export class TestStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const ssmDoc = new ssm.CfnDocument(this, "TestSsmDoc", {
      content: `---
schemaVersion: "2.2"
description: "Test SSM doc for CDK."
mainSteps:
  - action: "aws:runShellScript"
    name: "test"
    inputs:
      runCommand: |
        echo "Hello, World!"
`,
      name: "TestSsmDocument",
      documentType: "Command",
      documentFormat: "YAML",
    });
  }
}

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.114.1 (build 02bbb1d)

Framework Version

No response

Node.js Version

v18.16.1

OS

Mac OS X

Language

TypeScript

Language Version

Typescript (5.2.2)

Other information

No response

@chrisrtaylor chrisrtaylor added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Dec 12, 2023
@github-actions github-actions bot added the @aws-cdk/aws-ssm Related to AWS Systems Manager label Dec 12, 2023
@pahud
Copy link
Contributor

pahud commented Dec 13, 2023

This is a little bit tricky but I was able to synthesize with this:

    const content = yaml.parse(fs.readFileSync(path.join(__dirname, './demo.yaml')).toString());
    new ssm.CfnDocument(this, "TestSsmDoc", {
      content,
      name: "TestSsmDocument",
      documentType: "Command",
      documentFormat: "YAML",
    });

And I got this

  TestSsmDoc:
    Type: AWS::SSM::Document
    Properties:
      Content:
        schemaVersion: "2.2"
        description: Test SSM doc for CDK.
        mainSteps:
          - action: aws:runShellScript
            name: test
            inputs:
              runCommand: |
                echo "Hello, World!"
      DocumentFormat: YAML
      DocumentType: Command
      Name: TestSsmDocument

Let me know if it works for you.

@pahud pahud added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Dec 13, 2023
@pahud pahud changed the title ssm: unable to use string values as content for CfnDocument ssm: unable to use serialized YAML string values as content for CfnDocument Dec 13, 2023
@chrisrtaylor
Copy link
Author

Yes, wrapping the string content with the yaml library does work and is an okay work around. However, the documentation linked above indicates that the content property does accept the String type which is still a problem.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Dec 13, 2023
@pahud pahud added p3 and removed p2 labels Jun 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ssm Related to AWS Systems Manager bug This issue is a bug. effort/medium Medium work item – several days of effort p3
Projects
None yet
Development

No branches or pull requests

2 participants