Repository navigation
Closed
Labels
stage/pm-reviewWaiting for review by our Product Manager, please don't work on this yet
Description
Description:
I'm dockerising sam app. When accessing api endpoint for HelloWorld locally, it responds with No response from invoke container for HelloWorldFunction and Invalid lambda response received: Lambda response must be valid json.
It works fine whithout dockerisation.
Steps to reproduce:
Dockerfile
FROM ruby:2.7.0-alpine
RUN apk add --update --no-cache \
build-base \
postgresql-dev \
postgresql-client \
python3 \
py3-pip \
util-linux \
python3-dev
RUN pip3 install aws-sam-cli
ARG USER
ARG HOME
ARG UID
RUN apk add --update \
sudo
RUN echo "Welcome home: $USER => $UID"
RUN adduser -S -D -G users -u $UID $USER
RUN addgroup -S sudo
RUN echo "%sudo ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/sudo
RUN adduser $USER sudo
RUN echo "Welcome home: $USER"
WORKDIR ${HOME}
EXPOSE 3001
ENTRYPOINT ["sh", "./entrypoint.sh"]docker-compose.yml
version: '3.8'
services:
sam_app:
build:
context: ./sam-app
args:
- HOME
- USER
- UID
user: "${UID}:100"
command: ["$PWD"]
ports:
- "3001:3001"
volumes:
- ./sam-app:$HOME
- /var/run/docker.sock:/var/run/docker.sockEntrypoint.sh
BASEDIR="$1"
echo "Basedir => ${BASEDIR}"
sudo sam local start-api \
--template ./template.yaml \
--host 0.0.0.0 \
--port 3001 \
--docker-volume-basedir "${BASEDIR}/sam-app/" \
--docker-network drink_default \
--debughello_world/app.rb
def lambda_handler(event:, context:)
{
statusCode: 200,
body: {
message: "Hello World!",
# location: response.body
}.to_json
}
endObserved result:
am_app_1 | 2020-12-21 01:31:22,493 | Constructed String representation of Event to invoke Lambda. Event: {"body": null, "headers": {"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "en-GB,en;q=0.9", "Connection": "keep-alive", "Host": "localhost:3001", "Sec-Fetch-Dest": "document", "Sec-Fetch-Mode": "navigate", "Sec-Fetch-Site": "none", "Sec-Fetch-User": "?1", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36", "X-Forwarded-Port": "3001", "X-Forwarded-Proto": "http"}, "httpMethod": "GET", "isBase64Encoded": false, "multiValueHeaders": {"Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"], "Accept-Encoding": ["gzip, deflate, br"], "Accept-Language": ["en-GB,en;q=0.9"], "Connection": ["keep-alive"], "Host": ["localhost:3001"], "Sec-Fetch-Dest": ["document"], "Sec-Fetch-Mode": ["navigate"], "Sec-Fetch-Site": ["none"], "Sec-Fetch-User": ["?1"], "Upgrade-Insecure-Requests": ["1"], "User-Agent": ["Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36"], "X-Forwarded-Port": ["3001"], "X-Forwarded-Proto": ["http"]}, "multiValueQueryStringParameters": null, "path": "/hello", "pathParameters": null, "queryStringParameters": null, "requestContext": {"accountId": "123456789012", "apiId": "1234567890", "domainName": "localhost:3001", "extendedRequestId": null, "httpMethod": "GET", "identity": {"accountId": null, "apiKey": null, "caller": null, "cognitoAuthenticationProvider": null, "cognitoAuthenticationType": null, "cognitoIdentityPoolId": null, "sourceIp": "172.22.0.1", "user": null, "userAgent": "Custom User Agent String", "userArn": null}, "path": "/hello", "protocol": "HTTP/1.1", "requestId": "dd99d1bb-cc40-42f8-a2f2-eeefcc22d111", "requestTime": "21/Dec/2020:01:30:38 +0000", "requestTimeEpoch": 1608514238, "resourceId": "123456", "resourcePath": "/hello", "stage": "Prod"}, "resource": "/hello", "stageVariables": null, "version": "1.0"}
sam_app_1 | 2020-12-21 01:31:22,493 | Found one Lambda function with name 'HelloWorldFunction'
sam_app_1 | 2020-12-21 01:31:22,494 | Invoking app.lambda_handler (ruby2.7)
sam_app_1 | 2020-12-21 01:31:22,494 | No environment variables found for function 'HelloWorldFunction'
sam_app_1 | 2020-12-21 01:31:22,494 | Environment variables overrides data is standard format
sam_app_1 | 2020-12-21 01:31:22,494 | Loading AWS credentials from session with profile 'None'
sam_app_1 | 2020-12-21 01:31:24,549 | Resolving code path. Cwd=/home/sameer/projects/drink/sam-app/, CodeUri=hello_world/
sam_app_1 | 2020-12-21 01:31:24,549 | Resolved absolute path to code is /home/sameer/projects/drink/sam-app/hello_world
sam_app_1 | 2020-12-21 01:31:24,550 | Code /home/sameer/projects/drink/sam-app/hello_world is not a zip/jar file
sam_app_1 | 2020-12-21 01:31:24,574 | Skip pulling image and use local one: amazon/aws-sam-cli-emulation-image-ruby2.7:rapid-1.15.0.
sam_app_1 |
sam_app_1 | 2020-12-21 01:31:24,574 | Mounting /home/sameer/projects/drink/sam-app/hello_world as /var/task:ro,delegated inside runtime container
sam_app_1 | 2020-12-21 01:31:25,386 | Starting a timer for 3 seconds for function 'HelloWorldFunction'
sam_app_1 | 2020-12-21 01:31:26,707 | Cleaning all decompressed code dirs
sam_app_1 | 2020-12-21 01:31:26,707 | No response from invoke container for HelloWorldFunction
sam_app_1 | 2020-12-21 01:31:26,707 | Invalid lambda response received: Lambda response must be valid json
sam_app_1 | 2020-12-21 01:31:26 172.22.0.1 - - [21/Dec/2020 01:31:26] "GET /hello HTTP/1.1" 502 -
sam_app_1 | 2020-12-21 01:31:26 172.22.0.1 - - [21/Dec/2020 01:31:26] "GET /favicon.ico HTTP/1.1" 403 -
Expected result:
Json output on browser {message: "hello world"}
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
- OS: ubuntu VERSION="20.04.1 LTS (Focal Fossa)"
sam --version: 1.15.0Homebrew: VERSION="2.6.2"docker: VERSION="19.03.8"
Add --debug flag to command you are running
Reacted by Alonso A. Ortega, Vlad Momotov, Kareem March, Ilya Sotkov, patricknazar, hoffa, James Richards, Shintaro Anazawa, peter, Pero P. and 12 more
Metadata
Metadata
Assignees
Labels
stage/pm-reviewWaiting for review by our Product Manager, please don't work on this yet
Activity
jfuss commentedon Dec 21, 2020
@alliesground SAM CLI is meant to be run from your machine and interact with docker, not run SAM CLI within Docker. My suggestion here is to do that instead of Dockerize.
What's the use case on wanting to do this? Why do you need to be running docker within docker?
rbliss commentedon Dec 21, 2020
I also get this exact error. I will chime in on my use case which may or may not be similar:
In my case, I'm using VSCode's Dev Containers, to create an isolated dev environment SAM is running in. My setup is nearly identical, using a volume to expose the host's docker socket, and SAM's
--docker-volume-basedirflag to direct SAM to the appropriate location. Hitting the endpoint gives me the sameNo response from invoke container for HelloWorldLambdaUnfortunately, SAM's
--debugflag doesn't go into much detail about what went on with the lambda container it spins up to serve the request. I started tracing through the CLI source to see what happened, but haven't had time to finish my search.alliesground commentedon Dec 22, 2020
@jfuss my use case is similar to that of @rbliss i.e to create an isolated dev environment, which includes react-app and db, all composed with docker-compose.
alayor commentedon Dec 22, 2020
I'm also having the same problem. My use case is exactly the same as @alliesground.
I followed the instructions on this post. It makes me think this used to work before.
https://medium.com/monsoon-engineering/running-aws-sam-in-a-docker-container-2491596672c2
gpomykala commentedon Dec 22, 2020
I receive same error when trying to hit a lambda spun with 'sam local start-lambda --docker-network host'. It used to work with older version of SAM CLI. I am not doing anything extraordinary, just a basic integration test of a lambda client hitting lambda hosted in SAM CLI
jfuss commentedon Dec 22, 2020
@gpomykala Your issue is logged here: #2436 (comment) and workarounds here: #2436 (comment)
@rbliss @alayor @alliesground
We have seen this happen when SAM CLI cannot communicate to the container. Instead of exec-ing within the container, we updated to move to publishing a port and then calling into the container of http. This could be the cause here as well. SAM CLI will communicate to the container over localhost only, so there is probably some docker network trickery going on. I highly suggest to not use SAM CLI with the container like this. It's not really a use case we directly support or recommend.
alayor commentedon Dec 22, 2020
@jfuss Thank you for your response and links.
I wonder if there could be a way to change the "communicate to the container over localhost" setting.
I just wanted to test if changing "localhost" to "docker.for.mac.localhost" would make it work.
I know this is not recommended, but I figured I could ask anyway.
jfuss commentedon Dec 22, 2020
@alayor I am open to ways to make this work if we can find a solution that works across OS. I am not sure if moving to the
docker.for.mac.localhost(orhost.docker.internalwhich I think might work across OS).Just mapping this out:
sam localcommand will start up a container and publish ports to localhost (not sure if this is localhost of the machine or on the bridge docker network).If in step two, this does publish a port on the machines localhost, then I would assume what you are suggesting could work. Super hacky and seems much easier to just run the CLI on your machine and not have to care about docker at all. Could be a personal preference though.
alliesground commentedon Dec 23, 2020
@jfuss thanks for your response. It was just an itch of mine to try to dockerize sam-cli especially after reading some articles on the possibilities. Anyways I will move forward with your advice and run CLI on the host machine and maybe try to connect it with other tech stacks running in the container.
ilyasotkov commentedon Jan 8, 2021
I was able to get SAM local invoke to work inside Dev Container by replacing hardcoded
localhostherehttps://github.com/aws/aws-sam-cli/blob/develop/samcli/local/docker/container.py#L41
with
host.docker.internal. I used this guide for attaching the VS Code debugger: https://github.com/aws/aws-toolkit-vscode/blob/a380685696e39247d6ec0f8f1e9928ba2562f550/docs/debugging-python-lambda-functions.md, also replacinglocalhostwithhost.docker.internalinlaunch.json.@jfuss I'm running this on macOS, AFAIK this won't work on linux out of the box due to docker/for-linux#264, but a SAM CLI option to specify host would be nice.
jasonterando commentedon Jan 9, 2021
Given that SAM CLI is launching a container to run SAM template, would it make sense to consider documenting how to launch the image via Docker Compose (or similar tool)?
Probably ought to explain why some of us want to run SAM under Docker (regardless if it's a good idea, possible, etc.) One of the nice things about developing with Docker is that it helps to avoid workstation bloat and conflicts by, for example, having to install yet another package manager (i.e. Homebrew); which, in turn, steps on our environment's default Python version. This is not to pick on Homebrew, it's probably great; although like many tools of its ilk it is much easier to install than to remove when you no longer need it (like having to manually edit ~/bashrc). At the very least, let Python developers know in the docs there's the option of running
pip3 install aws-sam-cli.On a side note, I tried ilyasotkov's workaround/hack in Linux and it didn't work for me. For anybody interested, here's the Dockerfile I set up:
And the entrypoint.sh...
xiaoxinghu commentedon Jan 13, 2021
Another common use case for dockerizing sam: CI pipelines. Like @jasonterando said, it's a pain to maintain packages/tools installed on build agents. Different teams need their tools in particular ways. It's more like dependencies for your projects. The tools break over time, so you need to keep your setup predictable. That's what docker provides. If it's "not a good idea" to use sam within docker, then I run out of ideas to make my CI pipeline stable over long period of time. I can't trust my scripts/code to still work 1 year from now, after countless patching/upgrading of the build agents. Not to mention that normally it's not easy to tweak infrastructure setups within big corporates, it's a long and risky process. Sometimes impossible (e.g. they refuse to install arbitrary software on agents, because they gave you docker already).
ilyasotkov commentedon Jan 13, 2021
@jfuss Doesn't the presence of
-v, --docker-volume-basedir TEXToption forsam local invokecontradict with that statement? The docs for it saySo remote Docker hosts are already partially supported.
To use this in CircleCI with a remote docker, we need an ssh tunnel
To use this in CircleCI with a remote docker, we need an ssh tunnel
17 remaining items
kstro21 commentedon Apr 9, 2021
Hey, @xazhao, yes, I have SAM CLI and my project on my local computer, and the Docker engine is running on a remote host, Docker has access to my SAM files through a mounted remoted directory. But if you are in Linux, you can easily reproduce it using what @jamesorlakin is using, Docker Toolbox which will run the Docker containers inside a VirtualBox Virtual Machine.
Let me know if that helps.
rv2673 commentedon Apr 12, 2021
@xazhao
I do understand that the 127.0.0.1 is sensible for security, though what about having a flag to be able to change to which ip/host it binds for those who need it with a 'remote' docker host?
In our setup we have the following:
The docker host is remote from the perspective of the build container.
I slightly modified sam cli in our build container image to get it to work currently:
xazhao commentedon Apr 12, 2021
@rv2673 Using
--container-hostto replace127.0.0.1is a better idea in my opinion since it won't bind to all interfaces. It would support your use case and use case of @kstro21 .Thanks for looking into the warm container issue. We realized that issue before last release so we decided to revert the change. We would like to release a completed function as possible.
Currently I'm preparing a PR to include following changes:
127.0.0.1with the new option--container-hostso it can support remote Docker host.timoaudi commentedon Apr 16, 2021
For those who are wondering how to downgrade to 1.12.0 – this worked for me:
xazhao commentedon Apr 21, 2021
@kstro21 @rv2673 In our latest nightly release, a new option
--container-host-interfaceis added so you can specify what interface you want to bind ports to. Feel free to try it with remote Docker host.kstro21 commentedon Apr 23, 2021
@xazhao, thanks, it works.
What's the difference between
--container-hostand--container-host-interface? For me are the same. This is how I have to use themsam local start-api \ -t template.yaml \ --region us-east-1 \ --env-vars .envs.json \ --parameter-overrides Environment=sandbox \ --container-host 192.168.99.102 \ --container-host-interface 192.168.99.102 \ --warm-containers LAZY \ --skip-pull-imageAs you can see, both options have the same value.
xazhao commentedon Apr 27, 2021
@kstro21 In your case, yes these two options have the same value.
In some cases, for example, we want to use it in a docker container,
--container-hostwill behost.docker.internaland--container-host-interfacewill be127.0.0.1since we want it bind to loopback interface because of security reason.--container-hostis the host of locally emulated Lambda container while--container-host-interfaceis used to specify interface it binds ports to. They two options should work independently.Hopefully this makes sense to you.
rv2673 commentedon Apr 28, 2021
@xazhao Sorry was off for a few days. But just tested it, and it works great.
psdkumar commentedon Apr 30, 2021
@alexdrans This worked for me. Thanks!
For anyone who finds this:
I replaced
app-name_apiwith whatever is visible after runningdocker network ls. For example, in my case, it waslocal-setup_postgreskingram6865 commentedon Aug 7, 2021
Is there a resolution to this issue?
Or a breadcrumb I can grab to decipher this problem?
jomach commentedon Aug 17, 2021
I had the same issue.
running on a mac with visual studio code and dev containers.
Solved by just adding: --container-host host.docker.internal
kingram6865 commentedon Aug 17, 2021
That was a pretty good tip but the issue is still unresolved:
Navigating to the url:

Navigating to the url with the proper endpoint:
This is a mess. I just want to have an environment to test my lambdas before deploying them.
matthewcn56 commentedon Aug 17, 2021
I am having the same issue as @kingram6865
eichelkrauta commentedon Mar 10, 2022
hey friends above that were having the "Internal Server error" response, I think I had the same thing come up, maybe this would help?
I started off having the "empty response" issue no matter what I tried. After a lot of trial and error, eventually I landed on the below combination of things:
This here is the command I am using:
sam local start-api --warm-containers EAGER --host 0.0.0.0 --container-host host.docker.internalThe combination of the
--hostand--container-hostis what made it work for me. I noticed that when I did adocker psit was publishing0.0.0.0:3000->3000, which tipped me off to add the--host 0.0.0.0. So that got me to the point where I was seeing the internal server error. After adding the magical--container-host host.docker.internalas well, that's when it all started to work for me.