【问题标题】:Unpredictable Cloudfront 502 errors不可预测的 Cloudfront 502 错误
【发布时间】:2021-11-10 13:12:22
【问题描述】:

我有一个涉及到的 AWS 设置

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

配置代码使用 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 函数是否有任何可能与 CloudFront 问题相关的错误?
  • 是的。没什么:(
  • 您是否打开了 API Gateway 日志记录?你可能会在那里找到一些线索
  • 如果您从 Lambda 中删除所有逻辑并返回 ack/200 所有请求会发生什么?我很好奇你是否正在吃 Lambda 起源中的问题。

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


【解决方案1】:

当我过去遇到此问题时,这是由于 lambda 超时造成的。默认超时值为 3 秒。尝试将您的函数增加到 30:

timeout: Duration.seconds(30)

真的很难说这是问题所在,因为 lambda 日志不会出现错误,它只会显示 3000 billed for lambda 之类的信息。

【讨论】:

  • 这并不难说。如果发生这种情况,CloudWatch 日志中会出现文字错误:例如 Task timed out after 3.00 seconds
猜你喜欢
  • 2014-12-12
  • 2020-12-28
  • 2018-12-29
  • 2022-01-07
  • 2020-09-07
  • 1970-01-01
  • 2020-10-13
  • 1970-01-01
  • 2021-01-22
相关资源
最近更新 更多