I am using AWS CDK to build my lambda, and I would like to register endpoints from the lambda's CDK stack.
I found I can get an existing ApiGateway construct using fromRestApiId(scope, id, restApiId) (documentation here)
So currently this works well:
//TODO how to look up by ARN instead of restApiId and rootResourceId??
const lambdaApi = apiGateway.LambdaRestApi
.fromRestApiAttributes(this, generateConstructName("api-gateway"), {
restApiId: <API_GATEWAY_ID>,
rootResourceId: <API_GATEWAY_ROOT_RESOURCE_ID>,
});
const lambdaApiIntegration = new apiGateway.LambdaIntegration(lambdaFunction,{
proxy: true,
allowTestInvoke: true,
})
const root = lambdaApi.root;
root.resourceForPath("/v1/meeting/health")
.addMethod("GET", lambdaApiIntegration);
But I would like to deploy to many AWS accounts, and many regions. I don't want to have to hardcode the API_GATEWAY_ID or API_GATEWAY_ROOT_RESOURCE_ID for each account-region pair.
Is there a more generic way to get the existing ApiGateway construct, (e.g. by name or ARN)?
Thank you in advance.
I am using AWS CDK to build my lambda, and I would like to register endpoints from the lambda's CDK stack.
I found I can get an existing ApiGateway construct using fromRestApiId(scope, id, restApiId) (documentation here)
So currently this works well:
//TODO how to look up by ARN instead of restApiId and rootResourceId??
const lambdaApi = apiGateway.LambdaRestApi
.fromRestApiAttributes(this, generateConstructName("api-gateway"), {
restApiId: <API_GATEWAY_ID>,
rootResourceId: <API_GATEWAY_ROOT_RESOURCE_ID>,
});
const lambdaApiIntegration = new apiGateway.LambdaIntegration(lambdaFunction,{
proxy: true,
allowTestInvoke: true,
})
const root = lambdaApi.root;
root.resourceForPath("/v1/meeting/health")
.addMethod("GET", lambdaApiIntegration);
But I would like to deploy to many AWS accounts, and many regions. I don't want to have to hardcode the API_GATEWAY_ID or API_GATEWAY_ROOT_RESOURCE_ID for each account-region pair.
Is there a more generic way to get the existing ApiGateway construct, (e.g. by name or ARN)?
Thank you in advance.
Share Improve this question asked Jan 13, 2021 at 17:25 baumannalexjbaumannalexj 8752 gold badges11 silver badges21 bronze badges 4-
what is ARN for Api Gateway, I don't see one. Are you referring to
arn:aws:execute-api:region:accountId:apiId/*
that we refer in IAM policies? it still has api-id part of it. so, whatever we choose to parameterize the CDK code with stack imports or environment variables or stack inputs, we have to choose to include api-id anyway. – Balu Vyamajala Commented Jan 13, 2021 at 17:44 - Great point :} I found this docs.aws.amazon./apigateway/latest/developerguide/…, but maybe since there is no direct ARN, that is why we cannot look up the existing API construct by ARN. – baumannalexj Commented Jan 13, 2021 at 17:50
- Mostly places, I have exported api id from CloudFormation/CDK and imported into other stacks/cdk where it is used. let me know if you want me to write up an answer with some details. – Balu Vyamajala Commented Jan 13, 2021 at 17:59
- That would be amazing if you could write something on exports! We need to do this in other CDK stacks where we want to reference an export of a group of resources (export a group of security groups, and the CDK can reference the export by name, instead of the unique security groups by account/region -unique names). – baumannalexj Commented Jan 13, 2021 at 18:03
1 Answer
Reset to default 13Lets take a simple Api with one resource
const restApi = new apigw.RestApi(this, "my-api", {
restApiName: `my-api`,
});
const mockIntegration = new apigw.MockIntegration();
const someResource = new apigw.Resource(this, "new-resource", {
parent: restApi.root,
pathPart: "somePath",
defaultIntegration: mockIntegration,
});
someResource.addMethod("GET", mockIntegration);
Lets assume we want use this api and resource in another stack, we first need to export
new cdk.CfnOutput(this, `my-api-export`, {
exportName: `my-api-id`,
value: restApi.restApiId,
});
new cdk.CfnOutput(this, `my-api-somepath-export`, {
exportName: `my-api-somepath-resource-id`,
value: someResource.resourceId,
});
Now we need to import in new stack
const restApi = apigw.RestApi.fromRestApiAttributes(this, "my-api", {
restApiId: cdk.Fn.importValue(`my-api-id`),
rootResourceId: cdk.Fn.importValue(`my-api-somepath-resource-id`),
});
and simply add additional resources and methods.
const mockIntegration = new apigw.MockIntegration();
new apigw.Resource(this, "new-resource", {
parent: restApi.root,
pathPart: "new",
defaultIntegration: mockIntegration,
});