Skip to content

Commit 75c4ef7

Browse files
chore: tests + coverage + documentation
1 parent 9cf1ddc commit 75c4ef7

File tree

6 files changed

+49
-57
lines changed

6 files changed

+49
-57
lines changed

aws_lambda_powertools/utilities/data_classes/vpc_lattice.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from aws_lambda_powertools.utilities.data_classes.common import (
55
DictWrapper,
6-
get_header_value
6+
get_header_value,
77
)
88

99

@@ -89,4 +89,3 @@ def get_header_value(
8989
Header value
9090
"""
9191
return get_header_value(self.headers, name, default_value, case_sensitive)
92-

docs/utilities/data_classes.md

+4-51
Original file line numberDiff line numberDiff line change
@@ -1124,69 +1124,22 @@ This example is based on the AWS Blog post [Introducing Amazon S3 Object Lambda
11241124

11251125
### VPC Lattice
11261126

1127-
The VPC Lattice service supports Lambda invocation for requests over both HTTP and HTTPS. The service sends an event in JSON format, and adds the `X-Forwarded-For` header to every request. The service also adds the X-Forwarded-Proto header to requests over HTTPS.
1127+
You can register your Lambda functions as targets within an Amazon VPC Lattice service network. By doing this, your Lambda function becomes a service within the network, and clients that have access to the VPC Lattice service network can call your service.
11281128

1129-
The service Base64 encodes the body and sets isBase64Encoded to true, if the content-encoding header is present, and the content type is not one of the following:
1130-
1131-
* `text/*`
1132-
* `application/json`
1133-
* `application/xml`
1134-
* `application/javascript`
1135-
1136-
If the content-encoding header is not present, Base64 encoding depends on the content type. For the content types, `text/*`, `application/json`, `application/xml`, and `application/javascript`, the service sends the body as is and sets `is_base64_encoded` to `false`.
1129+
[Click here](https://docs.aws.amazon.com/lambda/latest/dg/services-vpc-lattice.html){target="_blank"} for more information about using AWS Lambda with Amazon VPC Lattice.
11371130

11381131
=== "app.py"
11391132

11401133
```python
1141-
from aws_lambda_powertools.utilities.data_classes import event_source, VPCLatticeEvent
1142-
1143-
@event_source(data_class=VPCLatticeEvent)
1144-
def lambda_handler(event: VPCLatticeEvent, context):
1145-
do_something_with(event.body)
1146-
1134+
--8<-- "examples/event_sources/src/vpc_lattice.py"
11471135
```
11481136

11491137
=== "Lattice Example Event"
11501138

1151-
```json
1152-
{
1153-
"raw_path": "/testpath",
1154-
"method": "GET",
1155-
"headers": {
1156-
"user_agent": "curl/7.64.1",
1157-
"x-forwarded-for": "10.213.229.10",
1158-
"host": "test-lambda-service-3908sdf9u3u.dkfjd93.vpc-lattice-svcs.us-east-2.on.aws",
1159-
"accept": "*/*"
1160-
},
1161-
"query_string_parameters": {
1162-
"key": "value"
1163-
},
1164-
"body": "...",
1165-
"is_base64_encoded": false
1166-
}
1167-
```
1168-
1169-
=== "Lattice Return Object"
1170-
11711139
```python
1172-
response = {
1173-
"isBase64Encoded": False,
1174-
"statusCode": 200,
1175-
headers: {
1176-
"Content-Type": "application/text"
1177-
},
1178-
body: "Event Response to VPC Lattice 🔥🚀🔥"
1179-
}
1180-
1181-
return response
1140+
--8<-- "examples/event_sources/src/vpc_lattice_payload.json"
11821141
```
11831142

1184-
The response from your Lambda function must include the Base64 encoding status, status code, and headers. You can omit the body.
1185-
1186-
To include a binary content in the body of the response, you must Base64 encode the content and set `isBase64Encoded` to `True`. The service decodes the content to retrieve the binary content and sends it to the client in the body of the HTTP response.
1187-
1188-
The VPC Lattice service does not honor hop-by-hop headers, such as `Connection` or `Transfer-Encoding`. You can omit the `Content-Length` header because the service computes it before sending responses to clients.
1189-
11901143
## Advanced
11911144

11921145
### Debugging
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from aws_lambda_powertools import Logger
2+
from aws_lambda_powertools.utilities.data_classes import VPCLatticeEvent, event_source
3+
from aws_lambda_powertools.utilities.typing import LambdaContext
4+
5+
logger = Logger()
6+
7+
8+
@event_source(data_class=VPCLatticeEvent)
9+
def lambda_handler(event: VPCLatticeEvent, context: LambdaContext):
10+
logger.info(event.body)
11+
12+
response = {
13+
"isBase64Encoded": False,
14+
"statusCode": 200,
15+
"headers": {"Content-Type": "application/text"},
16+
"body": "Event Response to VPC Lattice 🔥🚀🔥",
17+
}
18+
19+
return response
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"raw_path": "/testpath",
3+
"method": "GET",
4+
"headers": {
5+
"user_agent": "curl/7.64.1",
6+
"x-forwarded-for": "10.213.229.10",
7+
"host": "test-lambda-service-3908sdf9u3u.dkfjd93.vpc-lattice-svcs.us-east-2.on.aws",
8+
"accept": "*/*"
9+
},
10+
"query_string_parameters": {
11+
"order-id": "1"
12+
},
13+
"body": "eyJ0ZXN0IjogImV2ZW50In0=",
14+
"is_base64_encoded": true
15+
}

tests/events/vpcLatticeEvent.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
"host": "test-lambda-service-3908sdf9u3u.dkfjd93.vpc-lattice-svcs.us-east-2.on.aws",
88
"accept": "*/*"
99
},
10-
"query_string_parameters": {},
11-
"body": "",
12-
"is_base64_encoded": false
10+
"query_string_parameters": {
11+
"order-id": "1"
12+
},
13+
"body": "eyJ0ZXN0IjogImV2ZW50In0=",
14+
"is_base64_encoded": true
1315
}

tests/functional/test_data_classes.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
SESEvent,
2626
SNSEvent,
2727
SQSEvent,
28-
VPCLatticeEvent
28+
VPCLatticeEvent,
2929
)
3030
from aws_lambda_powertools.utilities.data_classes.api_gateway_authorizer_event import (
3131
APIGatewayAuthorizerEventV2,
@@ -2048,6 +2048,10 @@ def test_vpc_lattice_event():
20482048
event = VPCLatticeEvent(load_event("vpcLatticeEvent.json"))
20492049

20502050
assert event.raw_path == event["raw_path"]
2051+
assert event.get_query_string_value("order-id") == "1"
2052+
assert event.get_header_value("user_agent") == "curl/7.64.1"
2053+
assert event.decoded_body == '{"test": "event"}'
2054+
assert event.json_body == {"test": "event"}
20512055
assert event.method == event["method"]
20522056
assert event.headers == event["headers"]
20532057
assert event.query_string_parameters == event["query_string_parameters"]

0 commit comments

Comments
 (0)