arabold / serverless-export-env

Serverless plugin to export environment variables into a .env file
MIT License
102 stars 34 forks source link

Usage of AWS pseudo parameters and intrinsic functions in environment variables #31

Closed gerdzhikov closed 3 years ago

gerdzhikov commented 3 years ago

As of 1st, thanks a lot for this great plugin for the serverless framework.

Could you please provide an example in the README how we can use the supported AWS pseudo parameters as AWS::AccountId, AWS::NotificationARNs, AWS::NoValue, AWS::Partition, AWS::Region, AWS::StackId, AWS::StackName, AWS::URLSuffix, and maybe a list of all supported parameters?

Currently, the example describes the usage of the Ref intrinsic function and it works like expected. But I'm somehow not able to use neither pseudo parameters like AWS::AccountId and AWS::Region nor other intrinsic functions like Fn::Join and Fn::ImportValue

Following configuration in the serverless.yaml

provider:
  name: aws
  environment:
    JOIN_TEST:
      Join:
        - ''
        - - ${AWS::Region}
    IMPORT_TEST:
      ImportValue: my-elastic-ip-address
    APPLICATION_NAME_WITH_EMPTY_SPACE: 'My Application'

plugins:
  - ./vendor/bref/bref
  - serverless-export-env

generates with serverless export-env the following .envfile

JOIN_TEST=[object Object]
IMPORT_TEST=[object Object]
APPLICATION_NAME_WITH_EMPTY_SPACE=My Application

which has two issues

  1. The pseudo parameters and intrinsic functions are not resolved
  2. The application name My Application must be quoted because the empty space is not valid in the values of dotenv files.

I'm testing this on the following environment:

$ sls -v
Serverless: Running "serverless" installed locally (in service node_modules)
Framework Core: 2.22.0 (local)
Plugin: 4.4.3
SDK: 2.3.2
Components: 3.7.1
serverless-export-env: 1.4.0

Kind regards!

Update:

Following configuration exports the pseudo parameter and intrinsic functions values:

provider:
  name: aws
  environment:
    REGION_TEST:
      Ref: 'AWS::Region'
    JOIN_TEST:
      Fn::Join:
        - ''
        - - Ref: 'AWS::Region'
    IMPORT_TEST:
      Fn::ImportValue: my-public-ip-address
    APPLICATION_NAME_WITH_EMPTY_SPACE: 'My Application'

plugins:
  - ./vendor/bref/bref
  - serverless-export-env

Need to figure out how to escape values containing empty spaces

arabold commented 3 years ago

I'm currently working on a new v2.0.0. It has support for Fn::GetAtt and most other commonly used functions. It should also be possible to solve any remaining issues you might have in this task.

The version is not pushed to npm yet but a branch exits: https://github.com/arabold/serverless-export-env/tree/v2.0.0. I'm happy about any feedback before the official release.

arabold commented 3 years ago

To simplify testing the new version, I've published 2.0.0-alpha.0 to as alpha release to npm:

npm install serverless-export-env@2.0.0-alpha.0 --save-dev

I'm closing this task. Please open a new one (or comment on this one) if your problems are still not solved.