Access nested variable in env.yml

Hi All,

I’m trying to understand how to access nested variable from an external file, see below the env.yml file.
Now, I ref my file like “custom: ${file(./serverless.env.yaml)}” and if I try “${self:custom.stageName}” it works, but what if I want to read stageName under vars.stages.dev.vars.stageName? I tried with ${self:custom.vars.stages.dev.vars.stageName} but I get the below error.

Any help is much appreciated as this scenario does not seem to be documented

First, stages isn’t under vars so it would be ${self:custom.stages.dev.vars.stageName}. If you want stages under vars you need to indent it.

I find it easier to include the file as ${file(env.yml):${self:custom.stage}} so the only place I need to muck around with the stage (stage & region in your case) is during the initial import. This topic comes up so frequently I wrote about blog post about using environment variables that covers this.

1 Like