Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
serverless-export-env
Advanced tools
Serverless plugin to export environment variables into a .env file
The Serverless Framework offers a very powerful feature: You are able to reference AWS resources anywhere from within your serverless.yml
and it will automatically resolve them to their respective values during deployment. However, this only works properly once your code is deployed to AWS. The Serverless Export Env Plugin extends the Serverless Framework's built-in variable resultion capabilities by adding support many additional CloudFormation intrinsic functions (Fn::GetAtt
, Fn::Join
, Fn::Sub
, etc.) as well as variable references (AWS::Region
, AWS::StackId
, etc.).
The Serverless Export Env Plugin helps solve two main use cases:
sls invoke local
and sls offline start
(see Serverless Offline Plugin) and help resolve your environment variables. This is fully transparent to your application and other plugins.sls export-env
from command line to generate a .env
file on your local filesystem. Then use a library such as dotenv to import it into your code, e.g. during local integration tests.Add the npm package to your project:
# Via yarn
$ yarn add arabold/serverless-export-env --dev
# Via npm
$ npm install arabold/serverless-export-env --save-dev
Add the plugin to your serverless.yml
. It should be the first to ensure it can resolve your environment variables before other plugins see them:
plugins:
- serverless-export-env
That's it! You can now call sls export-env
in your terminal to generate the .env
file based on your Serverless configuration. Or, you can just run sls invoke local -f FUNCTION
or sls offline start
to run your code locally as usual.
serverless export-env
This will export all global environment variables into a .env
file in your project root folder.
serverless export-env --function MyFunction --filename .env-MyFunction
This will export all environment variables of the MyFunction
Lambda function into a .env-MyFunction
file in your project root folder.
The plugin supports various configuration options under custom.export-env
in your serverless.yml
file:
custom:
export-env:
filename: .env
overwrite: false
enableOffline: true
Option | Default | Description |
---|---|---|
filename | .env | Target file name where to write the environment variables to, relative to the project root. |
enableOffline | true | Evaluate the environment variables when running sls invoke local or sls offline start . |
overwrite | false | Overwrite the file even if it exists already. |
refMap | {} | A mapping of resource resolutions for the Ref function |
getAttMap | {} | A mapping of resource resolutions for the Fn::GetAtt function |
importValueMap | {} | A mapping of resource resolutions for the Fn::ImportValue function |
The Serverless Framework offers a very powerful feature: You are able to reference AWS resources anywhere from within your serverless.yml
and it will automatically resolve them to their respective values during deployment. A common example is to bind a DynamoDB table name to an environment variable, so you can access it in your Lambda function implementation later:
provider:
environment:
TABLE_NAME:
Ref: MyDynamoDbTable
# ...
resources:
Resources:
MyDynamoDbTable:
Type: AWS::DynamoDB::Table
DeletionPolicy: Retain
Properties:
# ...
Later in your code you can simply access process.env.TABLE_NAME
to get the proper DynamoDB table name without having to hardcode anything.
const AWS = require("aws-sdk");
const docClient = new AWS.DynamoDB.DocumentClient({
/* ... */
});
docClient.get(
{
TableName: process.env.TABLE_NAME,
Key: { foo: "bar" },
},
(result) => {
console.log(result);
}
);
So far, all this works out of the box when being deployed, without the need for this plugin. However, if you try to run the above code locally using sls invoke local
or sls offline start
, the TABLE_NAME
environment variable will not be initialized properly. This is where the Serverless Export Env Plugin comes in. It automatically resolves intrinsic functions like Ref
and Fn::ImportValue
and initializes your local environment properly.
Fn::And
Fn::Equals
Fn::If
Fn::Not
Fn::Or
Fn::FindInMap
Fn::GetAtt
Fn::GetAZs
Fn::Join
Fn::Select
Fn::Split
Fn::Sub
(at the moment only key-value map subtitution is supported)Fn::ImportValue
Ref
Example:
provider:
environment:
S3_BUCKET_URL:
Fn::Join:
- ""
- - https://s3.amazonaws.com/
- Ref: MyBucket
Or the short version:
provider:
environment:
S3_BUCKET_URL: !Join ["", [https://s3.amazonaws.com/, Ref: MyBucket]]
Note that sls invoke local
will report an error if you're using any function other than Fn::ImportValue
or Ref
in your environment variables. This is a Serverless limitation and cannot be fixed by this plugin unfortunately.
The plugin will try its best to resolve resource references like Ref
, Fn::GetAtt
, and Fn::ImportValue
for you. However, in sometimes this might fail or you might want to use mocked values instead. In those cases, you can override those values using the refMap
, getAttMap
and importValueMap
options.
refMap
takes a mapping of resource name to value pairs.getAttMap
takes a mapping of resource name to attribute/value pairs.importValueMap
takes a mapping of import name to value pairs.custom:
export-env:
refMap:
MyDbTable: "mock-myTable"
getAttMap:
ElasticSearchInstance:
DomainEndpoint: "localhost:9200"
importValueMap:
OtherLambdaFunction: "arn:aws:lambda:us-east-2::function:other-lambda-function"
Running sls export-env
will by default only export global environment variables into your .env
file (those defined under provider.environment
in your serverless.yml
). If you want to generate the .env
file for a specific function, pass the function name as a command line argument as follows:
sls export-env --function hello --filename .env-hello
Option | Description |
---|---|
filename | Target file name where to write the environment variables to, relative to the project root. |
overwrite | Overwrite the file even if it exists already. |
function | Name of a function for which to generate the .env file for. |
export-env:collect
- Collect environment variables from Serverlessexport-env:resolve
- Resolve CloudFormation references and import variablesexport-env:apply
- Set environment variables when testing Lambda functions locallyexport-env:write
- Write environment variables to filesls invoke local
or sls offline start
will no longer create or update your .env
file. If you want to create an .env
file, simply run sls export-env
instead..env
file. To enable overwriting existing files either specific --overwrite
in the command line or set the custom.export-env.overwrite
configuration option.Outputs
values (resources.Resources.Outputs.*
) are no longer exported automatically. This has always been a workaround and causes more problems than it actually solved. The plugin will try its best to resolve Fn::GetAtt
and other references for you now, so there should be little need for the old behavior anymore. Add the desired value new environment variable to provider.environment
instead.sls export-env
will no longer merge the environment variables of all functions into a single .env
file. Instead pass the name of the desired function as --function
argument to the command line. If no function is specified, only global environment variables will be exported.filename
and pathFromRoot
have been merged to filename
now. You can specify relative paths in filename
now such as ./dist/.env
. Make sure the target folder exists!Ref
and Fn::ImportValue
as in previous releases, but we're able to resolve most commonly used intrinstic functions automatically now.sls invoke local
)Outputs
as environment variables. Thanks to lielran..env
fileAWS::AccountId
being resolved as [Object Promise]
instead of the actual value.Fn::Join
operation (contribution by @jonasho)AWS::Region
, AWS::AccountId
, AWS::StackId
and AWS::StackName
.sls invoke local -f FUNCTION
). This allows seamless as if the function would be deployed on AWS.FAQs
Serverless plugin to export environment variables into a .env file
The npm package serverless-export-env receives a total of 9,168 weekly downloads. As such, serverless-export-env popularity was classified as popular.
We found that serverless-export-env demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.