1

I'm using aws-cdk-lib version 2 to create a CloudFormation stack. I define my API as follows:

const api = new apigatewayv2.CfnApi(this, 'MyApi', { body: YAML.parse(apiSpec), failOnWarnings: true });

This successfully creates my API on deploy. Now I'd like to automate creating a stage for this API. This is what I'm attempting:

new apigatewayv2.CfnStage(this, 'dev-stage', {
  stageName: 'dev',
  apiId: '???????', // <--- How do I find this value?
  autoDeploy: true,
});

As you can see, one of the required parameters when creating CfnStage is apiId, which doesn't seem available via the CfnApi object created earlier (there is a logicalId property, but it's not the value CfnStage is expecting).

I can find the correct value on the AWS console after the CDK script runs, but that doesn't do me much good if I'm trying to automate the creation of the stage via CDK all in a single deploy.

So: How do I determine the correct apiId when creating a CfnStage without having to have already deployed the API?

w.brian
  • 16,296
  • 14
  • 69
  • 118

1 Answers1

3

Use Ref.

AWS::ApiGatewayV2::Api CloudFormation docs: Ref: When you pass the logical ID of this resource to the intrinsic Ref function, Ref returns the API ID, such as a1bcdef2gh.

// synth-time token, deploy-time string
const apiId = cdk.Fn.ref(api.logicalId);
fedonev
  • 20,327
  • 2
  • 25
  • 34