How can I add the dynamic stage name to cloudformation output keys with serverless framework

27 Views Asked by At

Problem: I want to deploy the same service in two different cloudformation stacks on the same aws account. This works great since the stacknames differ on the ${self:provider.stage} However, I had to add a cognito userpool with some exports to it like this:

Outputs:
  UserPool:
    Value:
      Ref: UserPool
  UserPoolId:
    Value:
      Ref: UserPool
    Export:
      Name: UserPoolId

However this does not work since Exports have to be unique across one region so when this export is already created by the other stack, the second stack will get an error while deploying.

Unfortunatly my attept to add the stage (and servicename) to the key like this:

Outputs:
  UserPool-${self:custom.serviceName}-${self:provider.stage}:
    Value:
      Ref: UserPool
  UserPoolId-${self:custom.serviceName}-${self:provider.stage}:
    Value:
      Ref: UserPool
    Export:
      Name: UserPoolId

results in the following error:

The CloudFormation template is invalid: Template format error: Outputs name 'UserPool-${self:custom.serviceName}-${self:provider.stage}' is non alphanumeric.

So how can I deploy the same service in two different stack with Exports?

1

There are 1 best solutions below

2
Marcin On

You can't dynamically create output keys. Thus in your case your keys are literal strings UserPool-${self:custom.serviceName}-${self:provider.stage} which leads to errors, because keys can only contain a–z, A–Z, 0–9 characters, not $, } or - ref. The references are not going to be resolved and you must explicity define them.