amazon-web-services - 不可预测的 Cloudfront 502 错误

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

我有一个 AWS 设置,其中涉及

  • 也附加了一个自定义域名
  • 云前端分配
  • 一个API网关作为前面的源
  • 一些 lambda 函数可呈现由 https://www.npmjs.com/package/next-aws-lambda-webpack-plugin 生成的一些 nextJS 路由

配置代码使用 CDK。我试图删除所有不相关的内容,所以最小的例子(不幸的是不是那么小,对不起)在问题的底部。

问题是它似乎在大多数时间都有效。但我似乎变得无法预测“CloudFront 无法连接到源”错误,我真的不确定为什么。有 some documentation on this from aws但我不太清楚如何去验证它是哪一个。如果有人有任何建议,那将非常有帮助

import { Stack, StackProps } from "aws-cdk-lib";
import * as cdk from "aws-cdk-lib";
import { Cors, LambdaIntegration, RestApi } from "aws-cdk-lib/aws-apigateway";
import { Code, LayerVersion, Runtime, Function } from "aws-cdk-lib/aws-lambda";
import { DnsValidatedCertificate } from "aws-cdk-lib/lib/aws-certificatemanager";
import { Distribution, ViewerProtocolPolicy } from "aws-cdk-lib/lib/aws-cloudfront";
import { ARecord, PublicHostedZone, RecordTarget } from "aws-cdk-lib/lib/aws-route53";
import { CloudFrontTarget } from "aws-cdk-lib/lib/aws-route53-targets";
import { Construct } from "constructs";
import { HttpOrigin } from "aws-cdk-lib/lib/aws-cloudfront-origins";

interface Props {
  path1: string;
  path2: string;
  layerPath: string;
}

export class AppStack extends Stack {
  constructor(scope: Construct, id: string, props: StackProps & Props) {
    super(scope, id, props);

    const awsNextLayer = new LayerVersion(this, "aws-next-layer", {
      code: Code.fromAsset(props.layerPath),
    });

    const functionOne = new Function(this, `function-one`, {
      functionName: 'function-one',
      runtime: Runtime.NODEJS_14_X,
      handler: "page/handler.render",
      code: Code.fromAsset(props.path1),
      layers: [awsNextLayer],
    });

    const functionTwo = new Function(this, `function-one`, {
      functionName: 'function-one',
      runtime: Runtime.NODEJS_14_X,
      handler: "page/handler.render",
      code: Code.fromAsset(props.path1),
      layers: [awsNextLayer],
    });

    const api = new RestApi(this, "pages-api", {
      defaultCorsPreflightOptions: {
        allowOrigins: Cors.ALL_ORIGINS,
      },
      restApiName: `api`,
    });

    api.root.addResource('function-one').addMethod("GET", new LambdaIntegration(functionOne));
    api.root.addResource('function-two').addMethod("GET", new LambdaIntegration(functionTwo));

    const domainName = `dev.app.my-domain-name.com`

    const hostedZone = new PublicHostedZone(this, "HostedZone", {
      zoneName: domainName,
    });

    const certificate = new DnsValidatedCertificate(this, "cert", {
      domainName,
      hostedZone,
      region: "us-east-1",
    });

    const apiGatewayDomainName = `${api.restApiId}.execute-api.${cdk.Aws.REGION}.amazonaws.com`;

    const origin = new HttpOrigin(apiGatewayDomainName, { originPath: "/prod" });

    const distribution = new Distribution(this, "cdn", {
      domainNames: [domainName],
      defaultBehavior: {
        origin,
        viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
      },
      certificate,
    });

    new ARecord(this, "a-record", {
      zone: hostedZone,
      recordName: domainName,
      target: RecordTarget.fromAlias(new CloudFrontTarget(distribution)),
    });
  }

}

最佳答案

我过去遇到过这个问题,是因为 lambda 超时。默认超时值为 3 秒。尝试将函数增加到 30:

timeout: Duration.seconds(30)

很难说这是问题所在,因为 lambda 日志不会有错误,它只会说类似 3000 的 lambda 费用。

关于amazon-web-services - 不可预测的 Cloudfront 502 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69193242/

相关文章:

amazon-web-services - 使用 lambda 函数通过 spark 步骤创建 AWS EMR 集群失败并显示 "Local file does not exist"

java - 异步 Lambda 代理集成

typescript - 使用 AWS CDK 的 API 网关 GET 方法请求 URL 查询字符串参数

amazon-web-services - 具有相互身份验证的 AWS 安全 REST API

amazon-web-services - 自动部署 AWS API Gateway 阶段

amazon-web-services - 使用 Golang SDK 的 S3 对象不会过期

Amazon S3 中的 Tomcat/Apache Solr?那可能吗?

amazon-web-services - 使用具有严格白名单的公有子网与对 AWS 云资源使用私有(private)子网的缺点

amazon-web-services - 使用 Lambda 容器时,AWS Lambda 函数计费持续时间还包括初始化持续时间吗?

node.js - SES : Accessing email body inside lambda function