I am trying to add a new Route and Integration to my existing REST API in AWS API Gateway. I am using the below code snippet to make it happen:
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
export interface IApiGatewayIntegrationProps extends cdk.StackProps {
/**
* Application Name. Will be used to name all the resources
*/
appName: string;
/**
* Route name to add the API Gateway Integration onto.
* For example: setting `admin` for admin-api, the invocation url will be `${apiGatewayInvocationUrl}/admin`
*/
apiPath: string;
/**
* REST API ID for an existing API
*/
restApiId: string;
/**
* ID for the root resource in the API
*/
restApiRootResourceId: string;
/**
* VPC Link ID
*/
VpcLink: string;
/**
* URL for the Network Load Balancer (NLB)
*/
NLBDns: string;
/**
* Listener port on the NLB
*/
NLBPort: number;
}
export class CustomApiGatewayIntegration extends Construct {
constructor(scope: Construct, id: string, props: IApiGatewayIntegrationProps) {
super(scope, id);
const api = cdk.aws_apigateway.RestApi.fromRestApiAttributes(scope, 'api', {
restApiId: props.restApiId,
rootResourceId: props.restApiRootResourceId,
});
const proxyIntegration = new cdk.aws_apigatewayv2.CfnIntegration(this, 'gateway-integration', {
apiId: api.restApiId,
connectionId: props.VpcLink,
connectionType: 'VPC_LINK',
description: 'API Integration',
integrationMethod: 'ANY',
integrationType: 'HTTP_PROXY',
integrationUri: `http://${props.NLBDns}:${props.NLBPort}/${props.apiPath}/{proxy}`,
});
new cdk.aws_apigatewayv2.CfnRoute(this, 'gateway-route', {
apiId: api.restApiId,
routeKey: 'ANY somepath/{proxy+}',
target: `integrations/${proxyIntegration.ref}`,
});
}
}
After deploying the CDK Stack, I get the following error in the terminal:
failed: Error: The stack named $STACK_NAME failed to deploy: UPDATE_ROLLBACK_COMPLETE: Invalid API identifier specified $AWS_ACCOUNT_ID:$REST_API_ID
This is how the error looks in the Cloudformation console:
The interesting bit here is that the error message shows the AWS Account ID in addition to the Actual API ID. How do I resolve this?
Appreciate any help on this! Thanks in advance
Edit 1:
apigateway import means how the API Gateway class methods are imported. AWS Cloudformation has two Resource Groups:
- AWS::APIGateway
- AWS::APIGatewayV2
Both of them have different capabilities. In older versions of the AWS CDK (v1.x), you had to import both the resource groups separately:
Old: import * as apigateway from '@aws-cdk/aws-api-gateway';
New: import * as apigatewayv2 from '@aws-cdk/aws-api-gatewayv2';
The newer CDK has brought everything together and can be written simply as:
import * as cdk from 'aws-cdk-lib';
// Call to v1 Resource Group:
const api = new cdk.aws_apigateway.RestApi(...);
// Call to v2 Resources:
const apiv2 = new cdk.aws_apigatewayv2.CfnRestApi(...);

