diff --git a/deployment/database/provider.tf b/deployment/database/provider.tf index 46e887f..009e591 100644 --- a/deployment/database/provider.tf +++ b/deployment/database/provider.tf @@ -2,14 +2,14 @@ terraform { required_providers { aws = { source = "hashicorp/aws" - version = "~> 4.16" + version = "~> 6.3.0" } } backend "s3" { bucket = "survey-extractor-tf-state" region = "eu-west-2" profile = "domna.dev" # /home/vscode/aws/credentials - key = "terraform.tfstate" + key = "env:/dev/terraform.tfstate" } required_version = ">= 1.2.0" diff --git a/deployment/lambda/extractor_and_loader/Dockerfile b/deployment/lambda/extractor_and_loader/Dockerfile deleted file mode 100644 index 609981a..0000000 --- a/deployment/lambda/extractor_and_loader/Dockerfile +++ /dev/null @@ -1,25 +0,0 @@ -FROM public.ecr.aws/lambda/python:3.12 - -# Install Poetry (you could pin a version if you like) -RUN curl -sSL https://install.python-poetry.org | python3 - - -# Add Poetry to PATH -ENV PATH="/root/.local/bin:$PATH" - -# Set working directory -WORKDIR /var/task - -# Copy Poetry files first to leverage Docker layer caching -COPY pyproject.toml poetry.lock README.md ./ -COPY etl/ etl/ - - -# Install dependencies into /var/task -RUN poetry config virtualenvs.create false \ - && poetry install --only main --no-interaction --no-ansi - -# Copy app code -COPY deployment/extractor_and_loader/app.py ./ - -# Set Lambda handler -CMD ["app.handler"] \ No newline at end of file diff --git a/deployment/lambda/extractor_and_loader/app.py b/deployment/lambda/extractor_and_loader/app.py deleted file mode 100644 index 4a281ed..0000000 --- a/deployment/lambda/extractor_and_loader/app.py +++ /dev/null @@ -1,30 +0,0 @@ -""" -A quick example of lambda working a function in python -""" -from etl.read_stuff_from_s3_example import print_hello_from_etl_module - -def handler(event, context): - print("Outside try statment") - print_hello_from_etl_module() - try: - print("show me something.. anything...") - s3_uri = event.get("file_location") - if not s3_uri: - print("failed to get s3_uri") - return { - "statusCode": 400, - "body": "Missing 'file_location' in event" - } - print(f"s3 uri is {s3_uri}") - - return { - "statusCode": 200, - "body": f"s3 uri {s3_uri}" - } - - except Exception as e: - print(f"❌ Error: {e}") - return { - "statusCode": 500, - "body": str(e) - } \ No newline at end of file diff --git a/deployment/lambda/lambda_example/Dockerfile b/deployment/lambda/lambda_example/Dockerfile deleted file mode 100644 index c1caf03..0000000 --- a/deployment/lambda/lambda_example/Dockerfile +++ /dev/null @@ -1,8 +0,0 @@ -# AWS Lambda python pacakge -FROM public.ecr.aws/lambda/python:3.11 - -# Copy function code -COPY deployment/lambda_example/app.py ./ - -# Set the CMD to your handler -CMD ["app.handler"] \ No newline at end of file diff --git a/deployment/lambda/lambda_example/app.py b/deployment/lambda/lambda_example/app.py deleted file mode 100644 index 390fa63..0000000 --- a/deployment/lambda/lambda_example/app.py +++ /dev/null @@ -1,25 +0,0 @@ -""" -A quick example of lambda working a function in python -""" - -def handler(event, context): - try: - s3_uri = event.get("file_location") - if not s3_uri: - return { - "statusCode": 400, - "body": "Missing 'file_location' in event" - } - print(f"s3 uri is {s3_uri}") - - return { - "statusCode": 200, - "body": f"s3 uri {s3_uri}" - } - - except Exception as e: - print(f"❌ Error: {e}") - return { - "statusCode": 500, - "body": str(e) - } \ No newline at end of file diff --git a/deployment/lambda/lambda_example/lambda_example_and_config.tf b/deployment/lambda/lambda_example/lambda_example_and_config.tf index b020e2d..581d038 100644 --- a/deployment/lambda/lambda_example/lambda_example_and_config.tf +++ b/deployment/lambda/lambda_example/lambda_example_and_config.tf @@ -1,33 +1,9 @@ -# IAM role for both Lambdas (can be shared) -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" -} - # SQS queue for lambda_example resource "aws_sqs_queue" "lambda_example_queue" { name = "lambda-example-queue" } -# ECR repo for lambda_example -resource "aws_ecr_repository" "lambda_example" { - name = "lambda_example" -} + # Custom IAM policy specific to lambda_example resource "aws_iam_policy" "lambda_example_policy" { @@ -84,23 +60,3 @@ resource "aws_lambda_event_source_mapping" "lambda_example_trigger" { batch_size = 1 } -# ECR policy to allow Lambda access -resource "aws_ecr_repository_policy" "lambda_example_ecr_access" { - repository = aws_ecr_repository.lambda_example.name - - policy = jsonencode({ - Version = "2008-10-17", - Statement = [{ - Sid = "AllowLambdaPull", - Effect = "Allow", - Principal = { - Service = "lambda.amazonaws.com" - }, - Action = [ - "ecr:GetDownloadUrlForLayer", - "ecr:BatchGetImage", - "ecr:BatchCheckLayerAvailability" - ] - }] - }) -} diff --git a/deployment/lambda/lambda_shared/lambda_shared_config.tf b/deployment/lambda/lambda_shared/lambda_shared_config.tf new file mode 100644 index 0000000..1533dfb --- /dev/null +++ b/deployment/lambda/lambda_shared/lambda_shared_config.tf @@ -0,0 +1,21 @@ +# IAM role for both Lambdas (can be shared) +resource "aws_iam_role" "lambda_exec_role" { + name = "lambda-exec-role" + + assume_role_policy = jsonencode({ + Version = "2012-10-17", + Statement = [{ + Effect = "Allow", + Principal = { + Service = "lambda.amazonaws.com" + }, + Action = "sts:AssumeRole" + }] + }) +} + + +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" +} \ No newline at end of file diff --git a/deployment/lambda/lambda_shared/lambda_shared_condig.tf b/deployment/lambda/lambda_shared/main.tf similarity index 100% rename from deployment/lambda/lambda_shared/lambda_shared_condig.tf rename to deployment/lambda/lambda_shared/main.tf diff --git a/deployment/lambda/lambda_shared/provider.tf b/deployment/lambda/lambda_shared/provider.tf new file mode 100644 index 0000000..fa6a5d4 --- /dev/null +++ b/deployment/lambda/lambda_shared/provider.tf @@ -0,0 +1,16 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 6.3.0" + } + } + backend "s3" { + bucket = "survey-extractor-tf-state" + region = "eu-west-2" + profile = "domna.dev" # /home/vscode/aws/credentials + key = "env:/dev/lambda/lambda_share_configuration.tfstate" + } + + required_version = ">= 1.2.0" +}