amazon-web-services - 从 apigateway 迁移到 apigatewayv2

标签 amazon-web-services aws-api-gateway amazon-cloudfront aws-cdk

如何使用 AWS-CDK 从 apigateway 迁移到 apigatewayv2?

具体来说:我正在使用 LambdaRestApirestApiIddeploymentStage从那个资源。

    // 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
脚步:
  • 创建集成(例如 lambda 函数),这来自专用包 (apigatewayv2-integrations)
  • 创建一个 HttpApi,不需要任何选项
  • 新:与 APIGWv1 相对,您必须为您的路径添加路由处理程序 (httpApi.addRoutes)。
  • Cloudfront 配置非常相似
  • 关于amazon-web-services - 从 apigateway 迁移到 apigatewayv2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60250370/

    相关文章:

    amazon-web-services - AWS CodeBuild- docker :找不到

    ios - AWSMobileHubHelper登录Google无法通过cocoa pods工作

    java - 使用 AWS Secrets Manager 管理 RDS 访问

    aws-api-gateway - REGIONAL 处于事件状态时无法导入 EDGE 的证书

    amazon-web-services - 使用 S3 存储桶为使用单个 Cloudfront 分发的多个网站提供服务

    amazon-web-services - 将 CloudFront 分配与 Route 53 域相关联时出现问题

    c++ - 如何将 aws-sdk-cpp 与 CMake 链接?

    amazon-web-services - 将 CloudFront 主机 header 转发到 API 网关

    amazon-web-services - 有没有办法在aws apigateway中调试映射模板

    node.js - 如何在 Lambda 函数中使用 AWS.CloudFront.Signer