From 9d73dba85f8297620ea2ea6aae69a012a9e61158 Mon Sep 17 00:00:00 2001 From: "Miroslav Chomut (CZ)" Date: Thu, 5 Sep 2024 10:57:47 +0200 Subject: [PATCH 1/2] #9 Forcefully terminate environment on request --- notebook.ipynb | 13 +++++++++++++ src/event_gate_lambda.py | 3 +++ terraform/api_gateway.tf | 25 ++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) 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..ed69182 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"]) + if event["resource"] == "/Terminate": + sys.exit("TERMINATING") return {"statusCode": 404} \ No newline at end of file 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 { From e58ee1f0753d84092eece2b9e474e97cdb56c000 Mon Sep 17 00:00:00 2001 From: "Miroslav Chomut (CZ)" Date: Wed, 18 Sep 2024 08:24:44 +0200 Subject: [PATCH 2/2] Update src/event_gate_lambda.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Saša Zejnilović --- src/event_gate_lambda.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/event_gate_lambda.py b/src/event_gate_lambda.py index ed69182..b795005 100644 --- a/src/event_gate_lambda.py +++ b/src/event_gate_lambda.py @@ -121,4 +121,4 @@ def lambda_handler(event, context): return postTopicMessage(event["pathParameters"]["topicName"], json.loads(event["body"]), event["headers"]["bearer"]) if event["resource"] == "/Terminate": sys.exit("TERMINATING") - return {"statusCode": 404} \ No newline at end of file + return {"statusCode": 404}