Skip to content

Commit d1ce847

Browse files
authored
Automatically empty buckets in Lambda examples (#4453)
1 parent 809ddd7 commit d1ce847

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

distribution/lambda/Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,17 @@ deploy-mock-data: package check-env
6767

6868
# address https://github.com/aws/aws-cdk/issues/20060
6969
before-destroy:
70+
mkdir -p cdk.out
7071
touch $(INDEXER_PACKAGE_PATH)
7172
touch $(SEARCHER_PACKAGE_PATH)
7273

7374
destroy-hdfs: before-destroy
74-
cdk destroy -a cdk/app.py HdfsStack
75+
python -c 'from cdk import cli; cli.empty_hdfs_bucket()'
76+
cdk destroy --force -a cdk/app.py HdfsStack
7577

7678
destroy-mock-data: before-destroy
77-
cdk destroy -a cdk/app.py MockDataStack
79+
python -c 'from cdk import cli; cli.empty_mock_data_buckets()'
80+
cdk destroy --force -a cdk/app.py MockDataStack
7881

7982
clean:
8083
rm -rf cdk.out

distribution/lambda/cdk/cli.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,16 @@ def get_logs(
269269

270270

271271
def download_logs_to_file(request_id: str, function_name: str, invoke_start: float):
272-
with open(f"lambda.{request_id}.log", "w") as f:
273-
for log in get_logs(
274-
function_name,
275-
request_id,
276-
int(invoke_start * 1000),
277-
):
278-
f.write(log)
272+
try:
273+
with open(f"lambda.{request_id}.log", "w") as f:
274+
for log in get_logs(
275+
function_name,
276+
request_id,
277+
int(invoke_start * 1000),
278+
):
279+
f.write(log)
280+
except Exception as e:
281+
print(f"Failed to download logs: {e}")
279282

280283

281284
def invoke_mock_data_searcher():
@@ -288,11 +291,31 @@ def invoke_mock_data_searcher():
288291

289292

290293
def _clean_s3_bucket(bucket_name: str, prefix: str = ""):
294+
print(f"Cleaning up bucket {bucket_name}/{prefix}...")
291295
s3 = session.resource("s3")
292296
bucket = s3.Bucket(bucket_name)
293297
bucket.objects.filter(Prefix=prefix).delete()
294298

295299

300+
def empty_hdfs_bucket():
301+
bucket_name = _get_cloudformation_output_value(
302+
app.HDFS_STACK_NAME, hdfs_stack.INDEX_STORE_BUCKET_NAME_EXPORT_NAME
303+
)
304+
305+
_clean_s3_bucket(bucket_name)
306+
307+
308+
def empty_mock_data_buckets():
309+
bucket_name = _get_cloudformation_output_value(
310+
app.MOCK_DATA_STACK_NAME, mock_data_stack.INDEX_STORE_BUCKET_NAME_EXPORT_NAME
311+
)
312+
_clean_s3_bucket(bucket_name)
313+
bucket_name = _get_cloudformation_output_value(
314+
app.MOCK_DATA_STACK_NAME, mock_data_stack.SOURCE_BUCKET_NAME_EXPORT_NAME
315+
)
316+
_clean_s3_bucket(bucket_name)
317+
318+
296319
@cache
297320
def _git_commit():
298321
return subprocess.run(

distribution/lambda/cdk/stacks/examples/mock_data_stack.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from ..services.quickwit_service import QuickwitService
1616

1717
SEARCHER_FUNCTION_NAME_EXPORT_NAME = "mock-data-searcher-function-name"
18+
INDEX_STORE_BUCKET_NAME_EXPORT_NAME = "mock-data-index-store-bucket-name"
19+
SOURCE_BUCKET_NAME_EXPORT_NAME = "mock-data-source-bucket-name"
1820

1921

2022
class Source(Construct):
@@ -66,6 +68,12 @@ def __init__(
6668
mock_data_bucket.add_object_created_notification(
6769
aws_s3_notifications.LambdaDestination(qw_svc.indexer.lambda_function)
6870
)
71+
aws_cdk.CfnOutput(
72+
self,
73+
"source-bucket-name",
74+
value=mock_data_bucket.bucket_name,
75+
export_name=SOURCE_BUCKET_NAME_EXPORT_NAME,
76+
)
6977

7078

7179
class SearchAPI(Construct):
@@ -164,6 +172,12 @@ def __init__(
164172
api_key=search_api_key,
165173
)
166174

175+
aws_cdk.CfnOutput(
176+
self,
177+
"index-store-bucket-name",
178+
value=qw_svc.bucket.bucket_name,
179+
export_name=INDEX_STORE_BUCKET_NAME_EXPORT_NAME,
180+
)
167181
aws_cdk.CfnOutput(
168182
self,
169183
"searcher-function-name",

docs/guides/e2e-serverless-aws-lambda.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ curl -d '{"query":"quantity:>5", "max_hits": 10}' \
143143
--compressed
144144
```
145145

146+
:::note
146147
The index is not created until the first run of the Indexer, so you might need a few minutes before your first search request succeeds. The API Gateway key configuration also takes a minute or two to propagate, so the first requests might receive an authorization error response.
148+
:::
147149

148150
Because the JSON query responses are often quite verbose, the Searcher Lambda always compresses them before sending them on the wire. It is crucial to keep this size low, both to avoid hitting the Lambda payload size limit of 6MB and to avoid egress costs at around $0.10/GB. We do this regardless of the `accept-encoding` request header, this is why the `--compressed` flag needs to be set to `curl`.
149151

0 commit comments

Comments
 (0)