Skip to content

Commit 0ce2739

Browse files
authored
Merge pull request #7 from mokshasoft/aws-IaC
Add an IHP terraform configuration for AWS
2 parents b0aec46 + 4892abb commit 0ce2739

File tree

6 files changed

+248
-2
lines changed

6 files changed

+248
-2
lines changed

Application/Migration/1720937042.sql

Whitespace-only changes.

IaC/aws/ec2.tf

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
resource "aws_vpc" "main" {
2+
cidr_block = "10.0.0.0/16"
3+
enable_dns_support = true
4+
enable_dns_hostnames = true
5+
6+
tags = {
7+
Name = "${var.prefix}-main"
8+
}
9+
}
10+
11+
resource "aws_subnet" "public" {
12+
availability_zone = var.az_1
13+
vpc_id = aws_vpc.main.id
14+
cidr_block = "10.0.1.0/24"
15+
map_public_ip_on_launch = true # This enables auto-assign public IPs for instances launched in this subnet
16+
17+
tags = {
18+
Name = "${var.prefix}-public"
19+
}
20+
}
21+
22+
resource "aws_internet_gateway" "main" {
23+
vpc_id = aws_vpc.main.id
24+
25+
tags = {
26+
Name = "${var.prefix}-main"
27+
}
28+
}
29+
30+
resource "aws_route_table" "public" {
31+
vpc_id = aws_vpc.main.id
32+
33+
route {
34+
cidr_block = "0.0.0.0/0"
35+
gateway_id = aws_internet_gateway.main.id
36+
}
37+
38+
tags = {
39+
Name = "${var.prefix}-public"
40+
}
41+
}
42+
43+
resource "aws_route_table_association" "public" {
44+
subnet_id = aws_subnet.public.id
45+
route_table_id = aws_route_table.public.id
46+
}
47+
48+
resource "aws_security_group" "ec2_sg" {
49+
vpc_id = aws_vpc.main.id
50+
51+
ingress {
52+
from_port = 22
53+
to_port = 22
54+
protocol = "tcp"
55+
cidr_blocks = ["0.0.0.0/0"]
56+
}
57+
58+
ingress {
59+
from_port = 80
60+
to_port = 80
61+
protocol = "tcp"
62+
cidr_blocks = ["0.0.0.0/0"]
63+
}
64+
65+
ingress {
66+
from_port = 443
67+
to_port = 443
68+
protocol = "tcp"
69+
cidr_blocks = ["0.0.0.0/0"]
70+
}
71+
72+
egress {
73+
from_port = 22
74+
to_port = 22
75+
protocol = "tcp"
76+
cidr_blocks = ["0.0.0.0/0"]
77+
}
78+
79+
egress {
80+
from_port = 80
81+
to_port = 80
82+
protocol = "tcp"
83+
cidr_blocks = ["0.0.0.0/0"]
84+
}
85+
86+
egress {
87+
from_port = 443
88+
to_port = 443
89+
protocol = "tcp"
90+
cidr_blocks = ["0.0.0.0/0"]
91+
}
92+
93+
egress {
94+
from_port = 5432
95+
to_port = 5432
96+
protocol = "tcp"
97+
cidr_blocks = ["0.0.0.0/0"]
98+
}
99+
100+
tags = {
101+
Name = "${var.prefix}-ec2_sg"
102+
}
103+
}
104+
105+
resource "aws_instance" "ihp_app" {
106+
availability_zone = var.az_1
107+
ami = "ami-075111b79058282b7" # nixos/23.11
108+
instance_type = "t3.medium"
109+
key_name = "${var.key_name}"
110+
subnet_id = aws_subnet.public.id
111+
vpc_security_group_ids = [aws_security_group.ec2_sg.id]
112+
associate_public_ip_address = true
113+
114+
# Add an EBS volume with 30GB of storage
115+
ebs_block_device {
116+
device_name = "/dev/xvda"
117+
volume_size = 30 # Size in GB
118+
volume_type = "gp2" # General Purpose SSD
119+
}
120+
121+
tags = {
122+
Name = "${var.prefix}-ihp_app"
123+
}
124+
}

IaC/aws/provider.tf

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
provider "aws" {
2+
region = var.region
3+
}
4+
5+
provider "local" {}
6+
7+
resource "local_file" "db_info" {
8+
content = <<EOF
9+
Instance URL: ${aws_instance.ihp_app.public_dns}
10+
DB Endpoint: ${aws_db_instance.postgres.address}
11+
DB Username: ${aws_db_instance.postgres.username}
12+
DB Password: ${aws_db_instance.postgres.password}
13+
EOF
14+
15+
filename = "${path.module}/db_info.txt"
16+
}

IaC/aws/rds.tf

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Define Subnet in Availability Zone 1
2+
resource "aws_subnet" "private_1" {
3+
vpc_id = aws_vpc.main.id
4+
cidr_block = "10.0.2.0/24"
5+
availability_zone = var.az_1
6+
map_public_ip_on_launch = false
7+
tags = {
8+
Name = "${var.prefix}-private-1"
9+
}
10+
}
11+
12+
# Define Subnet in Availability Zone 2
13+
resource "aws_subnet" "private_2" {
14+
vpc_id = aws_vpc.main.id
15+
cidr_block = "10.0.3.0/24"
16+
availability_zone = var.az_2
17+
map_public_ip_on_launch = false
18+
tags = {
19+
Name = "${var.prefix}-private-2"
20+
}
21+
}
22+
23+
resource "aws_db_subnet_group" "main" {
24+
name = "main"
25+
subnet_ids = [
26+
aws_subnet.private_1.id,
27+
aws_subnet.private_2.id
28+
]
29+
30+
tags = {
31+
Name = "${var.prefix}-main"
32+
}
33+
}
34+
35+
# Define a security group for RDS
36+
resource "aws_security_group" "rds_sg" {
37+
description = "Allow inbound traffic to RDS"
38+
vpc_id = aws_vpc.main.id
39+
40+
ingress {
41+
from_port = 5432
42+
to_port = 5432
43+
protocol = "tcp"
44+
security_groups = [aws_security_group.ec2_sg.id] # Allow traffic from EC2 security group
45+
}
46+
47+
egress {
48+
from_port = 0
49+
to_port = 0
50+
protocol = "-1"
51+
cidr_blocks = ["0.0.0.0/0"]
52+
}
53+
54+
tags = {
55+
Name = "${var.prefix}-rds_sg"
56+
}
57+
}
58+
59+
# Define the RDS instance
60+
resource "aws_db_instance" "postgres" {
61+
identifier = "${var.prefix}-postgres-db"
62+
allocated_storage = 20
63+
engine = "postgres"
64+
engine_version = "16.3"
65+
instance_class = "db.t3.micro"
66+
username = "postgres_user"
67+
password = var.db_password
68+
db_subnet_group_name = aws_db_subnet_group.main.name
69+
vpc_security_group_ids = [aws_security_group.rds_sg.id]
70+
skip_final_snapshot = true
71+
publicly_accessible = false
72+
73+
tags = {
74+
Name = "${var.prefix}-PostgresDB"
75+
}
76+
}

IaC/aws/variables.tf

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
variable "prefix" {
2+
description = "Project prefix for the resource names"
3+
type = string
4+
}
5+
6+
variable "region" {
7+
description = "AWS Region to deploy to."
8+
type = string
9+
}
10+
11+
variable "az_1" {
12+
description = "Availability Zone 1. Used by the EBS (disk) and RDS (database) resource."
13+
type = string
14+
}
15+
16+
variable "az_2" {
17+
description = "Availability Zone 2. Used by the RDS resource. "
18+
type = string
19+
}
20+
21+
variable "key_name" {
22+
description = "The key name of the SSH key-pair"
23+
type = string
24+
}
25+
26+
variable "db_password" {
27+
description = "The password for the RDS database"
28+
type = string
29+
sensitive = true
30+
}

flake.nix

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
# Used to deploy the IHP application to AWS.
5252
#
5353
# Change the `CHANGE-ME` to your correct config.
54-
flake.nixosConfigurations."qa" = nixpkgs.lib.nixosSystem {
54+
flake.nixosConfigurations."ihp-app" = nixpkgs.lib.nixosSystem {
5555
system = "x86_64-linux";
5656
specialArgs = inputs;
5757
modules = [
@@ -136,7 +136,7 @@
136136
# @see https://nixos.wiki/wiki/Automatic_system_upgrades
137137
system.autoUpgrade.enable = true;
138138
# Keep as is. See https://nixos.wiki/wiki/FAQ/When_do_I_update_stateVersion
139-
system.stateVersion = "23.05";
139+
system.stateVersion = lib.mkForce "23.11";
140140
})
141141
];
142142
};

0 commit comments

Comments
 (0)