Skip to content

Commit de05873

Browse files
committed
feat: add terraform
1 parent 659c5af commit de05873

19 files changed

Lines changed: 526 additions & 4 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,6 @@ yarn-error.log*
9292
*.tsbuildinfo
9393
next-env.d.ts
9494

95+
# terraform
96+
.terraform
97+
.terraform.lock.hcl

infra/main.tf

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
aws_access_key =
2+
aws_secret_key =
3+
aws_session_token =
4+
db_username =
5+
db_password =

terraform/main.tf

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
provider "aws" {
2+
region = var.aws_region
3+
access_key = var.aws_access_key
4+
secret_key = var.aws_secret_key
5+
token = var.aws_session_token
6+
}
7+
8+
module "security_group" {
9+
source = "./modules/security_group"
10+
sg_name = "marai-database-sg"
11+
sg_description = "Security group for RDS and ElastiCache"
12+
vpc_id = var.vpc_id
13+
14+
ingress_rules = [
15+
{
16+
from_port = 5432
17+
to_port = 5432
18+
protocol = "tcp"
19+
cidr_blocks = var.allowed_cidr_blocks
20+
description = "Allow PostgreSQL access"
21+
},
22+
{
23+
from_port = 6379
24+
to_port = 6379
25+
protocol = "tcp"
26+
cidr_blocks = var.allowed_cidr_blocks
27+
description = "Allow Redis access"
28+
}
29+
]
30+
}
31+
32+
module "ec2" {
33+
source = "./modules/ec2"
34+
aws_region = var.aws_region
35+
ami = var.ami
36+
instance_type = var.instance_type
37+
subnet_id = var.subnet_id
38+
key_name = var.key_name
39+
security_group_ids = [module.security_group.sg_id]
40+
}
41+
42+
module "s3" {
43+
source = "./modules/s3"
44+
bucket_name = var.s3_bucket_name
45+
environment = var.environment
46+
enable_versioning = var.enable_versioning
47+
}
48+
49+
module "rds" {
50+
source = "./modules/rds"
51+
db_identifier = var.db_identifier
52+
db_engine = var.db_engine
53+
db_engine_version = var.db_engine_version
54+
db_instance_class = var.db_instance_class
55+
db_allocated_storage = var.db_allocated_storage
56+
db_username = var.db_username
57+
db_password = var.db_password
58+
parameter_group_name = "default.${var.db_engine}${var.db_engine_version}"
59+
subnet_group_name = var.db_subnet_group_name
60+
subnet_ids = var.rds_subnet_ids
61+
security_group_ids = [module.security_group.sg_id]
62+
multi_az = var.db_multi_az
63+
}

terraform/modules/ec2/main.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
resource "aws_instance" "this" {
2+
ami = var.ami
3+
instance_type = var.instance_type
4+
subnet_id = var.subnet_id
5+
key_name = var.key_name
6+
vpc_security_group_ids = var.security_group_ids
7+
8+
root_block_device {
9+
volume_type = "gp3"
10+
volume_size = 30
11+
encrypted = true
12+
}
13+
14+
tags = {
15+
Name = "marai-instance"
16+
}
17+
}

terraform/modules/ec2/outputs.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
output "id" {
2+
value = aws_instance.this.id
3+
}
4+
5+
output "public_ip" {
6+
value = aws_instance.this.public_ip
7+
}
8+
9+
output "private_ip" {
10+
value = aws_instance.this.private_ip
11+
}

terraform/modules/ec2/variables.tf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
variable "aws_region" {
2+
description = "AWS region"
3+
type = string
4+
}
5+
6+
variable "ami" {
7+
description = "AMI ID for the EC2 instance"
8+
type = string
9+
}
10+
11+
variable "instance_type" {
12+
description = "EC2 instance type"
13+
type = string
14+
}
15+
16+
variable "subnet_id" {
17+
description = "Subnet ID for EC2 instance"
18+
type = string
19+
}
20+
21+
variable "key_name" {
22+
description = "SSH key name"
23+
type = string
24+
}
25+
26+
variable "security_group_ids" {
27+
description = "Security group IDs for EC2 instance"
28+
type = list(string)
29+
default = []
30+
}

terraform/modules/rds/main.tf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
resource "aws_db_subnet_group" "this" {
2+
name = var.subnet_group_name
3+
subnet_ids = var.subnet_ids
4+
5+
tags = {
6+
Name = var.subnet_group_name
7+
}
8+
}
9+
10+
resource "aws_db_instance" "this" {
11+
identifier = var.db_identifier
12+
engine = var.db_engine
13+
engine_version = var.db_engine_version
14+
instance_class = var.db_instance_class
15+
allocated_storage = var.db_allocated_storage
16+
storage_type = "gp2"
17+
storage_encrypted = true
18+
username = var.db_username
19+
password = var.db_password
20+
parameter_group_name = var.parameter_group_name
21+
db_subnet_group_name = aws_db_subnet_group.this.name
22+
vpc_security_group_ids = var.security_group_ids
23+
multi_az = var.multi_az
24+
backup_retention_period = 7
25+
skip_final_snapshot = true
26+
27+
tags = {
28+
Name = var.db_identifier
29+
}
30+
}

terraform/modules/rds/outputs.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
output "endpoint" {
2+
value = aws_db_instance.this.endpoint
3+
}
4+
5+
output "address" {
6+
value = aws_db_instance.this.address
7+
}
8+
9+
output "port" {
10+
value = aws_db_instance.this.port
11+
}

terraform/modules/rds/variables.tf

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
variable "db_identifier" {
2+
description = "RDS instance identifier"
3+
type = string
4+
}
5+
6+
variable "db_engine" {
7+
description = "RDS database engine"
8+
type = string
9+
}
10+
11+
variable "db_engine_version" {
12+
description = "RDS database engine version"
13+
type = string
14+
}
15+
16+
variable "db_instance_class" {
17+
description = "RDS instance class"
18+
type = string
19+
}
20+
21+
variable "db_allocated_storage" {
22+
description = "RDS allocated storage in GB"
23+
type = number
24+
}
25+
26+
variable "db_username" {
27+
description = "RDS master username"
28+
type = string
29+
sensitive = true
30+
}
31+
32+
variable "db_password" {
33+
description = "RDS master password"
34+
type = string
35+
sensitive = true
36+
}
37+
38+
variable "parameter_group_name" {
39+
description = "RDS parameter group name"
40+
type = string
41+
}
42+
43+
variable "subnet_group_name" {
44+
description = "RDS subnet group name"
45+
type = string
46+
}
47+
48+
variable "subnet_ids" {
49+
description = "Subnet IDs for RDS"
50+
type = list(string)
51+
}
52+
53+
variable "security_group_ids" {
54+
description = "Security group IDs for RDS"
55+
type = list(string)
56+
}
57+
58+
variable "multi_az" {
59+
description = "Enable multi-AZ deployment"
60+
type = bool
61+
default = false
62+
}

0 commit comments

Comments
 (0)