Reputation: 204
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
Upvotes: 11
Views: 17345
Reputation: 34704
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")
Upvotes: 9
Reputation: 216
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
Upvotes: 0
Reputation: 13433
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
Upvotes: 20