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
相关产品推荐
相关产品推荐

