I have utilized the "aws-nodejs-typescript" template from "serverless" to create a serverless function. My goal is to employ separate .env files for the "dev" and "prod" stages. Is there a way to accomplish this?
Upon deployment, the correct stage is not obtained.
Any advice on how to resolve this issue would be greatly appreciated.
Below is my "serverless.ts" file.
import type { AWS } from '@serverless/typescript';
import {verify} from '@functions/order';
const serverlessConfiguration: AWS = {
service: 'sample-api-aws',
frameworkVersion: '3',
plugins: ['serverless-esbuild', 'serverless-offline'],
provider: {
name: 'aws',
runtime: 'nodejs14.x',
timeout: 300,
apiGateway: {
minimumCompressionSize: 1024,
shouldStartNameWithService: true,
},
environment: (() => {
const stage = this.provider.getStage();
console.log(`Environment is : `, stage);
switch (stage) {
case 'dev':
return require('./dev.env').default;
case 'prod':
return require('./prod.env').default;
default:
return {};
}
})(),
},
// import the function via paths
functions: { verify},
package: { individually: true },
custom: {
esbuild: {
bundle: true,
minify: false,
sourcemap: true,
exclude: ['aws-sdk'],
target: 'node14',
define: { 'require.resolve': undefined },
platform: 'node',
concurrency: 10,
},
},
};
module.exports = serverlessConfiguration;
Here's how I deploy this function in "dev" and in "prod"
Dev
serverless offline start --httpPort 3171 --lambdaPort 3172 --stage dev --noTimeout
Prod
serverless deploy -s prod --aws-profile my_profile
Note:
I am looking to provide an environment file instead of passing arguments via command-line during deployment.