Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/event_gate_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import os
import base64
import json
import logging
Expand All @@ -30,7 +31,8 @@
from confluent_kafka import Producer

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
log_level = os.environ.get('LOG_LEVEL', 'INFO')
logger.setLevel(log_level)
logger.addHandler(logging.StreamHandler())

with open("conf/api.yaml", "r") as file:
Expand Down Expand Up @@ -68,11 +70,11 @@
else:
EVENT_BUS_ARN = ""

logger.info("Loaded configs")
logger.debug("Loaded configs")

token_public_key_encoded = requests.get(CONFIG["token_public_key_url"], verify=False).json()["key"]
TOKEN_PUBLIC_KEY = serialization.load_der_public_key(base64.b64decode(token_public_key_encoded))
logger.info("Loaded token public key")
logger.debug("Loaded token public key")

producer_config = {"bootstrap.servers": CONFIG["kafka_bootstrap_server"]}
if "kafka_sasl_kerberos_principal" in CONFIG and "kafka_ssl_key_path" in CONFIG:
Expand All @@ -87,13 +89,13 @@
"ssl.key.location": CONFIG["kafka_ssl_key_path"],
"ssl.key.password": CONFIG["kafka_ssl_key_password"]
})
logger.info("producer will use SASL_SSL")
logger.debug("producer will use SASL_SSL")

kafka_producer = Producer(producer_config)
logger.info("Initialized kafka producer")
logger.debug("Initialized kafka producer")

def kafka_write(topicName, message):
logger.info(f"Sending to kafka {topicName}")
logger.debug(f"Sending to kafka {topicName}")
error = []
kafka_producer.produce(topicName,
key="",
Expand All @@ -105,10 +107,10 @@ def kafka_write(topicName, message):

def event_bridge_write(topicName, message):
if not EVENT_BUS_ARN:
logger.info("No EventBus Arn - skipping")
logger.debug("No EventBus Arn - skipping")
return

logger.info(f"Sending to eventBridge {topicName}")
logger.debug(f"Sending to eventBridge {topicName}")
response = aws_eventbridge.put_events(
Entries=[
{
Expand All @@ -129,22 +131,22 @@ def get_api():
}

def get_token():
logger.info("Handling GET Token")
logger.debug("Handling GET Token")
return {
"statusCode": 303,
"headers": {"Location": TOKEN_PROVIDER_URL}
}

def get_topics():
logger.info("Handling GET Topics")
logger.debug("Handling GET Topics")
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json.dumps([topicName for topicName in TOPICS])
}

def get_topic_schema(topicName):
logger.info(f"Handling GET TopicSchema({topicName})")
logger.debug(f"Handling GET TopicSchema({topicName})")
if topicName not in TOPICS:
return { "statusCode": 404 }

Expand All @@ -155,7 +157,7 @@ def get_topic_schema(topicName):
}

def post_topic_message(topicName, topicMessage, tokenEncoded):
logger.info(f"Handling POST {topicName}")
logger.debug(f"Handling POST {topicName}")
try:
token = jwt.decode(tokenEncoded, TOKEN_PUBLIC_KEY, algorithms=["RS256"])
except Exception as e:
Expand Down
15 changes: 14 additions & 1 deletion terraform/lambda.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,24 @@ resource "aws_vpc_security_group_egress_rule" "allow_all_traffic_ipv4" {
ip_protocol = "-1"
}

data "aws_s3_object" "event_gate_lambda_zip" {
count = var.lambda_package_type == "Zip" ? 1 : 0
bucket = var.lambda_src_s3_bucket
key = var.lambda_src_s3_key
}

resource "aws_lambda_function" "event_gate_lambda" {
function_name = "${var.resource_prefix}event-gate-lambda"
role = var.lambda_role_arn
architectures = ["x86_64"]
timeout = 60
runtime = "python3.11"
package_type = var.lambda_package_type

s3_bucket = var.lambda_package_type == "Zip" ? var.lambda_src_s3_bucket : null
s3_key = var.lambda_package_type == "Zip" ? var.lambda_src_s3_key : null
handler = var.lambda_package_type == "Zip" ? "event_gate_lambda.lambda_handler" : null
source_code_hash = var.lambda_package_type == "Zip" ? filebase64sha256("s3://${var.lambda_src_s3_bucket}/${var.lambda_src_s3_key}") : null
source_code_hash = var.lambda_package_type == "Zip" ? data.aws_s3_object.event_gate_lambda_zip[0].etag : null

image_uri = var.lambda_package_type == "Image" ? var.lambda_src_ecr_image : null

Expand All @@ -30,4 +37,10 @@ resource "aws_lambda_function" "event_gate_lambda" {
security_group_ids = [aws_security_group.event_gate_sg.id]
}
tags = {"BuiltBy" = "Terraform"}

environment {
variables = {
LOG_LEVEL = "INFO"
}
}
}