Skip to content

Commit 44a1258

Browse files
committed
add an env variable for host to be consistent
1 parent fcd7951 commit 44a1258

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

Sources/AWSLambdaRuntime/Lambda+LocalServer.swift

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import NIOHTTP1
2121
import NIOPosix
2222
import Synchronization
2323

24-
// This functionality is designed for local testing hence being a #if DEBUG flag.
24+
// This functionality is designed for local testing when the LocalServerSupport trait is enabled.
2525

2626
// For example:
2727
// try Lambda.withLocalServer {
@@ -41,22 +41,25 @@ extension Lambda {
4141
/// Execute code in the context of a mock Lambda server.
4242
///
4343
/// - parameters:
44-
/// - invocationEndpoint: The endpoint to post events to.
44+
/// - host: the hostname or IP address to listen on
4545
/// - port: the TCP port to listen to
46+
/// - invocationEndpoint: The endpoint to post events to.
4647
/// - body: Code to run within the context of the mock server. Typically this would be a Lambda.run function call.
4748
///
48-
/// - note: This API is designed strictly for local testing and is behind a DEBUG flag
49+
/// - note: This API is designed strictly for local testing when the LocalServerSupport trait is enabled.
4950
@usableFromInline
5051
static func withLocalServer(
51-
invocationEndpoint: String? = nil,
52+
host: String,
5253
port: Int,
54+
invocationEndpoint: String? = nil,
5355
logger: Logger,
5456
_ body: sending @escaping () async throws -> Void
5557
) async throws {
5658
do {
5759
try await LambdaHTTPServer.withLocalServer(
58-
invocationEndpoint: invocationEndpoint,
60+
host: host,
5961
port: port,
62+
invocationEndpoint: invocationEndpoint,
6063
logger: logger
6164
) {
6265
try await body()
@@ -113,9 +116,9 @@ internal struct LambdaHTTPServer {
113116
}
114117

115118
static func withLocalServer<Result: Sendable>(
116-
invocationEndpoint: String?,
117-
host: String = "127.0.0.1",
119+
host: String,
118120
port: Int,
121+
invocationEndpoint: String?,
119122
eventLoopGroup: MultiThreadedEventLoopGroup = .singleton,
120123
logger: Logger,
121124
_ closure: sending @escaping () async throws -> Result

Sources/AWSLambdaRuntime/LambdaRuntime.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,19 @@ public final class LambdaRuntime<Handler>: Sendable where Handler: StreamingLamb
126126
// we're not running on Lambda and we're compiled in DEBUG mode,
127127
// let's start a local server for testing
128128

129+
let host = Lambda.env("LOCAL_LAMBDA_HOST") ?? "127.0.0.1"
129130
let port = Lambda.env("LOCAL_LAMBDA_PORT").flatMap(Int.init) ?? 7000
131+
let endpoint = Lambda.env("LOCAL_LAMBDA_INVOCATION_ENDPOINT")
130132

131133
try await Lambda.withLocalServer(
132-
invocationEndpoint: Lambda.env("LOCAL_LAMBDA_SERVER_INVOCATION_ENDPOINT"),
134+
host: host,
133135
port: port,
136+
invocationEndpoint: endpoint,
134137
logger: self.logger
135138
) {
136139

137140
try await LambdaRuntimeClient.withRuntimeClient(
138-
configuration: .init(ip: "127.0.0.1", port: port),
141+
configuration: .init(ip: host, port: port),
139142
eventLoop: self.eventLoop,
140143
logger: self.logger
141144
) { runtimeClient in
@@ -147,7 +150,7 @@ public final class LambdaRuntime<Handler>: Sendable where Handler: StreamingLamb
147150
}
148151
}
149152
#else
150-
// in release mode, we can't start a local server because the local server code is not compiled.
153+
// When the LocalServerSupport trait is disabled, we can't start a local server because the local server code is not compiled.
151154
throw LambdaRuntimeError(code: .missingLambdaRuntimeAPIEnvironmentVariable)
152155
#endif
153156
}

readme.md

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -464,28 +464,22 @@ curl -v --header "Content-Type:\ application/json" --data @events/create-session
464464
* Connection #0 to host 127.0.0.1 left intact
465465
{"statusCode":200,"isBase64Encoded":false,"body":"...","headers":{"Access-Control-Allow-Origin":"*","Content-Type":"application\/json; charset=utf-8","Access-Control-Allow-Headers":"*"}}
466466
```
467-
### Modifying the local endpoint
467+
### Modifying the local server URI
468468

469-
By default, when using the local Lambda server, it listens on the `/invoke` endpoint.
469+
By default, when using the local Lambda server during your tests, it listens on `http://127.0.0.1:7000/invoke`.
470470

471-
Some testing tools, such as the [AWS Lambda runtime interface emulator](https://docs.aws.amazon.com/lambda/latest/dg/images-test.html), require a different endpoint. In that case, you can use the `LOCAL_LAMBDA_SERVER_INVOCATION_ENDPOINT` environment variable to force the runtime to listen on a different endpoint.
471+
Some testing tools, such as the [AWS Lambda runtime interface emulator](https://docs.aws.amazon.com/lambda/latest/dg/images-test.html), require a different endpoint, the port might be used, or you may want to bind a specific IP address.
472472

473-
Example:
474-
475-
```sh
476-
LOCAL_LAMBDA_SERVER_INVOCATION_ENDPOINT=/2015-03-31/functions/function/invocations swift run
477-
```
478-
479-
## Modifying the local port
480-
481-
By default, when using the local Lambda server, it listens on TCP port 7000.
473+
In these cases, you can use three environment variables to control the local server:
482474

483-
On macOS, this port might be used by AirPlay under some configurations. You can use the `LOCAL_LAMBDA_PORT` environment variable to configure the runtime to listen on a different TCP port.
475+
- Set `LOCAL_LAMBDA_HOST` to configure the local server to listen on a different TCP address.
476+
- Set `LOCAL_LAMBDA_PORT` to configure the local server to listen on a different TCP port.
477+
- Set `LOCAL_LAMBDA_INVOCATION_ENDPOINT` to force the local server to listen on a different endpoint.
484478

485479
Example:
486480

487481
```sh
488-
LOCAL_LAMBDA_PORT=7777 swift run
482+
LOCAL_LAMBDA_PORT=8080 LOCAL_LAMBDA_INVOCATION_ENDPOINT=/2015-03-31/functions/function/invocations swift run
489483
```
490484

491485
## Deploying your Swift Lambda functions

0 commit comments

Comments
 (0)