-
Notifications
You must be signed in to change notification settings - Fork 6
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
fix: Log messages as YAML comments #434
Conversation
When a stack is synthesized, AWS CDK will: - print the resulting CloudFormation template as YAML to the console - save the resulting CloudFormation template as JSON to disk The AWS CDK library does not support synthesizing a YAML template to disk. The only way to achieve this is is to redirect the result of `cdk synth` to disk. For example: ```console cdk synth > cloudformation.yaml ``` Since #364 we have started printing important messages to the console. This breaks the saving YAML to disk trick as the file on disk is not valid YAML. For example: ```yaml GuStack has 'migratedFromCloudFormation' set to false. MyDatabase is a stateful construct, it's logicalId will be auto-generated and AWS will create a new resource. Parameters: Stage: Type: String Default: CODE AllowedValues: - CODE - PROD Description: Stage name Resources: MyDatabaseA1B2C3D4: Type: AWS::RDS::DBInstance ``` For this reason, prefix console messages with "# ". This results in `cloudformation.yaml` still being valid YAML. For example: ```yaml \# GuStack has 'migratedFromCloudFormation' set to false. MyDatabase is a stateful construct, it's logicalId will be auto-generated and AWS will create a new resource. Parameters: Stage: Type: String Default: CODE AllowedValues: - CODE - PROD Description: Stage name Resources: MyDatabaseA1B2C3D4: Type: AWS::RDS::DBInstance ```
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have never used this trick before, but having a #
in the log line doesn't cause any harm (and it might actually be useful to have these warnings at the top of a template), so 👍 for this simple workaround!
🎉 This PR is included in version 8.1.3 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Use AWS CDK Annotations to log messages instead of our custom Logger. Annotations have coloured output (info = white, warning = yellow, error = red). They also work with the `--trace`, `--strict` and `--ignore-errors` flags of the CDK CLI. Note, we don't need to employ the trick in #434 as Annotations (somehow) do not get piped to disk if one redirects output to disk. Before: ```console ➜ npx cdk synth --strict --path-metadata false --version-reporting false \# GuStack has 'migratedFromCloudFormation' set to false. MySnsTopic is a stateful construct, it's logicalId will be auto-generated and AWS will create a new resource. Parameters: Stage: Type: String Default: CODE AllowedValues: - CODE - PROD Description: Stage name ``` After: ``` ➜ npx cdk synth --strict --path-metadata false --version-reporting false [Info at /CdkPlayground/MySnsTopic] GuStack has 'migratedFromCloudFormation' set to false. MySnsTopic is a stateful construct, it's logicalId will be auto-generated and AWS will create a new resource. Parameters: Stage: Type: String Default: CODE AllowedValues: - CODE - PROD Description: Stage name ``` See: - https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.Annotations.html
Use AWS CDK Annotations to log messages instead of our custom Logger. Annotations have coloured output (info = white, warning = yellow, error = red). They also work with the `--trace`, `--strict` and `--ignore-errors` flags of the CDK CLI. Note, we don't need to employ the trick in #434 as Annotations (somehow) do not get piped to disk if one redirects output to disk. Before: ```console ➜ npx cdk synth --strict --path-metadata false --version-reporting false # GuStack has 'migratedFromCloudFormation' set to false. MySnsTopic is a stateful construct, it's logicalId will be auto-generated and AWS will create a new resource. Parameters: Stage: Type: String Default: CODE AllowedValues: - CODE - PROD Description: Stage name ``` After: ```console ➜ npx cdk synth --strict --path-metadata false --version-reporting false [Info at /CdkPlayground/MySnsTopic] GuStack has 'migratedFromCloudFormation' set to false. MySnsTopic is a stateful construct, it's logicalId will be auto-generated and AWS will create a new resource. Parameters: Stage: Type: String Default: CODE AllowedValues: - CODE - PROD Description: Stage name ``` See: - https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.Annotations.html
Use AWS CDK Annotations to log messages instead of our custom Logger. Annotations have coloured output (info = white, warning = yellow, error = red). They also work with the `--trace`, `--strict` and `--ignore-errors` flags of the CDK CLI. Note, we don't need to employ the trick in #434 as Annotations (somehow) do not get piped to disk if one redirects output to disk. Before: ```console ➜ npx cdk synth --strict --path-metadata false --version-reporting false # GuStack has 'migratedFromCloudFormation' set to false. MySnsTopic is a stateful construct, it's logicalId will be auto-generated and AWS will create a new resource. Parameters: Stage: Type: String Default: CODE AllowedValues: - CODE - PROD Description: Stage name ``` After: ```console ➜ npx cdk synth --strict --path-metadata false --version-reporting false [Info at /CdkPlayground/MySnsTopic] GuStack has 'migratedFromCloudFormation' set to false. MySnsTopic is a stateful construct, it's logicalId will be auto-generated and AWS will create a new resource. Parameters: Stage: Type: String Default: CODE AllowedValues: - CODE - PROD Description: Stage name ``` See: - https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.Annotations.html
What does this change?
When a stack is synthesized, AWS CDK will:
The AWS CDK library does not support synthesizing a YAML template to disk. The only way to achieve this is is to redirect the result of
cdk synth
to disk.For example:
cdk synth > cloudformation.yaml
Since #364 we have started printing important messages to the console. This breaks the saving YAML to disk trick as the file on disk is not valid YAML.
For example:
For this reason, prefix console messages with "# ". This results in
cloudformation.yaml
still being valid YAML.For example:
See:
Does this change require changes to existing projects or CDK CLI?
No.
How to test
Not sure. Help wanted!
Was thinking the integration-test however not sure how to write the test.
How can we measure success?
The redirect trick to save a YAML template works.
Have we considered potential risks?
n/a