How to get cfnoutputs of AWS stack to a file using AWS-CDK
Solution 1
For every value you want saved after the stack is run add a core.CfnOutput
call in your code.
Then when you deploy your stack, use:
% cdk deploy {stack-name} --profile $(AWS_PROFILE) --require-approval never \
--outputs-file {output-json-file}
This deploys the stack, doesn't stop to ask for yes/no approvals (so you can put it in a Makefile or a CI/CD script) and once done, saves the value of every CfnOutput
in your stack to a JSON file.
Details here: https://github.com/aws/aws-cdk/commit/75d5ee9e41935a9525fa6cfe5a059398d0a799cd
Solution 2
This answer is only relevant if you're using CDK <1.32.0. Since then #7020 was merged and --outputs-file
is supported. See the top voted answer for a full example.
Based on this closed issue, your best bet is using AWS CLI to describe the stack and extract the output. For example:
aws cloudformation describe-stacks \
--stack-name <my stack name> \
--query "Stacks[0].Outputs[?OutputKey==`PublicIp`].OutputValue" \
--output text
If you're using Python, this can also be done with boto3.
import boto3
outputs = boto3.Session().client("cloudformation").describe_stacks(StackName="<my stack here>")["Stacks"][0]["Outputs"]
for o in outputs:
if o["OutputKey"] == "PublicIp":
print(o["OutputValue"])
break
else:
print("Can't find output")
Solution 3
Not sure if this is a recent update to cdk CLI, but cdk deploy -O
will work.
-O, --outputs-file
Path to file where stack outputs will be written as JSON
This is an option now. It will take cdk.CfnOutput
and put in JSON file.
https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.CfnOutput.html
Amit Kanderi
Updated on June 24, 2022Comments
-
Amit Kanderi almost 2 years
I want to store the Cfnoutputs in AWS-CDK to a file(Python).
Below is the code to show Public IP on console.
my_ip = core.CfnOutput( scope=self, id="PublicIp", value=my_ec2.instance_public_ip, description="public ip of my instance", export_name="my-ec2-public-ip")
I have tried using redirecting the output in Python by using command:
cdk deploy * > file.txt
But no success.
Please help
-
Amit Kanderi about 4 yearsYeah this is one way of doing this. But AWS CDK using AWS SDK so why to use again another SDK. I have researched and found a module called custom resources can be found at link. So as written in the documentation it can handle response. But I have to investigate it more to see if this can be use or any other workaround to solve it.
-
kichik about 4 yearsCloudFormation custom resources still run in the context of deployment. They won't run on your machine so any output they may have will still need to be retrieved using AWS SDK.
-
Stephan Rauh over 3 yearsTo flesh out the answer: I was interested in the URL of my API gateway endpoints, because they change with every deployment. So I added
new cdk.CfnOutput(this, 'API_URL', { value: api.url });
to the end of the stack definition. After runningcdk deploy --output-file result.json
the fileresult.json
is generated, containing the URL in theAPI_URL
line.