Based on what you write I think the best option is to use CfnOutput
. With CfnOutput
you can return the security group or instance id like you would do it in the Outputs
section from CloudFormation.
For example:
new CfnOutput(this, 'SecurityGroupOutput', {
value: mySecurityGroup.securityGroupId,
description: 'security group id' // Optional,
exportName: 'MySecurityGroupId'
});
Then you can retrieve the stack outputs using the AWS CLI (or Python, whatever fits you) and use this value in your scripts:
aws cloudformation describe-stacks --stack-name <your-stack-name> --query "Stacks[0].Outputs[?ExportName == MySecurityGroupId].OutputValue" --output text
I'm using ExportName
here because CDK will automatically create some logical id for the output which isn't easy to read or remember. You can override it by calling .overrideLogicalId(newLogicalId)
on the CfnOutput
if you would like to use it instead to retrieve the correct OutputValue
.
You can do it similarly for retrieving the EC2 instance id.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…