xMemz
xMemz

Reputation: 21

AWS SAM, reference function name in Globals section

I'm using a yaml AWS SAM file for CI/CD purposes.I need to add a new tag with the functionName as a value for that tag in each lambda function in that file.

Is it possible to do this without the need to define that tag separately in each function, knowing that I already have a functionName property already set for each lambda?

So Instead of this

 Resources:
  Function1:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: function1
      Tags:
        MyTag: function1         # << check this
  Function2:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: function2
      Tags:
        MyTag: function2        #  << check this

Im wondering if I can do this:

Globals:
  Function:
    Tags:
      MyTag: <FunctionName> # << reference to function name

Resources:
  Function1:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: function1
  Function2:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: function2

Upvotes: 0

Views: 911

Answers (1)

Asfar Irshad
Asfar Irshad

Reputation: 765

Two ways to do this, check which one works for you

  1. Using reference for instance in your case if you want to refer "Function1" you should use Ref: "Function1" or Ref: "Function1LambdaFunction" (incase its defined using function tag)

  2. Get Attribute function - for instance in your case it would be (you can replace "Function1" to "Function1LambdaFunction" if you are using a direct function tag

    - Fn::GetAtt: [Function1, FunctionName]

Upvotes: 0

Related Questions