lamda addition

This commit is contained in:
Jun-te Kim 2025-07-14 12:34:14 +00:00
parent dd49a9e597
commit 04a7c68b5b
2 changed files with 9 additions and 4 deletions

View file

@ -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": {

View file

@ -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