Skip to content

Commit 91190a5

Browse files
Small aws module project deployed 3 env at same time (dev, stg, prod)
1 parent 97013f1 commit 91190a5

8 files changed

Lines changed: 214 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
resource "aws_dynamodb_table" "basic-dynamodb-table" {
2+
name = "${var.env}-dynamodb-table"
3+
billing_mode = var.billing_mode
4+
hash_key = "LockId"
5+
attribute {
6+
name = "LockId"
7+
type = "S"
8+
}
9+
tags = {
10+
Name = "${var.env}-table"
11+
Environment = var.env
12+
}
13+
}

Terraform-module-app/infra/ec2.tf

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
resource "aws_key_pair" "key" {
2+
key_name = "${var.env}-my-key"
3+
public_key = file("ssh-key.pub")
4+
}
5+
6+
resource "aws_security_group" "my-sg" {
7+
name = "${var.env}-security-group"
8+
description = "Allow SSH and HTTP traffic"
9+
vpc_id = aws_vpc.vpc-main.id
10+
11+
ingress {
12+
description = "port 22 allow"
13+
from_port = 22
14+
to_port = 22
15+
protocol = "tcp"
16+
cidr_blocks = ["0.0.0.0/0"]
17+
}
18+
19+
egress {
20+
description = " allow all outgoing traffic "
21+
from_port = 0
22+
to_port = 0
23+
protocol = "-1"
24+
cidr_blocks = ["0.0.0.0/0"]
25+
}
26+
27+
ingress {
28+
description = "port 80 allow"
29+
from_port = 80
30+
to_port = 80
31+
protocol = "tcp"
32+
cidr_blocks = ["0.0.0.0/0"]
33+
}
34+
35+
ingress {
36+
description = "port 443 allow"
37+
from_port = 443
38+
to_port = 443
39+
protocol = "tcp"
40+
cidr_blocks = ["0.0.0.0/0"]
41+
}
42+
43+
tags = {
44+
Name = "mysecurity-sg"
45+
environment = var.env
46+
}
47+
}
48+
49+
50+
resource "aws_instance" "linux" {
51+
count = 2
52+
depends_on = [aws_security_group.my-sg]
53+
ami = var.ami
54+
instance_type = var.instance_type
55+
key_name = aws_key_pair.key.key_name
56+
subnet_id = aws_subnet.subnet-1.id
57+
vpc_security_group_ids = [aws_security_group.my-sg.id]
58+
associate_public_ip_address = true
59+
60+
61+
62+
tags = {
63+
Name = "${var.env}-app-server-${count.index + 1}"
64+
}
65+
66+
root_block_device {
67+
volume_size = "20"
68+
volume_type = "gp3"
69+
}
70+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "ec2_instances_public_ips" {
2+
value = aws_instance.linux[*].public_ip
3+
description = "Public IP addresses of the EC2 instances"
4+
}

Terraform-module-app/infra/s3.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
resource "aws_s3_bucket" "my_s3_bucket2404" {
2+
bucket = "${var.env}-bucket-2404"
3+
4+
tags = {
5+
Name = "${var.env}-bucket-2404"
6+
Environment = var.env
7+
}
8+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
variable "env" {
2+
description = "Environment name"
3+
type = string
4+
}
5+
6+
7+
variable "billing_mode" {
8+
description = "Billing mode for DynamoDB table"
9+
default = "PAY_PER_REQUEST"
10+
type = string
11+
}
12+
13+
variable "instance_type" {
14+
description = "value of the instance type"
15+
type = string
16+
}
17+
18+
variable "ami" {
19+
description = "value of the ami"
20+
type = string
21+
}

Terraform-module-app/infra/vpc.tf

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
resource "aws_vpc" "vpc-main" {
2+
cidr_block = "10.0.0.0/16"
3+
instance_tenancy = "default"
4+
enable_dns_hostnames = true
5+
enable_dns_support = true
6+
tags = {
7+
Name = "${var.env}-vpc"
8+
}
9+
}
10+
11+
resource "aws_subnet" "subnet-1" {
12+
vpc_id = aws_vpc.vpc-main.id
13+
cidr_block = "10.0.1.0/24"
14+
availability_zone = "us-west-2a"
15+
map_public_ip_on_launch = true
16+
17+
tags = {
18+
Name = "${var.env}-subnet-1"
19+
}
20+
}
21+
22+
resource "aws_internet_gateway" "inw" {
23+
vpc_id = aws_vpc.vpc-main.id
24+
25+
tags = {
26+
Name = "${var.env}-inw"
27+
}
28+
}
29+
30+
resource "aws_route_table" "rt" {
31+
vpc_id = aws_vpc.vpc-main.id
32+
33+
route {
34+
cidr_block = "0.0.0.0/0"
35+
gateway_id = aws_internet_gateway.inw.id
36+
}
37+
38+
tags = {
39+
Name = "${var.env}-rt"
40+
}
41+
}
42+
43+
resource "aws_route_table_association" "rta" {
44+
subnet_id = aws_subnet.subnet-1.id
45+
route_table_id = aws_route_table.rt.id
46+
}
47+

Terraform-module-app/main.tf

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# dev
2+
3+
module "dev-app" {
4+
source = "./infra"
5+
env = "dev"
6+
instance_type = "t3.micro"
7+
ami = "ami-0320940581663281e"
8+
}
9+
10+
#prd
11+
module "prd-app" {
12+
source = "./infra"
13+
env = "prd"
14+
instance_type = "c7i-flex.large"
15+
ami = "ami-0320940581663281e"
16+
}
17+
18+
#stg
19+
module "stg-app" {
20+
source = "./infra"
21+
env = "stg"
22+
instance_type = "t3.small"
23+
ami = "ami-0320940581663281e"
24+
25+
}
26+
output "dev_app_public_ips" {
27+
value = module.dev-app.ec2_instances_public_ips
28+
description = "Public IPs of the dev environment EC2 instances"
29+
}
30+
31+
output "prd_app_public_ips" {
32+
value = module.prd-app.ec2_instances_public_ips
33+
description = "Public IPs of the prd environment EC2 instances"
34+
}
35+
36+
output "stg_app_public_ips" {
37+
value = module.stg-app.ec2_instances_public_ips
38+
description = "Public IPs of the stg environment EC2 instances"
39+
}

Terraform-module-app/terraform.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "6.32.1"
6+
}
7+
}
8+
}
9+
10+
provider "aws" {
11+
region = "us-west-2"
12+
}

0 commit comments

Comments
 (0)