问题描述
我正在使用SAM CLI v0.8.1。我正在尝试将环境变量MY_TABLE_VAR设置为资源(MyTableResource)中表的名称。但是,在本地运行我的应用程序时,未定义MY_TABLE_VAR。您能告诉我模板有什么问题吗,如何正确设置呢?以下是我的SAM模板:
I'm using SAM CLI v0.8.1. I'm trying to set environmental variable MY_TABLE_VAR as name of the table in my resources (MyTableResource). However, while running my app locally, the MY_TABLE_VAR is undefined. Can you tell me what's wrong in my template and how can I set it properly? Following is my SAM template:
Globals:
Function:
Timeout: 30
Runtime: nodejs8.10
Environment:
Variables:
MY_TABLE_VAR: !Ref MyTableResource
Resources:
MyTableResource:
Type: AWS::Serverless::SimpleTable
Properties:
TableName: table1
PrimaryKey:
Name: id
Type: String
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
推荐答案
根据我的理解, Globals
部分无法引用 Resources
部分中的资源(依赖关系是相反的,因为在<$ c中添加了任何内容添加了$ c> Globals 部分中的所有无服务器功能和API 。要解决此问题,建议您使用或,例如
From my understanding, the Globals
section cannot reference resources in the Resources
section (the dependency is in the other direction, since whatever is added to the Globals
section is added to all Serverless Functions and APIs in the Resources
section). To work around this, I suggest that you use either Mappings or Parameters, e.g.
Parameters:
TableName:
Type: String
Default: table1
Globals:
Function:
Timeout: 30
Runtime: nodejs8.10
Environment:
Variables:
MY_TABLE_VAR: !Ref TableName
Resources:
MyTableResource:
Type: AWS::Serverless::SimpleTable
Properties:
TableName: !Ref TableName
# more table config....
这篇关于在AWS SAM中使用!Ref设置环境变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!