Skip to content

API Gateway example code not working #480

Closed
@klsetzer

Description

@klsetzer

Expected behavior

Running curl against local API Gateway example from README.md, when running with swift run or running from Xcode

Expect the curl to return with an HTTP 200 response and a valid API Gateway response json

Actual behavior

  1. First curl request to the API at http://127.0.0.1:7000/invoke hangs (curl blocks)
  2. Error emitted in Xcode console (also on terminal, if using swift run
2025-02-17T14:16:29-0500 info LocalServer : host="127.0.0.1" port=7000 [AWSLambdaRuntimeCore] Server started and listening
2025-02-17T14:17:27-0500 error LocalServer : [AWSLambdaRuntimeCore] Hit error: CancellationError()
  1. Subsequent curl requests yield a 403 error.

Steps to reproduce

  1. Create Package.swift, main.swift per the example in this repo's README.md
  2. Run via Xcode or via swift run
  3. Run curl: curl --header 'Content-Type: application/json' --request POST --data @event2.json --verbose http://127.0.0.1:7000/invoke

NB: Deploying to AWS and curling against the created API gateway endpoint produces 502 errors with no application output visible in CloudWatch logs - although the lambda START/END/REPORT message appear.

If possible, minimal yet complete reproducer code (or URL to code)

Package.swift

// swift-tools-version:6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "Palindrome",
    platforms: [
        .macOS(.v15)
    ],
    products: [
        .executable(name: "PalindromeLambda", targets: ["PalindromeLambda"])
    ],
    dependencies: [
        .package(
            url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main"),
        .package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", from: "1.0.0"),
    ],
    targets: [
        .executableTarget(
            name: "PalindromeLambda",
            dependencies: [
                .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
                .product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events"),
            ],
            path: "Sources"
        )
    ]
)

main.swift

import AWSLambdaEvents
import AWSLambdaRuntime

let runtime = LambdaRuntime {
    (event: APIGatewayV2Request, context: LambdaContext) -> APIGatewayV2Response in

    var header = HTTPHeaders()
    context.logger.debug("HTTP API Message received")

    header["content-type"] = "application/json"

    // echo the request in the response
    return try APIGatewayV2Response(statusCode: .ok, headers: header, encodableBody: event)
}

try await runtime.run()

What version of this project (swift-aws-lambda-runtime) are you using?

main

Swift version

swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)
Target: arm64-apple-macosx15.0
Darwin Mac-R4DKJYX9 24.1.0 Darwin Kernel Version 24.1.0: Thu Oct 10 21:03:15 PDT 2024; root:xnu-11215.41.3~2/RELEASE_ARM64_T6000 arm64

Amazon Linux 2 docker image version

No response

Metadata

Metadata

Assignees

Labels

kind/bugFeature doesn't work as expected.

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions