aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.5k stars 1.17k forks source link

Bug: "socket.gaierror: [Errno -2] Name or service not known" when running in Ubuntu container #5784

Open ffMathy opened 1 year ago

ffMathy commented 1 year ago

Description:

Whenever I run my tests in GitHub Actions (which runs them via the Ubuntu image), I get errors in my tests when they try to call my Lambda after starting the SAM CLI (also done by my tests).

Steps to reproduce:

The relevant part of my GitHub actions YML is here:

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest

My template.yaml looks like this:

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31

Globals:
  Function:
    Runtime: nodejs18.x

Resources:
  Lambda:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: .
      Handler: app.handler
      Timeout: 60
      Architectures:
      - x86_64
      Events:
        Api:
          Type: HttpApi
          Properties:
            Path: /events
            TimeoutInMillis: 60000
            Method: POST

Inside that container, I programmatically try to start the SAM CLI with the following options:

sam local start-api \
    --container-env-vars ./.aws-sam/scripts/container-environment.start.json \
    --template ./.aws-sam/build/template.yaml \
    --host "0.0.0.0" \
    --container-host host.docker.internal \
    --container-host-interface "0.0.0.0" \
    --docker-volume-basedir ${LOCAL_WORKSPACE_FOLDER}/src/apps/redacted/src/.aws-sam/build

Then I make a request to the lambda.

This also happens if I just make an Ubuntu container myself and try to do it from there via CURL.

Observed result:

2023-08-17 12:10:12 127.0.0.1 - - [17/Aug/2023 12:10:12] "POST /events HTTP/1.1" 502 -
Invoking app.handler (nodejs18.x)
Using local image: public.ecr.aws/lambda/nodejs:18-rapid-x86_64.
Mounting /src/apps/redacted/src/.aws-sam/build/Lambda as /var/task:ro,delegated, inside runtime container
Exception on /events [POST]
Traceback (most recent call last):
  File "flask/app.py", line 2529, in wsgi_app
  File "flask/app.py", line 1825, in full_dispatch_request
  File "flask/app.py", line 1823, in full_dispatch_request
  File "flask/app.py", line 1799, in dispatch_request
  File "samcli/local/apigw/local_apigw_service.py", line 714, in _request_handler
  File "samcli/local/apigw/local_apigw_service.py", line 612, in _invoke_lambda_function
  File "samcli/commands/local/lib/local_lambda.py", line 144, in invoke
  File "samcli/lib/telemetry/metric.py", line 324, in wrapped_func
  File "samcli/local/lambdafn/runtime.py", line 201, in invoke
  File "samcli/local/docker/container.py", line 349, in wait_for_result
  File "samcli/local/docker/container.py", line 388, in _wait_for_socket_connection
  File "samcli/local/docker/container.py", line 406, in _can_connect_to_socket
socket.gaierror: [Errno -2] Name or service not known

Expected result:

I expected the call to succeed.

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: Any OS (happens inside Ubuntu container though)
  2. sam --version: SAM CLI, version 1.77.0
  3. AWS region: eu-west-1
# Paste the output of `sam --info` here
{
  "version": "1.77.0",
  "system": {
    "python": "3.8.17",
    "os": "macOS-13.5-arm64-arm-64bit"
  },
  "additional_dependencies": {
    "docker_engine": "24.0.2",
    "aws_cdk": "2.14.0 (build 762d71b)",
    "terraform": "Not available"
  }
}
moelasmar commented 1 year ago

thanks @ffMathy for raising this issue. I will try to reproduce this issue on github actions, mean while I have some questions, how you install SAM CLI in github actions, and what is the SAM CLI version you used in github actions.

ffMathy commented 1 year ago

I am installing the latest version of the SAM CLI via the SAM setup action 😊

ffMathy commented 11 months ago

@hawflau any progress on this?