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

使用AWS CLI部署Node.js应用至AWS Lambda Serverless遇GeneralServiceException

解决Serverless部署Lambda时的GeneralServiceException(403错误)

问题重现

部署Serverless应用时出现如下错误:

Deploying aws-demo to stage dev (us-east-1)
Warning: Not authorized to perform: lambda:GetFunction for at least one of the lambda functions. Deployment will not be skipped even if service files did not change.

× Stack aws-demo-dev failed to deploy (155s)
Environment: win32, node 14.15.5, framework 3.26.0, plugin 6.2.2, SDK 4.3.2
Credentials: Local, "default" profile
Docs:        docs.serverless.com
Support:     forum.serverless.com
Bugs:        github.com/serverless/serverless/issues

Error:
CREATE_FAILED: HelloLambdaFunction (AWS::Lambda::Function)
Resource handler returned message: "null (Service: Lambda, Status Code: 403, Request ID: 0f60ffeb-add5-4571-856f-72a390ce5be9)" (RequestToken: beefe9c6-32d2-5d7f-f483-d97a6e76b73c, HandlerErrorCode: GeneralServiceException)

对应的serverless.yml配置:

service: aws-demo
frameworkVersion: '3'

provider:
  name: aws
  runtime: nodejs12.x
  memorySize: 512
  stage: dev
  timeout: 15
  region: us-east-1

functions:
  hello:
    handler: handler.hello
    events:
      - http: ANY /{proxy+}
      - http: ANY /

核心原因

当前使用的AWS本地凭证(default profile)权限不足,无法执行Lambda函数的创建、读取等操作,触发403权限错误。

解决方案

1. 补充IAM用户权限

给当前凭证对应的IAM用户添加必要的AWS权限:

  • 必备权限:
    • lambda:GetFunction:解决部署前的权限警告
    • lambda:CreateFunction:创建Lambda函数的核心权限
    • lambda:UpdateFunctionCode、lambda:UpdateFunctionConfiguration:支持后续代码更新
  • 因配置了API Gateway事件,还需添加API Gateway相关权限:
    • apigateway:CreateRestApi、apigateway:CreateDeployment、apigateway:UpdateRestApi等
  • 测试环境可临时附加AWSLambdaFullAccess和AmazonAPIGatewayAdministrator策略快速验证,生产环境建议遵循最小权限原则配置自定义策略。

2. 验证凭证有效性

执行以下命令确认当前凭证的身份信息:

aws sts get-caller-identity

若返回异常,重新配置AWS凭证:

aws configure

输入正确的Access Key ID、Secret Access Key、默认区域等信息。

3. 优化serverless.yml配置(可选)

本地Node版本为14.15.5,但配置中runtime为nodejs12.x,建议升级runtime以匹配本地环境,减少潜在兼容问题:

provider:
  # 其他配置不变
  runtime: nodejs14.x

4. 重新部署

先清理失败的云栈:

serverless remove

再执行部署命令:

serverless deploy

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 23:45:30