如何使用 AWS-CDK 从 apigateway 迁移到 apigatewayv2?
具体来说:我正在使用 LambdaRestApi
和 restApiId
和 deploymentStage
从那个资源。
// old
const apiGw = new apigateway.LambdaRestApi(this, 'MyAPI', {
handler: lambdaFrontend,
proxy: true,
binaryMediaTypes: ['*/*'],
});
// new
const apiGw2 = new apigateway.CfnApi(this as any, 'MyAPIV2', {
protocolType: "http",
target: lambdaFrontend.functionArn,
})
我正在尝试为 CF 获取 OriginSource,如下所示:
const domainName = ${apiGw.restApiId}.execute-api.${this.region}.${this.urlSuffix};
第一个问题:我如何检索
domainName
与 ApiGW2?我还需要 stageName。目前我正在像这样检索它:
const originPath = '/' + apiGw.deploymentStage.stageName;
第二个问题:如何使用 ApiGW2 检索原始路径?
或者:有没有更好的方法将我的 ApiGW2 与 CF 连接起来?
const fecf = new cf.CloudFrontWebDistribution(this, "MyCF", {
originConfigs: [{
customOriginSource: {
domainName: `${apiGw.restApiId}.execute-api.${this.region}.${this.urlSuffix}`,
},
originPath: '/' + apiGw.deploymentStage.stageName,
...
}
最佳答案
现在可以很容易地解决这个问题,因为我们现在有官方文档。
如果现在有人想迁移到 V2,方法如下:
const httpApiIntegration = new apigatewayv2Integrations.LambdaProxyIntegration({
handler: fn,
});
const httpApi = new apigatewayv2.HttpApi(this, "MyApiV2");
httpApi.addRoutes({
path: "/",
methods: [HttpMethod.ANY],
integration: httpApiIntegration,
});
new cloudfront.CloudFrontWebDistribution(this, "MyCf", {
defaultRootObject: "/",
originConfigs: [
{
customOriginSource: {
domainName: `${httpApi.httpApiId}.execute-api.${this.region}.${this.urlSuffix}`,
},
behaviors: [
{
isDefaultBehavior: true,
},
],
},
],
enableIpV6: true,
});
https://docs.aws.amazon.com/cdk/api/latest/docs/aws-apigatewayv2-readme.html脚步:
关于amazon-web-services - 从 apigateway 迁移到 apigatewayv2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60250370/