7

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
matsev
  • 32,104
  • 16
  • 121
  • 156
Mahdi
  • 1,089
  • 1
  • 14
  • 27

2 Answers2

12

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 Resourcessection). 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....
matsev
  • 32,104
  • 16
  • 121
  • 156
  • 1
    This is incorrect, the `Globals` CAN reference resources in the `Resources` section, but (probably) not all types. I managed to declare a `AWS::Serverless::LayerVersion` resource object and set the `Layers` property in the `Globals: Function` scope, despite the layer being created inside the `Resources` section. – Samuel Prevost May 10 '19 at 13:52
0

It's been a while since this was asked, but I just found this question with a Google search. I wanted to use the name of a function as a parameter to all other functions in my template. If you override the environment variable on the source function, the circular dependency goes away:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Globals:
  Function:
    Environment:
      Variables:
        LOG_LEVEL: WARNING
        SPECIAL_FUNCTION: !GetAtt SpecialFunction.Arn

Resources:
  SpecialFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: functions/special_function
      Environment:
        Variables:
          SPECIAL_FUNCTION: Overriding global value to avoid circular dependency
      Handler: special_function.lambda_handler
      Runtime: python3.9
  OtherFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: functions/other_function
      Handler: other_function.lambda_handler
      Runtime: python3.9

OtherFunction will have the ARN of SpecialFunction in the SPECIAL_FUNCTION environment variable.

Carrot
  • 195
  • 2
  • 9