lambci / docker-lambda

Docker images and test runners that replicate the live AWS Lambda environment
MIT License
5.83k stars 431 forks source link

Python - Are Arbitrary Environment Variables Supported? #295

Closed mdhornet90 closed 4 years ago

mdhornet90 commented 4 years ago

I've been at this all day so I could very well be missing something obvious, but are environment variables not listed in the README supported? I'm trying to test a lambda that has extra environment variables but they never show up in the environment when the lambda is run in "stay open" mode.

mhart commented 4 years ago

Can you give me an example of how you're invoking docker-lambda?

mdhornet90 commented 4 years ago

Sure, thanks for the prompt response (Python, file called main.py and function is handle):

docker run --rm \
  -e RANDOM=environment \
  -e VARIABLES=needed \
  -e FOR=the \
  -e LAMBDA=to \
  -e FUNCTION=correctly \
  -e DOCKER_LAMBDA_STAY_OPEN=1 \
  -v $PWD:/var/task \
  -p 9001:9001 \
  lambci/lambda:python3.6 \
  main.handle

Printing out os.environ this is what I get:

{
   "PATH":"/var/lang/bin:/usr/local/bin:/usr/bin/:/bin:/opt/bin",
   "HOSTNAME":"846648d22547",
   "DOCKER_LAMBDA_STAY_OPEN":"1",
   "LD_LIBRARY_PATH":"/var/lang/lib:/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib:/opt/lib",
   "LANG":"en_US.UTF-8",
   "TZ":":UTC",
   "LAMBDA_TASK_ROOT":"/var/task",
   "LAMBDA_RUNTIME_DIR":"/var/runtime",
   "_LAMBDA_CONTROL_SOCKET":"14",
   "_LAMBDA_SHARED_MEM_FD":"11",
   "_LAMBDA_LOG_FD":"9",
   "_LAMBDA_SB_ID":"7",
   "_LAMBDA_CONSOLE_SOCKET":"16",
   "_LAMBDA_RUNTIME_LOAD_TIME":"1530232235231",
   "_AWS_XRAY_DAEMON_ADDRESS":"169.254.79.2",
   "_AWS_XRAY_DAEMON_PORT":"2000",
   "AWS_XRAY_DAEMON_ADDRESS":"169.254.79.2:2000",
   "AWS_XRAY_CONTEXT_MISSING":"LOG_ERROR",
   "_X_AMZN_TRACE_ID":"Root=1-dc99d00f-c079a84d433534434534ef0d;Parent=91ed514f1e5c03b2;Sampled=1",
   "AWS_EXECUTION_ENV":"AWS_Lambda_python3.7",
   "HOME":"/home/sbx_user1051",
   "_HANDLER":"main.handle",
   "AWS_LAMBDA_FUNCTION_NAME":"test",
   "AWS_LAMBDA_FUNCTION_VERSION":"$LATEST",
   "AWS_LAMBDA_FUNCTION_MEMORY_SIZE":"1536",
   "AWS_LAMBDA_LOG_GROUP_NAME":"/aws/lambda/test",
   "AWS_LAMBDA_LOG_STREAM_NAME":"2020/07/16/[$LATEST]84344ccea2b91d9931e3220b0f52dec8",
   "AWS_REGION":"us-east-1",
   "AWS_DEFAULT_REGION":"us-east-1",
   "AWS_ACCESS_KEY_ID":"SOME_ACCESS_KEY_ID",
   "AWS_SECRET_ACCESS_KEY":"SOME_SECRET_ACCESS_KEY"
}
mhart commented 4 years ago

Hmmm, I'd expect that to work. Rather strange that DOCKER_LAMBDA_STAY_OPEN=1 is there but the others aren't. Let me dig into it

mhart commented 4 years ago

I can't reproduce this.

import os

def lambda_handler(event, context):
    return os.environ['SOME']
docker run --rm -v "$PWD":/var/task -e SOME=THING -e DOCKER_LAMBDA_STAY_OPEN=1 -p 9001:9001 lambci/lambda:python3.6
$ curl -d '{}' http://localhost:9001/2015-03-31/functions/myfunction/invocations
"THING"
mdhornet90 commented 4 years ago

Ugh, problem is on my end. Like I said - it's been a long day and it turned out I was running an outdated script that didn't contain those variables 🤦

Sorry to waste your time, thanks for investigating.

mhart commented 4 years ago

Np, glad you figured it out 👍