diff --git a/deployment/lambda.tf b/deployment/lambda.tf new file mode 100644 index 0000000..6903652 --- /dev/null +++ b/deployment/lambda.tf @@ -0,0 +1,53 @@ +provider "aws" { + region = "us-east-1" # Change if needed +} + +resource "aws_sqs_queue" "my_queue" { + name = "my-lambda-queue" +} + +resource "aws_iam_role" "lambda_exec_role" { + name = "lambda-exec-role" + + assume_role_policy = jsonencode({ + Version = "2012-10-17" + Statement = [ + { + Action = "sts:AssumeRole" + Effect = "Allow" + Principal = { + Service = "lambda.amazonaws.com" + } + } + ] + }) +} + +resource "aws_iam_role_policy_attachment" "lambda_basic_execution" { + role = aws_iam_role.lambda_exec_role.name + policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" +} + +resource "aws_iam_role_policy_attachment" "sqs_access" { + role = aws_iam_role.lambda_exec_role.name + policy_arn = "arn:aws:iam::aws:policy/AWSLambdaSQSQueueExecutionRole" +} + +resource "aws_ecr_repository" "lambda_repo" { + name = "lambda-hello-world" +} + +resource "aws_lambda_function" "lambda_docker" { + function_name = "docker-hello-world" + role = aws_iam_role.lambda_exec_role.arn + package_type = "Image" + image_uri = "${aws_ecr_repository.lambda_repo.repository_url}:latest" + + timeout = 10 +} + +resource "aws_lambda_event_source_mapping" "sqs_trigger" { + event_source_arn = aws_sqs_queue.my_queue.arn + function_name = aws_lambda_function.lambda_docker.arn + batch_size = 1 +} diff --git a/deployment/lamda/Dockerfile b/deployment/lamda/Dockerfile new file mode 100644 index 0000000..4b57200 --- /dev/null +++ b/deployment/lamda/Dockerfile @@ -0,0 +1,8 @@ +# AWS Lambda python pacakge +FROM public.ecr.aws/lambda/python:3.11 + +# Copy function code +COPY app.py ./ + +# Set the CMD to your handler +CMD ["app.handler"] \ No newline at end of file diff --git a/deployment/lamda/app.py b/deployment/lamda/app.py new file mode 100644 index 0000000..696c654 --- /dev/null +++ b/deployment/lamda/app.py @@ -0,0 +1,10 @@ +""" +A quick example of lambda working a function in python +""" + +def handler(event, context): + print("Hello from Python function. This shold be running from a dockerfile env and executed on a aws lambda!") + return { + 'statusCode': 200, + 'body': 'Hello World' + } \ No newline at end of file