我们正在使用AWS CDK创建无服务器REST API。但是,端点很多,有时我们必须销毁并重新部署堆栈。为了防止REST API URL随每次部署更改,我计划在一个堆栈中创建API GATEWAY,并在单独的堆栈中添加方法和资源。如何在单独的堆栈中引用创建的rest API?
试图实现https://github.com/aws/aws-cdk/issues/3705中的某些功能,但是所有资源(API网关,资源和方法)都被推送到单个堆栈中,而不是一个堆栈中推送API Gateway,而其他堆栈中的资源则被推送。
相关代码段如下:
bts-app-cdk.ts
const first = new FirstStack(app, 'FirstStack', {
env: {
region: 'us-east-1',
account: '1234567890',
}
});
const second = new SecondStack(app, 'SecondStack', {
apiGateway: first.apiGateway,
env: {
region: 'us-east-1',
account: '1234567890',
}
});
second.addDependency(first)
first-stack.ts
export class FirstStack extends cdk.Stack {
public readonly apiGateway: apig.IResource;
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const apiGateway = new apig.RestApi(this, 'BooksAPI', {
restApiName:'Books API',
})
apiGateway.root.addMethod('GET');
this.apiGateway = apiGateway.root;
}
}
第二堆
export interface SecondStackProps extends cdk.StackProps {
readonly apiGateway: apig.IResource;
}
export class SecondStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props: SecondStackProps) {
super(scope, id, props);
props.apiGateway.addMethod('ANY')
}
}