我收到错误:“无法解析 serverless.yml:变量解析出错:
我希望有人能为我指出正确的方向,因为我不确定为什么无法解析 TableName 属性。
谢谢,
泰特美术馆
# 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
我正在学习教程,但不确定如何处理此错误。我仔细检查了语法。
您的环境部分似乎位于错误的位置。 它应该如下所示:
provider:
name: aws
runtime: nodejs12.x
lambdaHashingVersion: 20201221
stage: ${opt:stage, 'dev'}
environment:
CLIENTS_TABLE_NAME: ${self:provider.stage}WSClients
WSSAPIGATEWAYENPOINT:
Fn::Join:
- ""
- - "https://"
- Ref: WebsocketsApi
- ".execute-api"
- Ref: AWS::Region
- ".amazonaws.com/${sls:stage}"
这将修复您的错误,并自动将此环境变量包含在您的所有 lambda 函数中。