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

Serverless部署报错:无法解析serverless.yml中TableName变量

解决Serverless部署时TableName变量解析失败的问题

你的错误根源是配置层级错误——region、iam、environment这三个配置块都放在了provider块的外部,导致Serverless框架无法通过self:provider.environment.CLIENTS_TABLE_NAME路径找到对应的变量值。

修复步骤

将region、iam、environment全部缩进,纳入provider块内部,修正后的完整配置如下:

# Welcome to Serverless!
#
# This file is the main config file for your service.
# It's very minimal at this point and uses default values.
# You can always add more config options for more control.
# We've included some commented out config examples here.
# Just uncomment any of them to get that config option.
#
# For full config options, check the docs:
#    docs.serverless.com
#
# Happy Coding!

service: auto-garden-server
# app and org for use with dashboard.serverless.com
#app: your-app-name
#org: your-org-name

# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
frameworkVersion: "3"

provider:
  name: aws
  runtime: nodejs12.x
  lambdaHashingVersion: 20201221
  stage: ${opt:stage, 'dev'}
  region: ca-central-1
  iam:
    role:
      statements:
        - Effect: Allow
          Action:
            - "dynamodb:PutItem"
            - "dynamodb:DeleteItem"
            - "dynamodb:Scan"
          Resource: 
            - { "Fn::GetAtt": ["ClientsTable", "Arn"] }
  environment:
    CLIENTS_TABLE_NAME: ${self:provider.stage}WSClients
    WSSAPIGATEWAYENPOINT:
      Fn::Join:
        - ""
        - - "https://"
          - Ref: WebsocketsApi
          - ".execute-api"
          - Ref: AWS::Region
          - ".amazonaws.com/${sls:stage}"

functions:
  websocketHandler:
    handler: src/handlers.handle
    events:
      - websocket:
          route: $connect
      - websocket:
          route: $disconnect
      - websocket:
          route: $msg

plugins:
  - serverless-plugin-typescript

resources:
  Resources:
    ClientsTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: ${self:provider.environment.CLIENTS_TABLE_NAME}
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        AttributeDefinitions:
          - AttributeName: connectionId
            AttributeType: S
        KeySchema:
          - AttributeName: connectionId
            KeyType: HASH

额外排查建议

  • 执行serverless print命令,预览解析后的完整配置,提前确认变量是否能正常解析
  • 检查package.json,确保serverless-plugin-typescript插件已正确安装
  • 确认你的Serverless框架版本(v3)和教程使用的版本一致,避免版本差异导致的配置兼容问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 23:21:02