首先,您需要检查VPC接口端点本身是否已成功创建。然后,您可以尝试在CDK代码中显式等待VPC端点的创建完成,以确保在创建API网关时它已经存在。
以下是一个可能的解决方案:
import * as ec2 from '@aws-cdk/aws-ec2';
import * as apigw from '@aws-cdk/aws-apigateway';
// 创建VPC Endpoint
const vpc = new ec2.Vpc(this, 'MyVpc');
const service = new ec2.GatewayVpcEndpoint(this, 'MyEndpoint', {
service: ec2.GatewayVpcEndpointAwsService.S3,
vpc
});
// 显式等待端点创建完成
const vpcReady = service.endpointServiceIsCreated;
const timeout = {timeout: Duration.minutes(30)};
await vpcReady.catch((e) => {
console.error('VPC Endpoint not ready', e);
});
console.log('VPC Endpoint ready');
// 创建API网关
const restApi = new apigw.RestApi(this, 'MyApi', {
endpointTypes: [apigw.EndpointType.VPC_LINK],
endpointConfiguration: {
types: [apigw.EndpointType.VPC_LINK],
vpcLinks: [
{vpcLinkId: service.vpcEndpointId}
]
}
});
这个代码片段中,我们显式地等待了VPC endpoint的创建完成,然后将其ID用于创建API网关。您可以将所需的VPC endpoint service更改为适合您的需求的任何服务。