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

fix: Log messages as YAML comments #434

Merged
merged 1 commit into from
Apr 13, 2021
Merged

fix: Log messages as YAML comments #434

merged 1 commit into from
Apr 13, 2021

Conversation

akash1810
Copy link
Member

@akash1810 akash1810 commented Apr 13, 2021

What does this change?

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:

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:

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:

# 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

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

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
```
@akash1810 akash1810 requested a review from a team April 13, 2021 06:06
Copy link
Contributor

@jacobwinch jacobwinch left a 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!

@akash1810 akash1810 merged commit ffb5382 into main Apr 13, 2021
@akash1810 akash1810 deleted the aa-yaml-comments branch April 13, 2021 09:34
@github-actions
Copy link
Contributor

🎉 This PR is included in version 8.1.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

akash1810 added a commit that referenced this pull request May 7, 2021
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
akash1810 added a commit that referenced this pull request May 7, 2021
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
akash1810 added a commit that referenced this pull request May 10, 2021
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants