How to access the output of a CF stack as variables in the serverless.yml file?


  • What is the correct way to get the output of a cloudformation stack in a serverless.yml file without hardcoding the stack name?


I have a serverless.yml file where I import a cloudformation template to create an ElastiCache cluster.

When I try to do so, I get this error:

  Serverless Error ---------------------------------------
  Invalid variable reference syntax for variable AWS::StackName. You can only reference env vars, options, & files. You can check our docs for more info.

In my file Iā€™d like to expose as an environment variable the ElastiCacheAddress output from the cloudformation stack. I am using the serverless pseudo-parameters plugin to get the output:

# Here is where I try to reference the CF output value
service: hello-world

  name: aws
  # ...
    cacheUrl: ${cf:#{AWS::StackName}.ElastiCacheAddress}

# Reference to the CF template
  - '${file(./cf/cf-elasticache.yml)}'

The CF template is the one from the AWS Samples GitHub repository.

The snippet with the output is here:

    Description: ElastiCache endpoint address
    Value: !If [ IsRedis, !GetAtt ElastiCacheCluster.RedisEndpoint.Address, !GetAtt ElastiCacheCluster.ConfigurationEndpoint.Address]
      Name: !Sub ${AWS::StackName}-ElastiCacheAddress
1 Like