Skip to content

Commit 950c2ba

Browse files
committed
Add Lambda Function URL HTTP request and response types
1 parent bc1ec47 commit 950c2ba

File tree

4 files changed

+181
-0
lines changed

4 files changed

+181
-0
lines changed

events/lambda_http.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
3+
package events
4+
5+
// LambdaHTTPRequest contains data coming from the new HTTP API Gateway
6+
type LambdaHTTPRequest struct {
7+
Version string `json:"version"` // Version is expected to always be `"2.0"`
8+
RouteKey string `json:"routeKey"` // RouteKey is expected to always be `"$default"`
9+
RawPath string `json:"rawPath"`
10+
RawQueryString string `json:"rawQueryString"`
11+
Cookies []string `json:"cookies,omitempty"`
12+
Headers map[string]string `json:"headers"`
13+
QueryStringParameters map[string]string `json:"queryStringParameters,omitempty"`
14+
RequestContext LambdaHTTPRequestContext `json:"requestContext"`
15+
Body string `json:"body,omitempty"`
16+
IsBase64Encoded bool `json:"isBase64Encoded"`
17+
}
18+
19+
// LambdaHTTPRequestContext contains the information to identify the AWS account and resources invoking the Lambda function.
20+
type LambdaHTTPRequestContext struct {
21+
RouteKey string `json:"routeKey"` // RouteKey is expected to always be `"$default"`
22+
AccountID string `json:"accountId"`
23+
Stage string `json:"stage"` // Stage is expected to always be `"$default"`
24+
RequestID string `json:"requestId"`
25+
Authorizer *LambdaHTTPRequestContextAuthorizerDescription `json:"authorizer,omitempty"`
26+
APIID string `json:"apiId"` // APIID is the Lambda URL ID
27+
DomainName string `json:"domainName"` // DomainName is of the format `"<url-id>.lambda-url.<region>.on.aws"`
28+
DomainPrefix string `json:"domainPrefix"` // DomainPrefix is the Lambda URL ID
29+
Time string `json:"time"`
30+
TimeEpoch int64 `json:"timeEpoch"`
31+
HTTP LambdaHTTPRequestContextHTTPDescription `json:"http"`
32+
}
33+
34+
// LambdaHTTPRequestContextAuthorizerDescription contains authorizer information for the request context.
35+
type LambdaHTTPRequestContextAuthorizerDescription struct {
36+
IAM *LambdaHTTPRequestContextAuthorizerIAMDescription `json:"iam,omitempty"`
37+
}
38+
39+
// LambdaHTTPRequestContextAuthorizerIAMDescription contains IAM information for the request context.
40+
type LambdaHTTPRequestContextAuthorizerIAMDescription struct {
41+
AccessKey string `json:"accessKey"`
42+
AccountID string `json:"accountId"`
43+
CallerID string `json:"callerId"`
44+
UserARN string `json:"userArn"`
45+
UserID string `json:"userId"`
46+
}
47+
48+
// LambdaHTTPRequestContextHTTPDescription contains HTTP information for the request context.
49+
type LambdaHTTPRequestContextHTTPDescription struct {
50+
Method string `json:"method"`
51+
Path string `json:"path"`
52+
Protocol string `json:"protocol"`
53+
SourceIP string `json:"sourceIp"`
54+
UserAgent string `json:"userAgent"`
55+
}
56+
57+
// LambdaHTTPResponse configures the response to be returned by API Gateway V2 for the request
58+
type LambdaHTTPResponse struct {
59+
StatusCode int `json:"statusCode"`
60+
Headers map[string]string `json:"headers"`
61+
Body string `json:"body"`
62+
IsBase64Encoded bool `json:"isBase64Encoded"`
63+
Cookies []string `json:"cookies"`
64+
}

events/lambda_http_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
3+
package events
4+
5+
import (
6+
"encoding/json"
7+
"io/ioutil"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestLambdaHTTPResponseMarshaling(t *testing.T) {
14+
15+
// read json from file
16+
inputJSON, err := ioutil.ReadFile("./testdata/lambda-urls-response.json")
17+
if err != nil {
18+
t.Errorf("could not open test file. details: %v", err)
19+
}
20+
21+
// de-serialize into Go object
22+
var inputEvent LambdaHTTPResponse
23+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
24+
t.Errorf("could not unmarshal event. details: %v", err)
25+
}
26+
27+
// serialize to json
28+
outputJSON, err := json.Marshal(inputEvent)
29+
if err != nil {
30+
t.Errorf("could not marshal event. details: %v", err)
31+
}
32+
33+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
34+
}
35+
36+
func TestLambdaHTTPRequestMarshaling(t *testing.T) {
37+
38+
// read json from file
39+
inputJSON, err := ioutil.ReadFile("./testdata/lambda-urls-request.json")
40+
if err != nil {
41+
t.Errorf("could not open test file. details: %v", err)
42+
}
43+
44+
// de-serialize into Go object
45+
var inputEvent LambdaHTTPRequest
46+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
47+
t.Errorf("could not unmarshal event. details: %v", err)
48+
}
49+
50+
// serialize to json
51+
outputJSON, err := json.Marshal(inputEvent)
52+
if err != nil {
53+
t.Errorf("could not marshal event. details: %v", err)
54+
}
55+
56+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
57+
}
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"version": "2.0",
3+
"routeKey": "$default",
4+
"rawPath": "/my/path",
5+
"rawQueryString": "parameter1=value1&parameter1=value2&parameter2=value",
6+
"cookies": [
7+
"cookie1",
8+
"cookie2"
9+
],
10+
"headers": {
11+
"header1": "value1",
12+
"header2": "value1,value2"
13+
},
14+
"queryStringParameters": {
15+
"parameter1": "value1,value2",
16+
"parameter2": "value"
17+
},
18+
"requestContext": {
19+
"accountId": "123456789012",
20+
"apiId": "<urlid>",
21+
"authorizer": {
22+
"iam": {
23+
"accessKey": "AKIA...",
24+
"accountId": "111122223333",
25+
"callerId": "AIDA...",
26+
"userArn": "arn:aws:iam::111122223333:user/example-user",
27+
"userId": "AIDA..."
28+
}
29+
},
30+
"domainName": "<url-id>.lambda-url.us-west-2.on.aws",
31+
"domainPrefix": "<url-id>",
32+
"http": {
33+
"method": "POST",
34+
"path": "/my/path",
35+
"protocol": "HTTP/1.1",
36+
"sourceIp": "123.123.123.123",
37+
"userAgent": "agent"
38+
},
39+
"requestId": "id",
40+
"routeKey": "$default",
41+
"stage": "$default",
42+
"time": "12/Mar/2020:19:03:58 +0000",
43+
"timeEpoch": 1583348638390
44+
},
45+
"body": "Hello from client!",
46+
"isBase64Encoded": false
47+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"statusCode": 201,
3+
"headers": {
4+
"Content-Type": "application/json",
5+
"My-Custom-Header": "Custom Value"
6+
},
7+
"body": "{ \"message\": \"Hello, world!\" }",
8+
"cookies": [
9+
"Cookie_1=Value1; Expires=21 Oct 2021 07:48 GMT",
10+
"Cookie_2=Value2; Max-Age=78000"
11+
],
12+
"isBase64Encoded": false
13+
}

0 commit comments

Comments
 (0)