diff --git a/notebook.ipynb b/notebook.ipynb index 5250ccf..af23507 100644 --- a/notebook.ipynb +++ b/notebook.ipynb @@ -89,6 +89,19 @@ " })\n", "}, {})" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "be25af3b-f164-4ecf-82c8-d0657290bab1", + "metadata": {}, + "outputs": [], + "source": [ + "src.event_gate_lambda.lambda_handler({\n", + " \"httpMethod\": \"POST\",\n", + " \"resource\": \"/Terminate\"\n", + "}, {})" + ] } ], "metadata": { diff --git a/src/event_gate_lambda.py b/src/event_gate_lambda.py index eafa15b..b795005 100644 --- a/src/event_gate_lambda.py +++ b/src/event_gate_lambda.py @@ -15,6 +15,7 @@ # import base64 import json +import sys import urllib3 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) @@ -118,4 +119,6 @@ def lambda_handler(event, context): return getTopicSchema(event["pathParameters"]["topicName"]) if event["httpMethod"] == "POST": return postTopicMessage(event["pathParameters"]["topicName"], json.loads(event["body"]), event["headers"]["bearer"]) - return {"statusCode": 404} \ No newline at end of file + if event["resource"] == "/Terminate": + sys.exit("TERMINATING") + return {"statusCode": 404} diff --git a/terraform/api_gateway.tf b/terraform/api_gateway.tf index a0fa86d..1714a3e 100644 --- a/terraform/api_gateway.tf +++ b/terraform/api_gateway.tf @@ -106,6 +106,28 @@ resource "aws_api_gateway_integration" "event_gate_api_topic_name_post_integrati uri = aws_lambda_function.event_gate_lambda.invoke_arn } +resource "aws_api_gateway_resource" "event_gate_api_terminate" { + rest_api_id = aws_api_gateway_rest_api.event_gate_api.id + parent_id = aws_api_gateway_rest_api.event_gate_api.root_resource_id + path_part = "Terminate" +} + +resource "aws_api_gateway_method" "event_gate_api_terminate_post" { + rest_api_id = aws_api_gateway_rest_api.event_gate_api.id + resource_id = aws_api_gateway_resource.event_gate_api_terminate.id + authorization = "NONE" + http_method = "POST" +} + +resource "aws_api_gateway_integration" "event_gate_api_terminate_post_integration" { + rest_api_id = aws_api_gateway_rest_api.event_gate_api.id + resource_id = aws_api_gateway_resource.event_gate_api_terminate.id + http_method = aws_api_gateway_method.event_gate_api_terminate_post.http_method + integration_http_method = "POST" + type = "AWS_PROXY" + uri = aws_lambda_function.event_gate_lambda.invoke_arn +} + resource "aws_lambda_permission" "event_gate_api_lambda_permissions" { action = "lambda:InvokeFunction" function_name = aws_lambda_function.event_gate_lambda.function_name @@ -120,7 +142,8 @@ resource "aws_api_gateway_deployment" "event_gate_api_deployment" { aws_api_gateway_integration.event_gate_api_token_get_integration, aws_api_gateway_integration.event_gate_api_topics_get_integration, aws_api_gateway_integration.event_gate_api_topic_name_get_integration, - aws_api_gateway_integration.event_gate_api_topic_name_post_integration + aws_api_gateway_integration.event_gate_api_topic_name_post_integration, + aws_api_gateway_integration.event_gate_api_terminate_post_integration ])) } lifecycle {