@@ -10,6 +10,7 @@ provider "aws" {
1010 region = " eu-central-1"
1111}
1212
13+ # S3 bucket
1314resource "aws_s3_bucket" "shipment_picture_bucket" {
1415 bucket = " shipment-picture-bucket"
1516 force_destroy = true
@@ -18,6 +19,7 @@ resource "aws_s3_bucket" "shipment_picture_bucket" {
1819 }
1920}
2021
22+ # DynamoDB table creation
2123resource "aws_dynamodb_table" "shipment" {
2224 name = " shipment"
2325 read_capacity = 10
@@ -36,14 +38,15 @@ resource "aws_dynamodb_table" "shipment" {
3638 stream_view_type = " NEW_AND_OLD_IMAGES"
3739}
3840
41+ # Populate the table
3942resource "aws_dynamodb_table_item" "shipment" {
4043 for_each = local. tf_data
4144 table_name = aws_dynamodb_table. shipment . name
4245 hash_key = " shipmentId"
4346 item = jsonencode (each. value )
4447}
4548
46-
49+ # Define a bucket for the lambda zip
4750resource "aws_s3_bucket" "lambda_code_bucket" {
4851 bucket = " shipment-picture-lambda-validator-bucket"
4952 force_destroy = true
@@ -52,12 +55,14 @@ resource "aws_s3_bucket" "lambda_code_bucket" {
5255 }
5356}
5457
58+ # Lambda source code
5559resource "aws_s3_bucket_object" "lambda_code" {
5660 source = " ../../shipment-picture-lambda-validator/target/shipment-picture-lambda-validator.jar"
5761 bucket = aws_s3_bucket. lambda_code_bucket . id
5862 key = " shipment-picture-lambda-validator.jar"
5963}
6064
65+ # Lambda definition
6166resource "aws_lambda_function" "shipment_picture_lambda_validator" {
6267 function_name = " shipment-picture-lambda-validator"
6368 handler = " dev.ancaghenade.shipmentpicturelambdavalidator.ServiceHandler::handleRequest"
@@ -74,6 +79,7 @@ resource "aws_lambda_function" "shipment_picture_lambda_validator" {
7479 }
7580}
7681
82+ # Define trigger for S3
7783resource "aws_s3_bucket_notification" "demo_bucket_notification" {
7884 bucket = aws_s3_bucket. shipment_picture_bucket . id
7985 lambda_function {
@@ -82,6 +88,7 @@ resource "aws_s3_bucket_notification" "demo_bucket_notification" {
8288 }
8389}
8490
91+ # Give Lambda permission to call S3
8592resource "aws_lambda_permission" "s3_lambda_exec_permission" {
8693 statement_id = " AllowExecutionFromS3Bucket"
8794 action = " lambda:InvokeFunction"
@@ -90,7 +97,7 @@ resource "aws_lambda_permission" "s3_lambda_exec_permission" {
9097 source_arn = aws_s3_bucket. shipment_picture_bucket . arn
9198}
9299
93-
100+ # Define role to execute Lambda
94101resource "aws_iam_role" "lambda_exec" {
95102 name = " lambda_exec_role"
96103
@@ -110,12 +117,14 @@ resource "aws_iam_role" "lambda_exec" {
110117EOF
111118}
112119
120+
121+ # Attach policy (S3 access) to Lambda role
113122resource "aws_iam_role_policy_attachment" "lambda_exec_policy" {
114123 policy_arn = " arn:aws:iam::aws:policy/AmazonS3FullAccess"
115124 role = aws_iam_role. lambda_exec . name
116125}
117126
118-
127+ # Define IAM role policy that grants permissions to access & process on AWS CloudWatch Logs, S3
119128resource "aws_iam_role_policy" "lambda_exec_policy" {
120129 name = " lambda_exec_policy"
121130 role = aws_iam_role. lambda_exec . id
@@ -151,20 +160,25 @@ resource "aws_iam_role_policy" "lambda_exec_policy" {
151160 EOF
152161}
153162
163+ # Define the topic
154164resource "aws_sns_topic" "update_shipment_picture_topic" {
155165 name = " update_shipment_picture_topic"
156166}
157167
168+ # Define the queue
158169resource "aws_sqs_queue" "update_shipment_picture_queue" {
159170 name = " update_shipment_picture_queue"
160171}
161172
173+ # Define subscription
162174resource "aws_sns_topic_subscription" "my_subscription" {
163175 topic_arn = aws_sns_topic. update_shipment_picture_topic . arn
164176 protocol = " sqs"
165177 endpoint = aws_sqs_queue. update_shipment_picture_queue . arn
166178}
167179
180+
181+ # Define policy to allow SNS to send message to SQS
168182resource "aws_sqs_queue_policy" "my_queue_policy" {
169183 queue_url = aws_sqs_queue. update_shipment_picture_queue . id
170184
@@ -189,13 +203,14 @@ resource "aws_sqs_queue_policy" "my_queue_policy" {
189203EOF
190204}
191205
206+ # Define the SQS subscription
192207resource "aws_sns_topic_subscription" "my_topic_subscription" {
193208 topic_arn = aws_sns_topic. update_shipment_picture_topic . arn
194209 protocol = " sqs"
195210 endpoint = aws_sqs_queue. update_shipment_picture_queue . arn
196211
197212 # Additional subscription attributes
198- raw_message_delivery = true
213+ # raw_message_delivery = true
199214 filter_policy = " "
200215 delivery_policy = " "
201216
0 commit comments