0

I have a Cloud9 environment spun up and have modified my ~/.bash_profile to export a value at the end of the file.

export foo="hello world"

I run . ~/.bash_profile and then echo $foo and I see hello world output in the terminal.

I then created a NodeJS Lambda with API Gateway. I run the API Gateway locally in Cloud 9 and attempt to read the environment variables

console.log(process.env)

I see a list of variables available to me that AWS has defined. My export is not listed there however. Since I will be using environment variables when my Lambda is deployed, I want to test it with environment variables defined in the Cloud9 environment.

Is there something specific I have to do in order to get the Lambda to read my .bash_profile exports?

Johnathon Sullinger
  • 7,097
  • 5
  • 37
  • 102

2 Answers2

0

AWS Cloud9's Lambda plugin is backed by SAM Local, which uses Docker: https://github.com/awslabs/aws-sam-cli . By default, this means that the ~/.bash_profile file is not used by Lambda; you'll want to load this in manually.

brycei
  • 125
  • 3
  • Are there examples some place on how to do that manually? I'm coming from a Windows world with Visual Studio, so Docker, Linux and SAM are unfamiliar to me. – Johnathon Sullinger Jul 05 '18 at 01:29
0

Please see Using the AWS Serverless Application Model (AWS SAM) article that describes how to work with environment variables in SAM (so also in cloud9).

In summary - put environment variables into the template.yaml file (present in the root folder of your app) like below:

Properties:
  .... # tons of other properties here, add yours at the end
  Environment:
    Variables:
      MY_ENV_VARIABLE: 'This is my awesome env variable value'
Lech Migdal
  • 3,828
  • 5
  • 36
  • 63
  • If this is the answer, it's a terrible answer. There must be a way to import the shell variables! For testing an app in a variety of environments, each of which have a set of environment variables, all of which can be scripted for command line work, requiring static yaml files to hold all this data for every new environment is cumbersome and foolish. – Blunt Jackson Nov 26 '22 at 21:42