Skip to content

Commit d3b7517

Browse files
authored
Update APIGateway example to use AWSLambdaEvents (#76)
motivation: example of how to use the API Gateway model changes: Update APIGateway example to use AWSLambdaEvents
1 parent 76b2332 commit d3b7517

File tree

5 files changed

+15
-68
lines changed

5 files changed

+15
-68
lines changed

Examples/LambdaFunctions/APIGateway-template.yml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,18 @@ Resources:
1313
# Add an API Gateway event source for the Lambda
1414
Events:
1515
HttpGet:
16-
Type: Api
16+
Type: HttpApi
1717
Properties:
18-
RestApiId: !Ref lambdaApiGateway
18+
ApiId: !Ref lambdaApiGateway
1919
Path: '/samples/apig'
20-
Method: get
20+
Method: GET
2121
# Instructs new versions to be published to an alias named "live".
2222
AutoPublishAlias: live
2323

2424
lambdaApiGateway:
25-
Type: AWS::Serverless::Api
26-
Properties:
27-
Name: Lambda API Gateway
28-
StageName: Beta
25+
Type: AWS::Serverless::HttpApi
2926

3027
Outputs:
3128
LambdaApiGatewayEndpoint:
3229
Description: 'API Gateway endpoint URL.'
33-
Value: !Sub 'https://${lambdaApiGateway}.execute-api.${AWS::Region}.amazonaws.com/Beta/samples/apig'
30+
Value: !Sub 'https://${lambdaApiGateway}.execute-api.${AWS::Region}.amazonaws.com/samples/apig'

Examples/LambdaFunctions/Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ let package = Package(
3030
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
3131
]),
3232
.target(name: "Benchmark", dependencies: [
33-
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
33+
.product(name: "AWSLambdaRuntimeCore", package: "swift-aws-lambda-runtime"),
3434
]),
3535
.target(name: "ErrorHandling", dependencies: [
3636
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
3737
]),
3838
.target(name: "APIGateway", dependencies: [
3939
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
40+
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-runtime"),
4041
]),
4142
.target(name: "CurrencyExchange", dependencies: [
4243
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),

Examples/LambdaFunctions/Sources/APIGateway/main.swift

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import AWSLambdaEvents
1516
import AWSLambdaRuntime
1617
import NIO
1718

@@ -23,63 +24,11 @@ Lambda.run(APIGatewayProxyLambda())
2324

2425
// FIXME: Use proper Event abstractions once added to AWSLambdaRuntime
2526
struct APIGatewayProxyLambda: EventLoopLambdaHandler {
26-
public typealias In = APIGatewayRequest
27-
public typealias Out = APIGatewayResponse
27+
public typealias In = APIGateway.V2.Request
28+
public typealias Out = APIGateway.V2.Response
2829

29-
public func handle(context: Lambda.Context, payload: APIGatewayRequest) -> EventLoopFuture<APIGatewayResponse> {
30+
public func handle(context: Lambda.Context, payload: APIGateway.V2.Request) -> EventLoopFuture<APIGateway.V2.Response> {
3031
context.logger.debug("hello, api gateway!")
31-
return context.eventLoop.makeSucceededFuture(APIGatewayResponse(statusCode: 200,
32-
headers: nil,
33-
multiValueHeaders: nil,
34-
body: "hello, world!",
35-
isBase64Encoded: false))
32+
return context.eventLoop.makeSucceededFuture(APIGateway.V2.Response(statusCode: .ok, body: "hello, world!"))
3633
}
3734
}
38-
39-
struct APIGatewayRequest: Codable {
40-
let resource: String
41-
let path: String
42-
let httpMethod: String?
43-
let headers: [String: String]?
44-
let multiValueHeaders: [String: [String]]?
45-
let queryStringParameters: [String: String]?
46-
let multiValueQueryStringParameters: [String: [String]]?
47-
let pathParameters: [String: String]?
48-
let stageVariables: [String: String]?
49-
let requestContext: Context?
50-
let body: String?
51-
let isBase64Encoded: Bool?
52-
53-
struct Context: Codable {
54-
let accountId: String?
55-
let resourceId: String?
56-
let stage: String?
57-
let requestId: String?
58-
let identity: Identity?
59-
let resourcePath: String?
60-
let httpMethod: String?
61-
let apiId: String
62-
}
63-
64-
struct Identity: Codable {
65-
let cognitoIdentityPoolId: String?
66-
let accountId: String?
67-
let cognitoIdentityId: String?
68-
let caller: String?
69-
let apiKey: String?
70-
let sourceIp: String?
71-
let cognitoAuthenticationType: String?
72-
let cognitoAuthenticationProvider: String?
73-
let userArn: String?
74-
let userAgent: String?
75-
let user: String?
76-
}
77-
}
78-
79-
struct APIGatewayResponse: Codable {
80-
let statusCode: Int
81-
let headers: [String: String]?
82-
let multiValueHeaders: [String: [String]]?
83-
let body: String?
84-
let isBase64Encoded: Bool?
85-
}

Examples/LambdaFunctions/Sources/Benchmark/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import AWSLambdaRuntime
15+
import AWSLambdaRuntimeCore
1616
import NIO
1717

1818
// If you would like to benchmark Swift's Lambda Runtime,

Examples/LambdaFunctions/scripts/build-and-package.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ executable=$1
2020
echo "-------------------------------------------------------------------------"
2121
echo "building \"$executable\" lambda"
2222
echo "-------------------------------------------------------------------------"
23-
docker run --rm -v `pwd`:/workspace -w /workspace builder bash -cl "swift build --product $executable -c release -Xswiftc -g"
23+
docker run --rm -v "$(pwd)/../..":/workspace -w /workspace/Examples/LambdaFunctions builder bash -cl "swift build --product $executable -c release -Xswiftc -g"
2424
echo "done"
2525

2626
echo "-------------------------------------------------------------------------"
2727
echo "packaging \"$executable\" lambda"
2828
echo "-------------------------------------------------------------------------"
29-
docker run --rm -v `pwd`:/workspace -w /workspace builder bash -cl "./scripts/package.sh $executable"
29+
docker run --rm -v "$(pwd)/../..":/workspace -w /workspace/Examples/LambdaFunctions builder bash -cl "./scripts/package.sh $executable"

0 commit comments

Comments
 (0)