aws / aws/aws-sam-cli

Bug: Cannot resolve http://host.docker.internal when using --warm-containers

Open
#6,703 7 comments 0 reactions 0 assignees View on GitHub
type/feature
Dominant language
Python
Stars
6.7k
Forks
1.2k
Avg merge
1d 10h
Merged PRs (30d)
52

Description

### Description:
When using --warm-containers LAZY or --warm-containers EAGER, any calls to host.docker.internal will hang and timeout.

This is an issue when there are multiple routes in the Lambda function and some routes need to call each other. This does not occur if --warm-containers is not used but then local development is incredibly slow.

### Steps to reproduce:

Run `sam local start-api` and navigate to http://127.0.0.1:3000/route_two in the browser and it will work.

Run `sam local start-api --warm-containers LAZY` and navigate to http://127.0.0.1:3000/route_two in the browser and it will hang and timeout.

app.js
```
const api = require('lambda-api')();

api.get('/route_one', (req, res) => {
res.header('content-type', 'application/json').json('Hello from route_one');
});

api.get('/route_two', async (req, res) => {
let data = await fetch('http://host.docker.internal:3000/route_one');
data = await data.json();
res.send(data);
});

exports.handler = async (event, context) => {
return await api.run(event, context);
};
```

template.yaml
```
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Resources:
APILambda:
Type: AWS::Serverless::Function
Properties:
FunctionName: API
CodeUri: .
Handler: app.handler
Runtime: nodejs18.x
Timeout: 30
MemorySize: 384

API:
Type: AWS::Serverless::Api
Properties:
Name: dev
StageName: dev
DefinitionBody:
swagger: '2.0'
paths:
/route_one:
get:
x-amazon-apigateway-integration:
httpMethod: "GET"
uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${APILambda.Arn}/invocations
passthroughBehavior: "when_no_match"
type: "aws_proxy"

/route_two:
get:
x-amazon-apigateway-integration:
httpMethod: "GET"
uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${APILambda.Arn}/invocations
passthroughBehavior: "when_no_match"
type: "aws_proxy"

```

package.json
```
{
"dependencies": {
"lambda-api": "^1.0.3"
}
}
```

### Observed result:
```
024-02-16 10:21:14,403 | Lambda function 'APILambda' is already running
2024-02-16 10:21:14,405 | Starting a timer for 30 seconds for function 'APILambda'
2024-02-16 10:21:14,406 | Getting lock for the key localhost-8658
2024-02-16 10:21:14,408 | Waiting to retrieve the lock (localhost-8658) to start invocation
2024-02-16 10:21:42,696 | Function 'APILambda' timed out after 30 seconds
```
```
{
"message": "Internal server error"
}
```

### Expected result:
Expect being able to call local routes using http://host.docker.internal with --warm-containers LAZY without a timeout.

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

1. OS: Mac
2. `sam --version`: 1.109.0
3. AWS region: N/A

```
{
"version": "1.109.0",
"system": {
"python": "3.8.13",
"os": "macOS-14.1.1-arm64-arm-64bit"
},
"additional_dependencies": {
"docker_engine": "25.0.3",
"aws_cdk": "Not available",
"terraform": "Not available"
},
"available_beta_feature_env_vars": [
"SAM_CLI_BETA_FEATURES",
"SAM_CLI_BETA_BUILD_PERFORMANCE",
"SAM_CLI_BETA_TERRAFORM_SUPPORT",
"SAM_CLI_BETA_RUST_CARGO_LAMBDA"
]
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.