Home > front end >  Access existing API gateway by ARN
Access existing API gateway by ARN

Time:11-15

I'm building a Step Function using CDK. I need to call call external service using WAIT_FOR_TASK_TOKEN integration pattern. This external service is a separate CDK stack.

Previously, this was done using SQS queue:

new SqsSendMessage(stack, "Name", {
  queue: Queue.fromQueueArn("existing queue arn"),
  integrationPattern: IntegrationPattern.WAIT_FOR_TASK_TOKEN,
  ...
})

I used the ARN of the existing queue in order to define the queue for the integration.

Now I'd like to replace this with a call to the API defined in that other CDK stack. With the queue I could easily access existing one by providing its ARN, which seems not to be possible with API Gateway. CallApiGatewayRestApiEndpoint requires that I pass existing a CDK object of type IRestApi, but I don't see a way to get this object from its ARN.

Am I missing something or is it only possible to CallApiGatewayRestApiEndpoint to APIs defined in the same CDK stack?

CodePudding user response:

You can get a read-only IRestApi reference to an existing API Gateway with the fromRestApiId static method:

const api: IRestApi = RestApi.fromRestApiId(this, 'ApiRef', 'my-api-id');

Note that this interface-type construct is a glorified wrapper for the ApiId. The CDK accepts your ApiId without question, performing no cloud-side lookups or validation.

It's not much, but it's exactly what CallApiGatewayRestApiEndpoint needs. CallApiGatewayRestApiEndpoint is the CDK implementation of the Step Function API Gateway Optimized Integration. The integration expects an endpoint URL, not an ARN. The api: IRestApi prop is used to assemble that URL, which has the form <API ID>.execute-api.<region>.amazonaws.com.

  • Related