Skip to content

Commit bf28d7f

Browse files
[INIT] Terraform project - cloud infrastructure (#1)
1 parent 82affe7 commit bf28d7f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3159
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ override.tf.json
3535
# Ignore CLI configuration files
3636
.terraformrc
3737
terraform.rc
38+
39+
# Ignore zip files
40+
/src/lambda-function-python-zip/*.zip

README.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,51 @@
1-
# aws-terraform-static-hosting-cloudfront
1+
# Terraform AWS Serverless Project: S3 + CloudFront + Lambda + DynamoDB
2+
23
Terraform project to provision static hosting on AWS S3 and deliver content securely via CloudFront CDN.
4+
5+
## 📌 Infrastructure Overview
6+
7+
This project provisions the following resources on AWS:
8+
9+
- **S3 Bucket**: static website hosting
10+
- **CloudFront Distribution**: global CDN with HTTPS support
11+
- **DynamoDB Table**: to store product data
12+
- **Lambda Function**: `get_products`: read items from DynamoDB
13+
- **Lambda Function**: `post_products`: write items into DynamoDB
14+
15+
### Architecture Diagram
16+
![Infrastructure Diagram](src/infra/graph.png)
17+
18+
## Project structure:
19+
20+
The project is under the src folder.
21+
22+
- **infra**: contains all Terraform files; run `terraform` commands from here
23+
- **infra-modules**: reusable Terraform modules for resources
24+
- **lambda-function-python**: Python source code for Lambda functions
25+
- **lambda-function-python-zip**: folder used by Terraform to generate Lambda ZIPs
26+
- **web-template**: static web templates to be uploaded to S3
27+
28+
## 🚀 Commands
29+
30+
Initialize, plan and apply:
31+
32+
```bash
33+
terraform init
34+
terraform plan -var-file=variables/dev.tfvars
35+
terraform apply -var-file=variables/dev.tfvars
36+
```
37+
38+
Destroy infrastructure:
39+
40+
```bash
41+
terraform destroy -var-file=variables/dev.tfvars
42+
```
43+
44+
## Future Enhancements
45+
Stay tuned! This project will be extended with new features and improvements, making it an even more complete serverless case study. 🚀
46+
47+
## License
48+
This project is licensed under the **MIT License**. Use at your own risk. If something breaks, **it's your problem!** 😆
49+
50+
---
51+
🚀 Happy Coding!
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
resource "aws_dynamodb_table" "dynamodb_table" {
2+
name = var.table_name
3+
billing_mode = var.table_billing_mode
4+
hash_key = var.table_hash_key
5+
6+
attribute {
7+
name = var.table_attribute_name
8+
type = var.table_attribute_type
9+
}
10+
11+
tags = var.tags
12+
13+
lifecycle {
14+
ignore_changes = [tags]
15+
}
16+
}
17+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "arn_value" {
2+
value = aws_dynamodb_table.dynamodb_table.arn
3+
}
4+
5+
output "name_value" {
6+
value = aws_dynamodb_table.dynamodb_table.name
7+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
variable "table_name" {
2+
type = string
3+
nullable = false
4+
}
5+
6+
variable "table_billing_mode" {
7+
type = string
8+
nullable = false
9+
default = "PAY_PER_REQUEST"
10+
}
11+
12+
variable "table_hash_key" {
13+
type = string
14+
nullable = false
15+
default = "Id"
16+
}
17+
18+
variable "table_attribute_name" {
19+
type = string
20+
nullable = false
21+
default = "Id"
22+
}
23+
24+
variable "table_attribute_type" {
25+
type = string
26+
nullable = false
27+
default = "S"
28+
}
29+
30+
variable "tags" {
31+
type = map(string)
32+
default = {}
33+
nullable = true
34+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
resource "aws_iam_role" "iam_roles" {
3+
for_each = var.roles
4+
5+
name = each.value.name
6+
assume_role_policy = each.value.assume_role_policy
7+
}
8+
9+
resource "aws_iam_policy" "iam_policies" {
10+
for_each = merge([
11+
for role_key, role in var.roles : {
12+
for idx, policy in role.policies : "${role_key}-${idx}" => policy
13+
} if length(role.policies) > 0
14+
]...)
15+
16+
name = each.value.name
17+
description = each.value.description
18+
policy = each.value.policy
19+
}
20+
21+
resource "aws_iam_role_policy_attachment" "iam_role_attachments" {
22+
for_each = { for key, policy in aws_iam_policy.iam_policies : key => policy }
23+
24+
role = var.roles[split("-", each.key)[0]].name
25+
policy_arn = each.value.arn
26+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
output "iam_role_arns" {
3+
value = { for role_key, role in aws_iam_role.iam_roles : role_key => role.arn }
4+
}
5+
6+
output "iam_policy_arns" {
7+
value = { for role_key, policy in aws_iam_policy.iam_policies : role_key => policy.arn }
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
variable "roles" {
2+
description = "Map of IAM roles with policies."
3+
type = map(object({
4+
name = string
5+
assume_role_policy = string
6+
policies = list(object({
7+
name = string
8+
description = string
9+
policy = string
10+
}))
11+
}))
12+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
resource "aws_lambda_function" "lambda_function" {
2+
function_name = var.lambda_function_name
3+
role = var.lambda_role_arn
4+
handler = var.lambda_handler
5+
runtime = var.lambda_runtime
6+
filename = var.lambda_filename
7+
source_code_hash = filebase64sha256(var.lambda_filename)
8+
timeout = var.lambda_timeout
9+
tags = var.tags
10+
11+
lifecycle {
12+
# ignore_changes = [source_code_hash]
13+
ignore_changes = [tags]
14+
}
15+
16+
logging_config {
17+
log_format = "Text"
18+
}
19+
20+
environment {
21+
variables = var.lambda_environment_variables
22+
}
23+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
output "function_name_value" {
2+
value = aws_lambda_function.lambda_function.function_name
3+
}
4+
5+
output "arn_value" {
6+
value = aws_lambda_function.lambda_function.arn
7+
}
8+
9+
output "invoke_arn_value" {
10+
value = aws_lambda_function.lambda_function.invoke_arn
11+
}

0 commit comments

Comments
 (0)