Aws-cdk: CLI: way to get outputs from deployed stack

Created on 14 Feb 2019  Â·  30Comments  Â·  Source: aws/aws-cdk

After deploying the stack, there's no way in the CLI to go back and get outputs from the deployed stack with the CDK CLI. I expected something like "cdk metadata" to give me this information.

For example, the LoadBalancedFargateService construct outputs the DNS name of the load balancer. It will be useful to be able to query that output value for a CDK stack with the CDK CLI.

efformedium feature-request in-progress packagtools

Most helpful comment

picking this task up!

All 30 comments

You can scrape the outputs from the CLI stdout after running cdk deploy. Is that not enough? Would you prefer something like:

> cdk metadata --name MyLoadBalancer
your.dns.name

Yeah I'm really thinking of the scenario where I go back later after a deploy and need to grab the DNS name. It feels weird to issue a deploy again to get that. Even a list of the outputs would be helpful, since they tend to get auto-named something like ServiceLoadBalancerDNSEC5B149E

Had the same issue, solved it for now by adding this as a script to the package.json

aws cloudformation describe-stacks --stack-name <YOUR_STACK_NAME> | jq '.Stacks | .[] | .Outputs | reduce .[] as $i ({}; .[$i.OutputKey] = $i.OutputValue)'

+1

This is a pretty significant challenge when using the CDK programmatically, as the outputs are also randomly named. For example my stack has:

"Outputs": [
  {
      "OutputKey": "apiLoadBalancerDNS77556DD8",
      "OutputValue": "api-apiLB28-7S29OJ4JYVNQ-1248184514.us-east-2.elb.amazonaws.com"
  }
]

For now I'm relying on manually querying the output from CloudFormation like this:

aws cloudformation describe-stacks --stack-name <my stack name> --query "Stacks[0].Outputs[0].OutputValue" --output text

But the usage of a zero index isn't particularly safe as a new output may be added in the future. However I have no way of knowing what the key will be for the particular output that I need

Would something like “cdk deploy —format=json” which will output a json key value map of outputs and their values work?

I am finding this quite difficult as well. I am using cdk in a Jenkins pipeline.
Just getting and using my output is proving tricky.

Would something like “cdk deploy —format=json” which will output a json key value map of outputs and their values work?

I think that would be a good place to start

Another significant dimension to the problem is that the output keys are
auto generated so I don’t have a good way to know what they will be because
of the random portions. The CDK format=JSON needs some way to locate
outputs by construct name and attribute name because the auto generated key
name can not be known ahead of time

On Wed, Jul 10, 2019 at 7:59 AM Cristian RaČ› notifications@github.com
wrote:

Would something like “cdk deploy —format=json” which will output a json
key value map of outputs and their values work?

I think that would be a good place to start

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/awslabs/aws-cdk/issues/1773?email_source=notifications&email_token=AA2VN6T63QIOW77XA4ZSDPTP6XFJPA5CNFSM4GXSHCH2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZTHLGY#issuecomment-510031259,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AA2VN6U3MF2RZGFUGSWCMVTP6XFJPANCNFSM4GXSHCHQ
.

For me simply selecting one of the outputs is enough. For example I need the
VPC id to for the next step of the Jenkins pipeline. So if I could do
cdk deploy...... --get-output MYID
Where MYID is what I specified as CfnOutput in the stack.
Hope this makes sense. Writing from phone

On Wed, 10 Jul 2019, 20:44 Nathan Peck, notifications@github.com wrote:

Another significant dimension to the problem is that the output keys are
auto generated so I don’t have a good way to know what they will be because
of the random portions. The CDK format=JSON needs some way to locate
outputs by construct name and attribute name because the auto generated key
name can not be known ahead of time

On Wed, Jul 10, 2019 at 7:59 AM Cristian RaČ› notifications@github.com
wrote:

Would something like “cdk deploy —format=json” which will output a json
key value map of outputs and their values work?

I think that would be a good place to start

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<
https://github.com/awslabs/aws-cdk/issues/1773?email_source=notifications&email_token=AA2VN6T63QIOW77XA4ZSDPTP6XFJPA5CNFSM4GXSHCH2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZTHLGY#issuecomment-510031259
,
or mute the thread
<
https://github.com/notifications/unsubscribe-auth/AA2VN6U3MF2RZGFUGSWCMVTP6XFJPANCNFSM4GXSHCHQ

.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/awslabs/aws-cdk/issues/1773?email_source=notifications&email_token=ALFYCRFSZ3HJPAIHFA34F4DP6Y3Z5A5CNFSM4GXSHCH2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZUQZUI#issuecomment-510201041,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ALFYCRB5WYYWTNGPAWGRA3TP6Y3Z5ANCNFSM4GXSHCHQ
.

I've got a workaround for this. Rather than trying to get the value from an automatically named output we can manually create our own output in the CDK app like this:

import cdk = require('@aws-cdk/core');

// A manually named output
new cdk.CfnOutput(this, 'LoadBalancerDNS', {
  value: ecsService.loadBalancer.loadBalancerDnsName
});

Then its possible to locate the value for that manually named output in the CloudFormation outputs like this:

aws cloudformation describe-stacks \
  --stack-name <my stack name> \
  --query "Stacks[0].Outputs[?OutputKey==`LoadBalancerDNS`].OutputValue" \
  --output text

This CLI command will give you the value for the output, which in this case is the DNS for a load balancer. In my use case I needed to get the DNS name in order to run automated tests against the deployment, so I was able to embed this command in my build script after the CDK deploy to get the url for the deployed environment

Exactly what I did. But working around to get functionality that should already exist, is not ideal :)

My 2 bits. From an automation perspective.

cdk outputs --format=json (so I don't have to run a deploy for instance)

Or even dump them in cdk.out/<stack_name>.outputs.json during the deploy command.

Currently I do something like: cdk deploy 2>&1 | tee -a .deploy-log

Then grep the outputs section, awk for the thing I want.

It works, but using jq or a native cdk command that knew how to parse the cdk.out/<stackname>.outputs.json would be great.

An output file(s) would be a great initial step to this path I think.

It doesn't really make sense that outputs will be saved to cdk.out but maybe something similar. @shivlaks this is a desired feature of this CLI

I would like to add consideration for dynamic stack names in this feature. For integration tests we have dynamic stack names with a git branch appended. Being able to cdk deploy MyStack-branch-* and then get the outputs relevant for that deployment is a _bit_ nicer than writing a file like MyStack-branch-abc123.outputs.json and then needing to ls | grep to find it.

Something like cdk deploy 'MyStack-*' --save-outputs=outputs.json where I don't need to know the full stack name is closer to turn-key for this use case.

thank you so much @nathanpeck for the idea, the CfnOutput is amazing. Literally can output anything I need in precise detail like this.

@eladb “cdk deploy —format=json” sounds great too :+1: - is there a way to hook into an "all operations done, finishing" event that I can add an after-deployment action as callback ? In my case, I need to invoke a script that writes all outputs to a file.

I am also trying to get some outputs of the deployed CF stacks. Does anyone have any workaround solution to make it works?

@shivlaks this needs to be highly prioritized in the CLI backlog

@shivlaks I agree with @eladb this needs to be prioritized

for example I need to output AppSync API Endpoint, ID and Keys that are generated. These are not in the cloudformation templates, and I can get the endpoint and keys from aws cli, but need to at least know what the ID is and output that.

Another thing that would be valuable to me is resolved SSM parameters, or even just stack parameters of the currently deployed stacks.

Seeing these in a similar command as what we're discussing on this issue would be nice. If SSM params are resolved by Cloudformation, they show up in the parameters section of the stack, so seeing them on the CLI would also be nice.

Maybe this ask could be it's own issue? If we get outputs first, turning all params into an output would be a decent stop gap.

I'll second having cdk output (since it resembles the equivalent terraform command). CfnOutput is an okay solution but having a tiny bit of sugar around it would be nice so it feels more like a first class feature.

Just ran into this myself. :+1:

Will probably go with the manual cdk.CfnOutput workaround for now, but having a cdk output command would be much appreciated.

cdk deploy —format=json requires wrapper scripts. A better approach would be to have a post-deploy-app parameter, which CDK invokes. The stdin could be the json. But still, this feels incomplete.

A different design would have the CLI become a library that user code invokes to deploy a stack. Being aware of the object model, the deploy step could fill in all tokens.

It's really sad to see this not being available out of the box (like terraform output for example).

Since this is a common need for me and the projects I'm working on, I created a temporary solution in terms of an utility script: https://github.com/Dzhuneyt/cdk-get-stack-output script. I hope you find it useful.

Sample usage:
./cdk-output --name=s3bucket --fromStack=my-cdk-stack

Contributions welcome.

picking this task up!

@shivlaks is there an rfc for the feature?

It's really sad to see this not being available out of the box (like terraform output for example).

Harsh considering it's only been in GA since July last year.

Thanks for the script link though, do you have any documentation for it?

@jsdtaylor Yes, the README.md. It's very simple to use with 2 parameters only.

@nathanpeck 's solution should be fixed like below for me :star: :

new cdk.CfnOutput(this, 'OutputId', {
  value: ...,
  exportName: 'OutputExportName',
});
aws cloudformation describe-stacks \
  --stack-name <my stack name> \
  --query "Stacks[0].Outputs[?ExportName==`OutputExportName`].OutputValue" \
  --output text

While this isn't directly related to the CLI, some 'syntactic sugar' for cdk.CfnOutput has been mentioned here, so it felt relevant.

It would be kind of cool if any cdk.CfnOutput were also accessible directly from the corresponding cdk.Stack it was used within.

As a short contrived example:

class FooStack extends cdk.Stack {
  // ..snip..
  new cdk.CfnOutput(this, 'Foo', { value: 'foo!' })
  // ..snip..
}

const app = new cdk.App()
const fooStack = new FooStack(app, 'foo', {})

console.log(fooStack.outputs['Foo'])

I know we can use Fn.importValue, but I guess I was hoping for something that flows a little more 'natively' in the code.

https://docs.aws.amazon.com/cdk/latest/guide/constructs.html talks about using standard JS class properties to expose relevant details, which is workable, but then we end up having to both define the property as well as the cdk.CfnOutput.

Here's a fork from nathanpeck using jq. Hope it helps.

aws cloudformation describe-stacks \
    --stack-name YOURSTACK  \
    --query "Stacks[0].Outputs" \
    --output json | jq -rc '.[] | select(.OutputKey=="YOUR_OUTPUT_KEY") | .OutputValue '

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Kent1 picture Kent1  Â·  3Comments

v-do picture v-do  Â·  3Comments

kawamoto picture kawamoto  Â·  3Comments

nzspambot picture nzspambot  Â·  3Comments

peterdeme picture peterdeme  Â·  3Comments