swift-server / swift-aws-lambda-events Goto Github PK
View Code? Open in Web Editor NEWSwift implementation of AWS Lambda Events
License: Apache License 2.0
Swift implementation of AWS Lambda Events
License: Apache License 2.0
This issue is a carry over from the Lambda runtime project
swift-server/swift-aws-lambda-runtime#53
https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-message-attributes.html under "Message Attribute Data Types"
Number attributes can store positive or negative numerical values. A number can have up to 38 digits of precision, and it can be between 10^-128 and 10^+126.
right now using string as a stop gap, could use Foundation's Decimal or come up with our own BigNumber abstraction
The Apache licence added to the project does not contain a copyright year or name of the copyright holder - https://github.com/swift-server/swift-aws-lambda-events/blob/main/LICENSE.txt#L190
This should probably be fixed...
It appears Swift 5.7 is required now. If so the 5.4 and 5.5 Package.swift should probably be removed.
Similarly the docker-compose.al2.56.yaml file could be removed
Correct parsing using JSONDecoder().decode(SNS.Message.self, from: data)
Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"SigningCertUrl\", intValue: nil) (\"SigningCertUrl\").", underlyingError: nil))
SigningCertUrl
and SubscribeUrl
will result in parsing error
main
branch
swift --version && uname -a
)swift-driver version: 1.62.15 Apple Swift version 5.7.1 (swiftlang-5.7.1.135.3 clang-1400.0.29.51)
Package is correctly installed.
error: Dependencies could not be resolved because no versions of 'swift-aws-lambda-events' match the requirement 0.1.0..<1.0.0 and root depends on 'swift-aws-lambda-events' 0.1.0..<1.0.0.
There are no appropriate git tags on the repository.
swift --version && uname -a
)Apple Swift version 5.4.2 (swiftlang-1205.0.28.2 clang-1205.0.19.57).
Darwin breathwork.local 20.4.0 Darwin Kernel Version 20.4.0: Thu Apr 22 21:46:47 PDT 2021; root:xnu-7195.101.2~1/RELEASE_X86_64 x86_64
This is more of a question or thought than a bug...
There's some HTTP implementation here:
that has a lot of overlap with the recent https://github.com/apple/swift-http-types library.
Would it be a good idea to use the swift-http-types library instead?
Ability to process context from a Lambda authorizer in APIGatewayV2Request
Fails trying to deserialize jwt
from the Authorizer struct which isn't present for Lambda authorizers, from CloudWatch logs:
warning Lambda : lifecycleIteration=0 [AWSLambdaRuntimeCore] lambda handler returned an error: requestDecoding(Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "jwt", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "requestContext", intValue: nil), CodingKeys(stringValue: "authorizer", intValue: nil)], debugDescription: "No value associated with key CodingKeys(stringValue: \"jwt\", intValue: nil) (\"jwt\").", underlyingError: nil)))
Also, would be nice to have structs for Lambda authorizer payloads
Looks like
needs to be made optional (guessing this will be a breaking change because of Swift's type safety when compiling)3ac078f4d8fe6d9ae8dd05b680a284a423e1578d
swift --version && uname -a
)swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1)
Target: arm64-apple-macosx14.0
Darwin 192.168.1.121 23.1.0 Darwin Kernel Version 23.1.0: Mon Oct 9 21:27:24 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T6000 arm64
APIGatwewayV2Request should support iam
authorizers.
requestDecoding(Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "jwt", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "requestContext", intValue: nil), CodingKeys(stringValue: "authorizer", intValue: nil)], debugDescription: "No value associated with key CodingKeys(stringValue: \"jwt\", intValue: nil) (\"jwt\").", underlyingError: nil)))
Fail to decode to JSON
Here is a SAM snippet
Resources:
# Lambda function
MyLambdaFunction:
Type: AWS::Serverless::Function
Properties:
Events:
# pass through all HTTP verbs and paths
Api:
Type: HttpApi
Properties:
Auth:
Authorizer: AWS_IAM
Metadata:
BuildMethod: makefile
Here is the payload received that fails to decode :
{
"version": "2.0",
"routeKey": "$default",
"rawPath": "/liveness",
"rawQueryString": "",
"headers": {
"accept": "*/*",
"authorization": "AWS4-HMAC-SHA256 Credential=ASIA-redacted/us-east-1/execute-api/aws4_request, SignedHeaders=host;x-amz-date;x-amz-security-token, Signature=289b5fcef3d1156f019cc1140cb5565cc052880a5a0d5586c753e3e3c75556f9",
"content-length": "0",
"host": "74bxj8iqjc.execute-api.us-east-1.amazonaws.com",
"user-agent": "curl/8.4.0",
"x-amz-date": "20231214T203121Z",
"x-amz-security-token": "IQoJb3JpZ2luX2VjEO3//////////-redacted",
"x-amzn-trace-id": "Root=1-657b6619-3222de40051925dd66e1fd72",
"x-forwarded-for": "191.95.150.52",
"x-forwarded-port": "443",
"x-forwarded-proto": "https"
},
"requestContext": {
"accountId": "012345678912",
"apiId": "74bxj8iqjc",
"authorizer": {
"iam": {
"accessKey": "ASIA-redacted",
"accountId": "486652066693",
"callerId": "AIDA-redacted",
"cognitoIdentity": null,
"principalOrgId": "aws:PrincipalOrgID",
"userArn": "arn:aws:iam::012345678912:user/sst",
"userId": "AIDA-redacted"
}
},
"domainName": "74bxj8iqjc.execute-api.us-east-1.amazonaws.com",
"domainPrefix": "74bxj8iqjc",
"http": {
"method": "GET",
"path": "/liveness",
"protocol": "HTTP/1.1",
"sourceIp": "191.95.150.52",
"userAgent": "curl/8.4.0"
},
"requestId": "P8zkDiQ8oAMEJsQ=",
"routeKey": "$default",
"stage": "$default",
"time": "14/Dec/2023:20:31:21 +0000",
"timeEpoch": 1702585881671
},
"isBase64Encoded": false
}
Expected my lambda to return a APIGatewayV2Response along with my body
Found the following in the CloudWatch Logs
2022-04-25T15:40:54+0000 warning Lambda : lifecycleIteration=1 lambda handler returned an error: requestDecoding(Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "version", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"version\", intValue: nil) (\"version\").", underlyingError: nil)))
Deployment
Lambda found in the Examples dir and deployed to AWS using APIGateway with a Lambda_PROXYserverless.yml
service: helloworld-swift-aws
provider:
name: aws
runtime: provided
region: us-east-2
package:
artifact: .build/lambda/HelloWorld/lambda.zip
functions:
hello:
handler: HelloWorld
memorySize: 128
events:
- http:
method: ANY
path: /{proxy+}
cors: true
main
branch
swift --version && uname -a
)Apple Swift version 5.6 (swiftlang-5.6.0.323.62 clang-1316.0.20.8)
MacOS Monterrey 12.3.1
I am using swift as a appsync lambda resolver with the following GraphQL schema.
input TestInput {
ids: [String]
}
testMutation(input: TestInput!): String
According to the GraphQL Documentation arrays are allowed. So a correct JSON Decode should happen.
By setting the message
to type of AWSLambdaEvents.AppSync.Event
leads to a Unexpected AppSync argument. Expected a String or a Dictionary.
error.
{
"field": "testMutation",
"arguments": {
"input": {
"ids": [
"id1",
"id2"
]
}
}
}
This is a the json payload I get from appsync.
swift --version && uname -a
)Apple Swift version 5.4 (swiftlang-1205.0.26.9 clang-1205.0.19.55)
Target: x86_64-apple-darwin20.4.0
Darwin xx-MacBook-Pro.local 20.4.0 Darwin Kernel Version 20.4.0: Thu Apr 22 21:46:47 PDT 2021; root:xnu-7195.101.2~1/RELEASE_X86_64 x86_64
I'm trying to test a LambdaHandler
struct MyHandler: LambdaHandler {
init(context: LambdaInitializationContext) async throws { }
func handle(_ request: APIGatewayV2Request, context: LambdaContext) async throws -> APIGatewayV2Response {
...
}
}
using Lambda.test(MyHandler.self, with: <APIGatewayV2Request>)
but I can't create an APIGatewayV2Request
to test with because init
isn't public.
I would expect to be able to create an APIGatewayV2Request
via init
or some convenience method so that I can write tests for my LambdaHandler
.
LambdaHandler
whose Event
is of type APIGatewayV2Request
Lambda.test
from the AWSLambdaTesting
library.Handler
import AWSLambdaEvents
import AWSLambdaRuntime
import Foundation
struct MyHandler: LambdaHandler {
init(context: LambdaInitializationContext) async throws { }
func handle(_ request: APIGatewayV2Request, context: LambdaContext) async throws -> APIGatewayV2Response {
let response = APIGatewayV2Response(statusCode: .ok, body: "Hello World!")
return response
}
}
Test
import AWSLambdaEvents
import AWSLambdaRuntime
import AWSLambdaTesting
@testable import <MyLambda Target>
import XCTest
final class MyHandlerTests: XCTestCase {
func testOKResponse() async throws {
let request = APIGatewayV2Request() // Can't create this
let response = try await Lambda.test(MyHandler.self, with: request)
XCTAssertEqual(response.statusCode, .ok)
}
}
1.0.0-alpha.2
swift --version && uname -a
)swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0
Darwin xxx.local 23.4.0 Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:49 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6020 arm64
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.