serverless-export-env
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "serverless-export-env", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Serverless plugin to export environment variables into a .env file", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -29,2 +29,57 @@ # ⚡️ Serverless Export Env Plugin | ||
## Why another plugin? | ||
There're plenty of environment and dotenv plugins available for Serverless. However, | ||
some are already obsolete, others are very limited in use case. We needed a possibility | ||
to access Serverless environment variables from command line during integration | ||
testing of our code. As some of these environment variables are referencing | ||
CloudFormation resources, none of the existing plugins was able to solve this. | ||
## Referencing CloudFormation resources | ||
Serverless offers a very powerful feature: You are able to reference AWS | ||
resources anywhere from within your `.yaml` 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: | ||
```yaml | ||
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. | ||
```js | ||
require("dotenv").config({ path: "../.env" /* path to your project root folder */ }); | ||
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); | ||
}); | ||
``` | ||
The _Serverless Export Env Plugin_ allows you to make use of these references | ||
(and all other environment variables of course) from the command line by | ||
exporting them into a `.env` file in your project folder. Then use a library | ||
such as [dotenv](https://www.npmjs.com/package/dotenv) to read them during | ||
runtime. | ||
## Usage | ||
@@ -36,3 +91,3 @@ | ||
# Via yarn | ||
$ yarn add arabold/serverless-export-env | ||
$ yarn add arabold/serverless-export-env --dev | ||
@@ -74,2 +129,6 @@ # Via npm | ||
### 1.0.1 | ||
* Corrected plugin naming | ||
* Improved documentation | ||
### 1.0.0 | ||
@@ -76,0 +135,0 @@ |
@@ -17,3 +17,3 @@ "use strict"; | ||
*/ | ||
class ServerlessDotenvPlugin { | ||
class ExportEnv { | ||
constructor(serverless, options) { | ||
@@ -48,5 +48,6 @@ this.serverless = serverless; | ||
initOfflineHook() { | ||
this.isOfflineHooked = true; | ||
this.serverless.pluginManager.run([ "export-env" ]); | ||
if (!this.isOfflineHooked) { | ||
this.isOfflineHooked = true; | ||
this.serverless.pluginManager.run([ "export-env" ]); | ||
} | ||
} | ||
@@ -97,2 +98,2 @@ | ||
module.exports = ServerlessDotenvPlugin; | ||
module.exports = ExportEnv; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13759
183
135