diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 445d2e6..d050974 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -7,6 +7,7 @@ "postStartCommand": "bash .devcontainer/post-install.sh", "mounts": [ "source=${localEnv:HOME},target=/workspaces/home,type=bind", + // Make sure you aws credentials are saved at ~/.aws "source=${localEnv:HOME}/.aws/,target=/home/vscode/.aws/,type=bind" ], "customizations": { diff --git a/deployment/lambda.tf b/deployment/lambda.tf index 6903652..dc24055 100644 --- a/deployment/lambda.tf +++ b/deployment/lambda.tf @@ -1,11 +1,9 @@ -provider "aws" { - region = "us-east-1" # Change if needed -} - +# Create an SQS queue that will trigger the Lambda resource "aws_sqs_queue" "my_queue" { name = "my-lambda-queue" } +# IAM role that the Lambda function will assume to get permissions resource "aws_iam_role" "lambda_exec_role" { name = "lambda-exec-role" @@ -23,20 +21,25 @@ resource "aws_iam_role" "lambda_exec_role" { }) } +# Attach the basic execution policy (writes logs to CloudWatch) to the Lambda role 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" } +# Give Lambda permission to poll and process SQS messages resource "aws_iam_role_policy_attachment" "sqs_access" { role = aws_iam_role.lambda_exec_role.name policy_arn = "arn:aws:iam::aws:policy/AWSLambdaSQSQueueExecutionRole" } + +# Create an ECR repository to store the Docker image for the Lambda function resource "aws_ecr_repository" "lambda_repo" { name = "lambda-hello-world" } +# Define the Lambda function using a Docker image from ECR resource "aws_lambda_function" "lambda_docker" { function_name = "docker-hello-world" role = aws_iam_role.lambda_exec_role.arn @@ -46,6 +49,7 @@ resource "aws_lambda_function" "lambda_docker" { timeout = 10 } +# Connect the SQS queue to the Lambda so it gets triggered by incoming messages 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