question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Environment variables to identify Lambda runtime environment

See original GitHub issue

This is a followup question to the PR “Feature: Custom cloudformation resources” and perhaps related to the issue “switch server mode to proxies”.

The lambda submitted as a test case for that PR issued API calls to the EC2 service. Part of the fix required the lambda to specify the endpoint_url to any boto3 services it invoked, i.e.:

ec2 = boto3.client("ec2", endpoint_url="http://host.docker.internal:5000")

That lambda (or the test case) can be invoked in different environmentss:

  • against AWS itself (no endpoint_url value needed),
  • when LocalStack and moto are started up from the command line (the endpoint_url of “http://host.docker.internal:5000” works great here),
  • when LocalStack and moto are started within a docker container and then start up the lambda within that container.

I have not been able to figure out the appropriate endpoint_url for the last case. I also don’t have a way to determine which of the three situations I’m in. If I relied on LocalStack, I could use the environment variable LOCALSTACK_HOSTNAME to identify when I’m running against AWS versus localstack. However, if I only use moto endpoints, I don’t have an equivalent environment variable.

Since my test case uses Terraform, I’ve tried passing variables declared in the Terraform modules, but I still can’t distinguish between the three scenarios and I’m trying to avoid hardcoding the moto port.

Do you have any suggestions as to how I might solve this problem? I could live with hardcoding the moto port if I knew the appropriate endpoint for the third case above and if I had a way of knowing which endpoint I should be using.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:31 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
bblommerscommented, Jan 27, 2022

This is now all available as of moto >= 3.0.2.dev7. The documentation on all of the configuration options can be found here: http://docs.getmoto.org/en/latest/docs/services/lambda.html

0reactions
lorengordoncommented, Jan 27, 2022

Amazing, this is an awesome feature! Thanks @bblommers!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Modifying the runtime environment - AWS Lambda
Lambda provides language-specific environment variables that you can set to add options and tools to the runtime. Lambda also provides wrapper scripts, ...
Read more >
Guide to default AWS Lambda environment variables
AWS Lambda runtimes set a number of default environment variables during initialization of every serverless function.
Read more >
AWS Lambda Environment Variables Best Practices
According to the official AWS Lambda documentation, environment variables are a tool that allows you to adjust your function's behavior ...
Read more >
Using Lambda environment variables - Road to AWS
Lambda environment variables are a key-pair of strings that are stored in a function's version-specific configuration. The latter is important ...
Read more >
How to detect if I'm running in AWS Lambda environment?
If you want to detect whether or not your code is running in local Lambda (aws-sam-cli) or real Lambda, there's the AWS_SAM_LOCAL environment...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found