如何正确配置lambda函数PROFISIONEDCONCURRENTEXECTICTS?

发布于 2025-02-11 10:28:06 字数 2017 浏览 1 评论 0原文

我具有与API网关集成的Lambda函数。我正在尝试为该功能配置provisionedConcurrentExecutions而不会成功。云手表包含以下日志记录:

报告请求ID:6D818A07-CEE8-41F8-AEDC-9DF9B2AA1A75持续时间: 4276.27 MS计费持续时间:4277 MS内存尺寸:256 MB最大内存:107 MB Init持续时间:3536.87 MS

如我所知,如果provisionedconcurrentexections是正确配置的不在那里。

我是唯一打电话给该功能的人,因此只有一个并发请求。

我正在使用AWS CDK配置API GATWAY和功能。这是代码:

import { Duration, Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as lambda from 'aws-cdk-lib/aws-lambda'
import { LambdaIntegration, RestApi } from 'aws-cdk-lib/aws-apigateway';



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

    const api = new RestApi(this, 'OpenDataApi', {
        restApiName: `REST API`
    });
    this.configureLambda(api, "MyLambdaFunction", "my-lambda-function");
  }

  private configureLambda(api: RestApi, methodName: string, path: string, memorySize: number = 256): void {
    const lambdaFunction = new lambda.Function(this, `${methodName}LambdaFunction`, {
        runtime: lambda.Runtime.DOTNET_6,
        handler: `ApiGatewayLambda::ApiGatewayLambda.Functions::${methodName}`,
        code: lambda.Code.fromAsset('ApiGatewayLambda'),
        timeout: Duration.seconds(30),
        memorySize: memorySize,
        reservedConcurrentExecutions: 4
    });

    const version = lambdaFunction.currentVersion;
    const alias = new lambda.Alias(this, `${methodName}Al`, {
      aliasName: `${methodName}Al`,
      version: version,
      provisionedConcurrentExecutions: 2
    });

    const lambdaFunctionIntegration = new LambdaIntegration(alias);
    const resource = api.root.addResource(path);
    resource.addMethod('POST', lambdaFunctionIntegration);
  }
}

我错过了什么吗?如何正确配置provisionedConcurrentExecutions

我正在使用Postman来调用API网关,第一个请求大约需要10秒,后续请求不到1秒钟。如果我等待一些时间,那又需要10秒。因此,出了问题,配置并发似乎并没有产生任何效果。

I have a lambda function integrated with API Gateway. I'm trying to configure provisionedConcurrentExecutions for that function without success. Cloud Watch contains the following log record:

REPORT RequestId: 6d818a07-cee8-41f8-aedc-9df9b2aa1a75 Duration:
4276.27 ms Billed Duration: 4277 ms Memory Size: 256 MB Max Memory Used: 107 MB Init Duration: 3536.87 ms

As I understand, if provisionedConcurrentExecutions is configured properly, Init Duration should not be there.

I'm the only person calling the function, so there is only one concurrent request.

I'm using AWS CDK to configure the API Gatway and the function. Here is the code:

import { Duration, Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as lambda from 'aws-cdk-lib/aws-lambda'
import { LambdaIntegration, RestApi } from 'aws-cdk-lib/aws-apigateway';



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

    const api = new RestApi(this, 'OpenDataApi', {
        restApiName: `REST API`
    });
    this.configureLambda(api, "MyLambdaFunction", "my-lambda-function");
  }

  private configureLambda(api: RestApi, methodName: string, path: string, memorySize: number = 256): void {
    const lambdaFunction = new lambda.Function(this, `${methodName}LambdaFunction`, {
        runtime: lambda.Runtime.DOTNET_6,
        handler: `ApiGatewayLambda::ApiGatewayLambda.Functions::${methodName}`,
        code: lambda.Code.fromAsset('ApiGatewayLambda'),
        timeout: Duration.seconds(30),
        memorySize: memorySize,
        reservedConcurrentExecutions: 4
    });

    const version = lambdaFunction.currentVersion;
    const alias = new lambda.Alias(this, `${methodName}Al`, {
      aliasName: `${methodName}Al`,
      version: version,
      provisionedConcurrentExecutions: 2
    });

    const lambdaFunctionIntegration = new LambdaIntegration(alias);
    const resource = api.root.addResource(path);
    resource.addMethod('POST', lambdaFunctionIntegration);
  }
}

Am I missing something? How to properly configure provisionedConcurrentExecutions?

I'm using PostMan to call API Gateway, and the first request takes about 10 seconds, subsequent requests are under 1 second. If I wait some time, then it takes 10 seconds again. So something is wrong, provisioned concurrency doesn't seem to produce any effect.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文