|
| 1 | +terraform { |
| 2 | + required_providers { |
| 3 | + aws = { |
| 4 | + source = "hashicorp/aws" |
| 5 | + version = "~>5.41" |
| 6 | + } |
| 7 | + } |
| 8 | + |
| 9 | + required_version = ">=1.2.0" |
| 10 | +} |
| 11 | + |
| 12 | +provider "aws" { |
| 13 | + region = var.aws_region_name |
| 14 | +} |
| 15 | + |
| 16 | +data "archive_file" "lambda_handler_zip_file" { |
| 17 | + type = "zip" |
| 18 | + source_file = "${path.module}/handler.py" |
| 19 | + output_path = "${path.module}/sqs-lambda-s3.zip" |
| 20 | +} |
| 21 | + |
| 22 | +# Lambda function |
| 23 | +resource "aws_lambda_function" "event-processor" { |
| 24 | + function_name = "event-processor" |
| 25 | + filename = data.archive_file.lambda_handler_zip_file.output_path |
| 26 | + source_code_hash = filebase64sha256(data.archive_file.lambda_handler_zip_file.output_path) |
| 27 | + handler = "handler.lambda_handler" |
| 28 | + runtime = "python3.12" |
| 29 | + role = aws_iam_role.event-processor-exec-role.arn |
| 30 | + environment { |
| 31 | + variables = { |
| 32 | + AWS_REGION_NAME = var.aws_region_name |
| 33 | + S3_BUCKET_NAME = var.s3_bucket_name |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +# Lambda execution role |
| 39 | +resource "aws_iam_role" "event-processor-exec-role" { |
| 40 | + name = "event-processor-exec-role" |
| 41 | + assume_role_policy = jsonencode({ |
| 42 | + Version = "2012-10-17", |
| 43 | + Statement = [ |
| 44 | + { |
| 45 | + Effect = "Allow" |
| 46 | + Principal = { |
| 47 | + Service = "lambda.amazonaws.com" |
| 48 | + } |
| 49 | + Action = [ |
| 50 | + "sts:AssumeRole" |
| 51 | + ] |
| 52 | + } |
| 53 | + ] |
| 54 | + }) |
| 55 | +} |
| 56 | + |
| 57 | +# Lambda exec role policy |
| 58 | +resource "aws_iam_policy" "event-processor-policy" { |
| 59 | + name = "event-processor-policy" |
| 60 | + policy = jsonencode({ |
| 61 | + Version = "2012-10-17" |
| 62 | + Statement = [ |
| 63 | + { |
| 64 | + Effect = "Allow" |
| 65 | + Action = [ |
| 66 | + "sts:AssumeRole" |
| 67 | + ] |
| 68 | + Resource = [aws_lambda_function.event-processor.arn] |
| 69 | + }, |
| 70 | + { |
| 71 | + Effect = "Allow" |
| 72 | + Action = [ |
| 73 | + "sqs:ReceiveMessage", |
| 74 | + "sqs:GetQueueAttributes", |
| 75 | + "sqs:DeleteMessage" |
| 76 | + ] |
| 77 | + Resource = aws_sqs_queue.event-collector.arn |
| 78 | + }, |
| 79 | + { |
| 80 | + Effect = "Allow" |
| 81 | + Action = [ |
| 82 | + "s3:PutObject" |
| 83 | + ] |
| 84 | + Resource = [ |
| 85 | + "${aws_s3_bucket.event-storage.arn}", |
| 86 | + "${aws_s3_bucket.event-storage.arn}/*", |
| 87 | + ] |
| 88 | + } |
| 89 | + ] |
| 90 | + }) |
| 91 | +} |
| 92 | + |
| 93 | +# Attach policy to Lambda execution role for SQS permissions |
| 94 | +resource "aws_iam_role_policy_attachment" "lambda-exec-role-policy" { |
| 95 | + policy_arn = aws_iam_policy.event-processor-policy.arn |
| 96 | + role = aws_iam_role.event-processor-exec-role.name |
| 97 | +} |
| 98 | + |
| 99 | +# Attach policy to Lambda exec role for CloudWatch permissions |
| 100 | +resource "aws_iam_role_policy_attachment" "lambda-policy" { |
| 101 | + policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" |
| 102 | + role = aws_iam_role.event-processor-exec-role.name |
| 103 | +} |
| 104 | + |
| 105 | +# Event source mapping to create a trigger for Lambda to read from SQS queue |
| 106 | +resource "aws_lambda_event_source_mapping" "event-processor-event-src-map" { |
| 107 | + function_name = aws_lambda_function.event-processor.arn |
| 108 | + event_source_arn = aws_sqs_queue.event-collector.arn |
| 109 | + enabled = true |
| 110 | + depends_on = [ |
| 111 | + aws_lambda_function.event-processor, |
| 112 | + aws_sqs_queue.event-collector, |
| 113 | + aws_sqs_queue_policy.event-collector-policy, |
| 114 | + aws_iam_policy.event-processor-policy |
| 115 | + ] |
| 116 | +} |
| 117 | + |
| 118 | +# SQS Queue |
| 119 | +resource "aws_sqs_queue" "event-collector" { |
| 120 | + name = "event-collector-queue" |
| 121 | + max_message_size = 2048 |
| 122 | +} |
| 123 | + |
| 124 | +# SQS queue policy |
| 125 | +resource "aws_sqs_queue_policy" "event-collector-policy" { |
| 126 | + queue_url = aws_sqs_queue.event-collector.url |
| 127 | + policy = jsonencode({ |
| 128 | + Version = "2012-10-17" |
| 129 | + Statement = [ |
| 130 | + { |
| 131 | + Effect = "Allow" |
| 132 | + Principal = { |
| 133 | + Service = "lambda.amazonaws.com" |
| 134 | + } |
| 135 | + Action = [ |
| 136 | + "sqs:ReceiveMessage", |
| 137 | + "sqs:GetQueueAttributes", |
| 138 | + "sqs:DeleteMessage" |
| 139 | + ] |
| 140 | + Resource = aws_sqs_queue.event-collector.arn |
| 141 | + Condition = { |
| 142 | + ArnEquals = { |
| 143 | + "aws:SourceArn" = aws_lambda_function.event-processor.arn |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + ] |
| 148 | + }) |
| 149 | + |
| 150 | + depends_on = [ |
| 151 | + aws_sqs_queue.event-collector, |
| 152 | + aws_lambda_function.event-processor |
| 153 | + ] |
| 154 | +} |
| 155 | + |
| 156 | +# S3 bucket |
| 157 | +resource "aws_s3_bucket" "event-storage" { |
| 158 | + bucket = var.s3_bucket_name |
| 159 | + force_destroy = true |
| 160 | + tags = { |
| 161 | + Name = "event-storage" |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +# Bucket policy document |
| 166 | +data "aws_iam_policy_document" "bucket-policy" { |
| 167 | + statement { |
| 168 | + effect = "Allow" |
| 169 | + actions = ["s3:PutObject"] |
| 170 | + principals { |
| 171 | + type = "Service" |
| 172 | + identifiers = [ |
| 173 | + "lambda.amazonaws.com" |
| 174 | + ] |
| 175 | + } |
| 176 | + resources = [ |
| 177 | + "${aws_s3_bucket.event-storage.arn}", |
| 178 | + "${aws_s3_bucket.event-storage.arn}/*", |
| 179 | + ] |
| 180 | + condition { |
| 181 | + test = "ArnEquals" |
| 182 | + variable = "aws:SourceArn" |
| 183 | + values = ["${aws_lambda_function.event-processor.arn}"] |
| 184 | + } |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +# Bucket policy |
| 189 | +resource "aws_s3_bucket_policy" "event-storage-bucket-policy" { |
| 190 | + bucket = aws_s3_bucket.event-storage.id |
| 191 | + policy = data.aws_iam_policy_document.bucket-policy.json |
| 192 | +} |
0 commit comments