Input all cloudformation stack's output to environment variables

I knew i can use ${cf:another-service-dev.functionPrefix} to refer a variable (functionPrefix) from a cloudformation stack (another-service-dev) .

Are there any easy ways, similar to variables from a file, so I just assign the cloudformation to a variable, and I use all output variables directly?

Such as:

    stack: ${cf:another-service-dev}

So in lambda handers, I can directly use the output as:


Why do I need this feature? because there are a lot of outputs in my CFN stack, I don’t want to define them one by one.

Are you trying to get the cloudformation output of this service (i.e. the service of this serverless.yml file in which you are defining the environment variables)?

If so, I might have part of a solution with plugins, but for that you would have to be able to access output generated by a plugin from within serverless.yml. Unfortunately, I am not yet sure how to do that last part. I’ll see if I can figure this out.

Anyway, if you can somehow access plugin output from within serverless.yml, then in your plugin you could do:

const awsInfo = serverless.pluginManager.plugins.find(p => === 'AwsInfo')
const output = awsInfo.gatheredData.outputs
module.exports.output = output

Note that you would have to wait for the after:aws:info:gatherData lifecycle event to have occured, otherwise the cloudformation output data will not yet be available. To simplify this, you can also install the serverless-scriptable-plugin and then define a script instead of a new plugin to gather the output as such:

    # gather cloudformation output
    after:aws:info:gatherData: scripts/gather-cf-output.js

And then (somehow) you would have to in your serverless.yml:

  stack: ${gather-cf-output:output}

Of course, the last step doesn’t work. Perhaps someone else here can enlighten us if this last step can be achieved?

1 Like