You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在CDK TypeScript中为已有Lambda配置预留并发(不生成新版本)

解决CDK配置Lambda预留并发不生成新版本的问题

你当前使用currentVersionOptions的写法会自动创建Lambda新版本并绑定预留并发,这就是为什么会生成你不需要的版本。要给已有Lambda配置预留并发且不生成新版本,可通过以下两种场景处理:

场景1:配置非CDK创建的已有Lambda

先导入目标Lambda函数,再创建ProvisionedConcurrencyConfig资源绑定到指定版本或别名:

// 导入已有Lambda(替换为你的函数ARN)
const existingLambda = lambda.Function.fromFunctionArn(this, 'ExistingLambda', 'arn:aws:lambda:us-east-1:123456789012:function:my-existing-lambda');

// 给函数的当前版本配置3个预留并发
new lambda.ProvisionedConcurrencyConfig(this, 'LambdaProvisionedConcurrency', {
  function: existingLambda,
  provisionedConcurrentExecutions: 3,
  version: existingLambda.currentVersion // 绑定到当前版本,不会生成新版本
});

场景2:配置CDK中已定义的Lambda

如果是CDK项目里已存在的Lambda函数,直接新增ProvisionedConcurrencyConfig资源即可,无需修改原Function定义:

// 原有的Lambda定义(无需添加currentVersionOptions)
const myLambda = new lambda.Function(this, 'lambda1', {
  code: new lambda.InlineCode('exports.handler = async () => { console.log(\'hello world\'); };'),
  handler: 'index.handler',
  runtime: lambda.Runtime.NODEJS_10_X
});

// 为该Lambda的当前版本配置预留并发
new lambda.ProvisionedConcurrencyConfig(this, 'LambdaProvisionedConcurrency', {
  function: myLambda,
  provisionedConcurrentExecutions: 3,
  version: myLambda.currentVersion
});

关键说明

  • currentVersionOptions的设计逻辑就是自动生成新版本并绑定配置,因此要避免用它来给已有Lambda加预留并发
  • 预留并发必须绑定到特定版本或别名,如果需要后续自动同步到最新版本,可创建指向$LATEST的别名,再给别名配置预留并发:
    const currentAlias = new lambda.Alias(this, 'LambdaCurrentAlias', {
      aliasName: 'current',
      version: myLambda.currentVersion // 指向$LATEST版本
    });
    
    new lambda.ProvisionedConcurrencyConfig(this, 'AliasProvisionedConcurrency', {
      function: currentAlias,
      provisionedConcurrentExecutions: 3
    });
    

内容的提问来源于stack exchange,提问作者user13000875

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 05:03:16