在 AWS SAM 中使用 !Ref 设置环境变量?
Setting environmental variables with !Ref in AWS SAM?
我正在使用 SAM CLI v0.8.1。我试图在我的资源 (MyTableResource) 中将环境变量 MY_TABLE_VAR 设置为 table 的名称。但是,虽然 运行 我的应用程序在本地,但 MY_TABLE_VAR 是未定义的。你能告诉我我的模板有什么问题吗?我该如何正确设置它?以下是我的 SAM 模板:
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
部分中的资源(依赖关系在另一个方向,因为添加到 Globals
部分的任何内容都已添加to all Serverless Functions and APIs in the Resources
section). To work around this, I suggest that you use either Mappings or Parameters,例如
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....
我正在使用 SAM CLI v0.8.1。我试图在我的资源 (MyTableResource) 中将环境变量 MY_TABLE_VAR 设置为 table 的名称。但是,虽然 运行 我的应用程序在本地,但 MY_TABLE_VAR 是未定义的。你能告诉我我的模板有什么问题吗?我该如何正确设置它?以下是我的 SAM 模板:
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
部分中的资源(依赖关系在另一个方向,因为添加到 Globals
部分的任何内容都已添加to all Serverless Functions and APIs in the Resources
section). To work around this, I suggest that you use either Mappings or Parameters,例如
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....