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