question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to pipe output of cdk diff to file (without colors)?

See original GitHub issue

I found cdk diff and @aws-cdk/cloudformation-diff.formatDifferences as fabulous tools when rewriting some CFN stacks to CDK. It is really great!

Yesterday I attempted to check differences between two templates with cdk diff but there was too many lines that has changed and it was not really tiresome to use terminal to check all of this changes, so I decided to pipe output to some text file cdk diff > out.txt. I got an empty file, so I tried again with stderr - cdk diff 2> out.txt and I noticed there are also saved some extra sequences for text and background colors, extract:

Resources
[~] AWS::IAM::Policy Pipeline/Role/DefaultPolicy PipelineRoleDefaultPolicyC7A05455 
 └─ [~] PolicyDocument
     └─ [~] .Statement:
         └─ @@ -65,54 +65,6 @@
            [ ]   ]
            [ ] },
            [ ] {
            [-]   "Action": [
            [-]     "cloudformation:CreateChangeSet",
            [-]     "cloudformation:DeleteChangeSet",
            [-]     "cloudformation:DescribeChangeSet",
            [-]     "cloudformation:DescribeStacks"
            [-]   ],
            [-]   "Condition": {
            [-]     "StringEqualsIfExists": {
            [-]       "cloudformation:ChangeSetName": "Deploy"
            [-]     }
            [-]   },
            [-]   "Effect": "Allow",
            [-]   "Resource": [
            [-]     {
            [-]       "Fn::Join": [
            [-]         "",
            [-]         [

I am not a person who gives up so easily to troubleshoot that problem, so I wrote simple JS script which utilizes the @aws-cdk/cloudformation-diff package to check it is related to formatDifferences function or not. Answer is “yes, it is”.

const cfndiff = require('@aws-cdk/cloudformation-diff')
const fs = require('fs')

const templatesDir = 'some/outputdir'

const tplA = JSON.parse(fs.readFileSync(`${templatesDir}/${process.argv[2]}`).toString())
const tplB = JSON.parse(fs.readFileSync(`${templatesDir}/${process.argv[3]}`).toString())

cfndiff.formatDifferences(process.stdout, cfndiff.diffTemplate(tplB, tplA))

Of course, I went through I wrote some quick post-processing script to ignore those sequences but personally, I would be more satisfied if this function will allow to specify if I want or not an output in a plain-text or not.

Generally speaking, it is a good practice to check if output is piped or not, and then (unless forced with some flag, like --color/--no-color) do not add those colouring sequences, otherwise use them.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

13reactions
erict-devcommented, Feb 10, 2021

putting the answer here because this thread still comes up in many searches:

cdk diff --no-color &> diff.txt

2reactions
sam-goodwincommented, Jan 30, 2019

We should update the CLI to detect if a terminal is attached, like awslint does: https://github.com/awslabs/aws-cdk/blob/master/tools/awslint/bin/awslint.ts#L41

I’ve also hacked around it by piping to perl: cdk deploy | perl -pe 's/\x1b\[[^m]+m//g;')"

Read more comments on GitHub >

github_iconTop Results From Across the Web

Git diff output to file preserve coloring - Stack Overflow
git diff --color > foo.txt ... Open the output diff file in Sublime Text 2. ... You can pipe the output to an...
Read more >
How do I pipe colored diff output to less? - Unix Stack Exchange
diff cannot output colors, you need another program, such as colordiff for that. Colors in the terminal are printed via ANSI escape codes...
Read more >
cdk-pretty-diff - npm
Formatting tool for CDK Diff output. Inspired by Terraform prettyplan ... There are no other projects in the npm registry using cdk-pretty-diff.
Read more >
Command: plan | Terraform - HashiCorp Developer
-no-color - Disables terminal formatting sequences in the output. Use this if you are running Terraform in a context where its output will...
Read more >
aws-cdk/pipelines module - AWS Documentation - Amazon.com
It is not intended to support arbitrary deployment pipelines, ... to an ECS cluster directly: use CDK file assets with CloudFormation Init for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found