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

如何在Serverless中仅更新单个函数且不影响云栈?

问题描述

我有一个包含3个函数的serverless.yml文件,执行serverless deploy命令时会更新全部3个函数,耗时很长。我想只更新单个函数且不影响其余云栈,尝试用serverless deploy -f functionA命令,但它似乎还是会处理并上传其他函数,我的配置如下:

service: myrestapi
provider:
  name: aws
  region: 'us-east-1'
  runtime: nodejs14.x
functions:
  functionA:
    handler: functions/functionA.handler
    memorySize: 256
  functionB:
    handler: functions/functionB.handler
    memorySize: 128
  functionC:
    handler: functions/functionC.handler
    memorySize: 128
解决方案

要实现仅更新单个函数且完全跳过其他函数的处理,需要调整打包配置并使用正确的命令,具体步骤如下:

  • 开启函数单独打包
    在serverless.yml中添加package配置项,开启单个函数独立打包,这样每个函数只会打包自身相关代码,不会包含其他函数的文件:
service: myrestapi
provider:
  name: aws
  region: 'us-east-1'
  runtime: nodejs14.x
package:
  individually: true # 启用每个函数单独打包
functions:
  functionA:
    handler: functions/functionA.handler
    memorySize: 256
  functionB:
    handler: functions/functionB.handler
    memorySize: 128
  functionC:
    handler: functions/functionC.handler
    memorySize: 128
  • 执行单函数部署命令
    配置完成后,运行以下命令即可仅更新functionA,不会处理或上传其他函数:
serverless deploy function -f functionA

额外说明

  • 单独打包不仅能解决当前的部署效率问题,还能减小每个函数的部署包体积,间接提升函数冷启动速度。
  • 如果函数间有共享依赖或资源,可以在package根配置下用patterns定义全局包含规则,也可以在单个函数的配置里单独设置package.patterns来定制打包内容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 07:30:52