'In serverless(aws), how to get variable reference from serverless.yml file?

In Serverless.yml, I defined resource:

provider:
  name: aws
  runtime: nodejs6.10
  region: us-east-1
  stage: dev
  environment:
    customerDef: myvariable
resources:
  Resources:
    NewResource:
     Type: AWS::S3::Bucket
     Properties:
      BucketName: ${self:service.name}-${self:provider.stage}-uploads

while in handler.js file which is write handle function.

How to get the reference of BucketName?

How to get the Bucket URI?

How to get the customerDef variable value? (provider->environment->customerDef)



Solution 1:[1]

All the environment variables defined under the environment node are available at any .js file using process.env.<variable_name>.

In your case, to access the customerDef variable you should use process.env.customerDef.

You can do the same with the BucketName and Bucket URI.

Solution 2:[2]

If you have your variables in environment key, you can references them by process.env.yourVariable

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Henrique Fuschini
Solution 2 Enzo Fabricio Olrtegui Vidales