2
votes

I have a sam/cloudformation template I am using to manage my stack. I use the same sam/cloudformation template across multiple stacks.

For example I have a dev stack and a prod stack. We have git and CI that updates the corresponding stack based on the branch.

My question is, how can I tell what stack/context my lambda function is running in? This would be useful for configuring DB endpoints.

I know I can setup Environment Variables through the console, but I would like to keep this in the template as much as possible. I don't see a good way to incorporate this into the template.

1
It's not clear what you mean by "my Lambda function". Obviously you can see all the resources including the Lambda function by reviewing the resources section in Cloudformation.Alex Harvey
@AlexHarvey I suppose I meant how does my code know which lambda function it is running in? Both stacks use the same codeTemporaryFix

1 Answers

3
votes

The way I do it is to pass in the stack name to the Lambda functions as an ENV var this way:

Globals:
  Function:
    Environment:
      Variables:
        STACK_NAME: !Ref AWS::StackName

It uses SAM's Globals section (so the Environment is set on all Lambda's that are in the template), combined with Cloudformation's Pseudoparameters.