|
| 1 | +terraform { |
| 2 | + required_providers { |
| 3 | + aws = { |
| 4 | + source = "hashicorp/aws" |
| 5 | + version = "~> 5.57.0" |
| 6 | + } |
| 7 | + } |
| 8 | + |
| 9 | + required_version = ">= 0.14.9" |
| 10 | +} |
| 11 | + |
| 12 | +data "aws_caller_identity" "current" {} |
| 13 | +data "aws_region" "current" {} |
| 14 | + |
| 15 | + |
| 16 | +################################################################# |
| 17 | +# S3 Buckets |
| 18 | +################################################################# |
| 19 | +# Create a new Source S3 bucket |
| 20 | +resource "aws_s3_bucket" "MySourceS3Bucket" { |
| 21 | + bucket_prefix = "s3-sqs-lambda-tf-sources3bucket-" |
| 22 | +} |
| 23 | + |
| 24 | +# Send notifications to SQS for all events in the bucket |
| 25 | +resource "aws_s3_bucket_notification" "MySourceS3BucketNotification" { |
| 26 | + bucket = aws_s3_bucket.MySourceS3Bucket.id |
| 27 | + |
| 28 | + queue { |
| 29 | + queue_arn = aws_sqs_queue.MyHandlerQueue.arn |
| 30 | + events = [ |
| 31 | + "s3:ObjectCreated:*" |
| 32 | + ] |
| 33 | + filter_suffix = ".jpg" |
| 34 | + } |
| 35 | + |
| 36 | +} |
| 37 | + |
| 38 | +################################################################# |
| 39 | +# SQS - Queue |
| 40 | +################################################################# |
| 41 | +# Create SQS - Queue |
| 42 | +resource "aws_sqs_queue" "MyHandlerQueue" { |
| 43 | + name = "s3-sqs-lambda-tf-SQSResizerQueue" |
| 44 | +} |
| 45 | + |
| 46 | +# Create SQS - Policy |
| 47 | +resource "aws_sqs_queue_policy" "MyHandlerQueuePolicy" { |
| 48 | + queue_url = aws_sqs_queue.MyHandlerQueue.id |
| 49 | + |
| 50 | + policy = <<POLICY |
| 51 | +{ |
| 52 | + "Version": "2012-10-17", |
| 53 | + "Id": "QueuePolicy", |
| 54 | + "Statement": [ |
| 55 | + { |
| 56 | + "Sid": "Allow-SendMessage-To-Queue-From-S3-Event-Notification", |
| 57 | + "Effect": "Allow", |
| 58 | + "Principal": { |
| 59 | + "Service": "s3.amazonaws.com" |
| 60 | + }, |
| 61 | + "Action": "sqs:SendMessage", |
| 62 | + "Resource": "arn:aws:sqs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${aws_sqs_queue.MyHandlerQueue.name}", |
| 63 | + "Condition": { |
| 64 | + "StringEquals": { |
| 65 | + "aws:SourceAccount": "${data.aws_caller_identity.current.account_id}" |
| 66 | + }, |
| 67 | + "ArnLike": { |
| 68 | + "aws:SourceArn": "arn:aws:s3:::${aws_s3_bucket.MySourceS3Bucket.id}" |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + ] |
| 73 | +} |
| 74 | +POLICY |
| 75 | +} |
| 76 | + |
| 77 | + |
| 78 | +################################################################# |
| 79 | +# Lambda Function |
| 80 | +################################################################# |
| 81 | +# Creating Lambda Function |
| 82 | +resource "aws_lambda_function" "MyHandlerFunction-Function" { |
| 83 | + filename = data.archive_file.LambdaZipFile.output_path |
| 84 | + function_name = "s3-sqs-lambda-tf-LambdaFunction" |
| 85 | + role = aws_iam_role.MyHandlerFunction-Role.arn |
| 86 | + handler = "app.handler" |
| 87 | + runtime = "nodejs20.x" |
| 88 | +} |
| 89 | + |
| 90 | +# Create a zip file from the Lambda source code |
| 91 | +data "archive_file" "LambdaZipFile" { |
| 92 | + type = "zip" |
| 93 | + source_file = "${path.module}/src/app.mjs" |
| 94 | + output_path = "${path.module}/lambda-src.zip" |
| 95 | +} |
| 96 | + |
| 97 | +# Creating SQS Queue Trigger for Lambda Function |
| 98 | +resource "aws_lambda_event_source_mapping" "MyHandlerFunction-Function-to-SQS" { |
| 99 | + event_source_arn = aws_sqs_queue.MyHandlerQueue.arn |
| 100 | + function_name = aws_lambda_function.MyHandlerFunction-Function.arn |
| 101 | +} |
| 102 | + |
| 103 | +# Creating IAM Role for Lambda Function |
| 104 | +resource "aws_iam_role" "MyHandlerFunction-Role" { |
| 105 | + name = "s3-sqs-lambda-tf-MyHandlerFunction-Role" |
| 106 | + |
| 107 | + assume_role_policy = jsonencode({ |
| 108 | + Version = "2012-10-17" |
| 109 | + Statement = [ |
| 110 | + { |
| 111 | + Action = "sts:AssumeRole" |
| 112 | + Effect = "Allow" |
| 113 | + Principal = { |
| 114 | + Service = "lambda.amazonaws.com" |
| 115 | + } |
| 116 | + } |
| 117 | + ] |
| 118 | + }) |
| 119 | +} |
| 120 | + |
| 121 | +# Creating IAM Policies for Lambda |
| 122 | +resource "aws_iam_role_policy" "MyHandlerFunction-Policy-source" { |
| 123 | + name = "s3-sqs-lambda-tf-MyHandlerFunction-Role" |
| 124 | + policy = jsonencode( |
| 125 | +{ |
| 126 | + "Statement": [ |
| 127 | + { |
| 128 | + "Action": [ |
| 129 | + "s3:GetObject", |
| 130 | + "s3:ListBucket", |
| 131 | + "s3:GetBucketLocation", |
| 132 | + "s3:GetObjectVersion", |
| 133 | + "s3:GetLifecycleConfiguration" |
| 134 | + ], |
| 135 | + "Resource": [ |
| 136 | + "arn:aws:s3:::${aws_s3_bucket.MySourceS3Bucket.id}", |
| 137 | + "arn:aws:s3:::${aws_s3_bucket.MySourceS3Bucket.id}/*" |
| 138 | + ], |
| 139 | + "Effect": "Allow" |
| 140 | + } |
| 141 | + ] |
| 142 | +} |
| 143 | + ) |
| 144 | + role = aws_iam_role.MyHandlerFunction-Role.name |
| 145 | +} |
| 146 | + |
| 147 | +resource "aws_iam_role_policy_attachment" "AWSLambdaBasicExecutionRole" { |
| 148 | + policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" |
| 149 | + role = "${aws_iam_role.MyHandlerFunction-Role.name}" |
| 150 | +} |
| 151 | + |
| 152 | +resource "aws_iam_role_policy_attachment" "AWSLambdaSQSQueueExecutionRole" { |
| 153 | + policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaSQSQueueExecutionRole" |
| 154 | + role = "${aws_iam_role.MyHandlerFunction-Role.name}" |
| 155 | +} |
| 156 | + |
| 157 | + |
| 158 | +################################################################# |
| 159 | +# Outputs |
| 160 | +################################################################# |
| 161 | +# Displaying the SQS Queue, SourceS3 buckets and Lambda Function |
| 162 | +output "SQSQueueName" { |
| 163 | + value = aws_sqs_queue.MyHandlerQueue.name |
| 164 | + description = "SQS Queue for queuing the s3 events" |
| 165 | +} |
| 166 | +output "SourceS3BucketName" { |
| 167 | + value = aws_s3_bucket.MySourceS3Bucket.id |
| 168 | + description = "S3 Bucket for object storage" |
| 169 | +} |
| 170 | +output "LambdaFunctionArn" { |
| 171 | + value = aws_lambda_function.MyHandlerFunction-Function.arn |
| 172 | + description = "HandlerFunction function Arn" |
| 173 | +} |
0 commit comments