Different vpc per stage

hi,

i am trying to setup vpc per stage (dev, staging, prod) and tried this:

vpc:
subnetIds:
  - ${self:custom.subnets.${self:provider.stage}.subnet1}
  - ${self:custom.subnets.${self:provider.stage}.subnet2}

and in the custom sections

subnets:
dev:
staging:
  subnet1: subnet-a29a5dd5
  subnet2: subnet-06dbc140
prod:

the problem is that dev does not need subnets and prod might have a different number of them.
any idea how to get around this?

The way I just resolved this myself was by using Javascript based variables.
In my serverless config I have the following
vpc: securityGroupIds: ${file(config/${opt:region,self:provider.region}/vpc.js):security} subnetIds: ${file(config/${opt:region,self:provider.region}/vpc.js):subnets}
I then created a file (vpc.js) within the folders config/(us-east-1/eu-central-1/etc.)
The js file contents look like this:

module.exports.subnets = () =>{
    return ['subnet-XXXX','subnet-XXXX']
};

module.exports.security = () =>{
    return ['sg-XXXX']
};

Hope that helps!

3 Likes

worked like a charm, thanks!