gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:04:23 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com