Reputation: 844
I am using the serverless framework to deploy some REST APIs. Earlier I was using serverless.yml and now migrating to the typescript version. I am having some AWS keys which I would like to keep in env files and not add directly. Earlier I was adding them directly.
I am trying to do something like the below:
export const serverless = {
frameworkVersion: '3',
useDotenv : true,
plugins: ['serverless-webpack', 'serverless-offline'],
provider: {
name: 'aws',
runtime: 'nodejs14.x',
stage: process.env.stage, // check this
profile: process.env.stage === 'dev ? 'dev' : 'prod', // check this
region: process.env.region,
},
};
this is not working. I tried logging the env variables and above object but it's coming as empty. I have already tried using this plugin- https://github.com/neverendingqs/serverless-dotenv-plugin
it seems that, env variables are loaded after serverless.ts file.
should I stick to using - ${self:custom.stage}
syntax. I was trying to avoid this. or Is it possible to use process.env?
Edit:
when trying to use syntax - ${env:profile}
Cannot resolve variable at "provider.profile": Value not found at "env" source
Upvotes: 1
Views: 2968
Reputation: 11
To access env variables in serverless configuration use ${env:VARIABLE_NAME}. For the stage: for me ${opt:stage} works when running sls --stage STAGE_NAME
.
In your case:
export const serverless = {
frameworkVersion: '3',
useDotenv : true,
plugins: ['serverless-webpack', 'serverless-offline'],
provider: {
name: 'aws',
runtime: 'nodejs14.x',
stage: '${opt:stage}',
profile: ${opt:stage, 'prod'}, // if no --stage default to 'prod'
region: '${env:region}',
},
};
Upvotes: 1