Skip to content

Commit e52acdd

Browse files
committed
converted to use vpc module to address #41
1 parent ffe35c6 commit e52acdd

File tree

3 files changed

+18
-67
lines changed

3 files changed

+18
-67
lines changed

ec2.tf

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# create a security group
2+
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group
23
resource "aws_security_group" "ec2_instance" {
34
name = "${var.name}-ec2"
45
description = "Allow inbound to and outbound access from the Amazon EC2 instance."
5-
vpc_id = aws_vpc.this.id
6+
vpc_id = module.vpc.vpc.id
67
}
8+
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule
79
resource "aws_security_group_rule" "ec2_instance_ingress" {
810
type = "ingress"
911
security_group_id = aws_security_group.ec2_instance.id
@@ -13,7 +15,7 @@ resource "aws_security_group_rule" "ec2_instance_ingress" {
1315
cidr_blocks = [var.vpc_cidr]
1416
description = "Enable access from any resource inside the VPC."
1517
}
16-
18+
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule
1719
resource "aws_security_group_rule" "ec2_instance_egress" {
1820
type = "egress"
1921
security_group_id = aws_security_group.ec2_instance.id
@@ -24,7 +26,7 @@ resource "aws_security_group_rule" "ec2_instance_egress" {
2426
description = "Enable access to the internet."
2527
}
2628

27-
#create an EC2 in a public subnet
29+
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami
2830
data "aws_ami" "amazon_ami" {
2931
filter {
3032
name = "name"
@@ -37,14 +39,16 @@ data "aws_ami" "amazon_ami" {
3739
most_recent = true
3840
owners = ["amazon"]
3941
}
42+
#create an EC2 in a public subnet
43+
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance
4044
resource "aws_instance" "app-server-read" {
4145
instance_type = var.instance_type
4246
ami = data.aws_ami.amazon_ami.id
4347
vpc_security_group_ids = [aws_security_group.ec2_instance.id]
4448
iam_instance_profile = aws_iam_instance_profile.ec2_profile.name
4549
associate_public_ip_address = true
4650
#checkov:skip=CKV_AWS_88: Required for Session Manager access
47-
subnet_id = aws_subnet.public[0].id
51+
subnet_id = module.vpc.private_subnets[0].id
4852
ebs_optimized = true
4953
monitoring = true
5054
root_block_device {
@@ -65,14 +69,15 @@ resource "aws_instance" "app-server-read" {
6569
elasticache_auth_token = aws_secretsmanager_secret.elasticache_auth.name
6670
})
6771
}
72+
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance
6873
resource "aws_instance" "app-server-write" {
6974
instance_type = var.instance_type
7075
ami = data.aws_ami.amazon_ami.id
7176
vpc_security_group_ids = [aws_security_group.ec2_instance.id]
7277
iam_instance_profile = aws_iam_instance_profile.ec2_profile.name
7378
associate_public_ip_address = true
7479
#checkov:skip=CKV_AWS_88: Required for Session Manager access
75-
subnet_id = aws_subnet.public[0].id
80+
subnet_id = module.vpc.private_subnets[0].id
7681
ebs_optimized = true
7782
monitoring = true
7883
root_block_device {

elasticache.tf

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/elasticache_subnet_group
12
resource "aws_elasticache_subnet_group" "elasticache_subnet" {
23
name = "${var.name}-cache-subnet"
3-
subnet_ids = [for subnet in aws_subnet.private : subnet.id]
4+
subnet_ids = [for subnet in module.vpc.private_subnets : subnet.id]
45
}
5-
6+
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/secretsmanager_secret
67
resource "aws_secretsmanager_secret" "elasticache_auth" {
78
name = "${var.name}-elasticache-auth"
89
recovery_window_in_days = 0
910
kms_key_id = aws_kms_key.encryption_secret.id
1011
#checkov:skip=CKV2_AWS_57: Disabled Secrets Manager secrets automatic rotation
1112
}
13+
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/secretsmanager_secret_version
1214
resource "aws_secretsmanager_secret_version" "auth" {
1315
secret_id = aws_secretsmanager_secret.elasticache_auth.id
1416
secret_string = random_password.auth.result
@@ -43,8 +45,8 @@ resource "aws_elasticache_replication_group" "app4" {
4345
log_format = "json"
4446
log_type = "engine-log"
4547
}
46-
lifecycle {
47-
ignore_changes = [kms_key_id]
48-
}
48+
# lifecycle {
49+
# ignore_changes = [kms_key_id]
50+
# }
4951
apply_immediately = true
5052
}

network.tf

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,6 @@
1-
# # https://docs.aws.amazon.com/glue/latest/dg/set-up-vpc-dns.html
2-
# resource "aws_vpc" "this" {
3-
# cidr_block = var.vpc_cidr
4-
# # https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc#enable_dns_support
5-
# enable_dns_support = true
6-
# # https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc#enable_dns_hostnames
7-
# enable_dns_hostnames = true
8-
# #checkov:skip=CKV2_AWS_11: Not creating a flow log for this VPC
9-
# tags = {
10-
# "Name" = "app-4"
11-
# }
12-
# }
13-
# data "aws_availability_zones" "available" {
14-
# state = "available"
15-
# }
16-
# resource "aws_subnet" "private" {
17-
# count = length(var.subnet_cidr_private)
18-
# vpc_id = aws_vpc.this.id
19-
# cidr_block = var.subnet_cidr_private[count.index]
20-
# availability_zone = data.aws_availability_zones.available.names[(count.index) % length(data.aws_availability_zones.available.names)]
21-
# tags = {
22-
# "Name" = "app-4-private-${count.index + 1}"
23-
# }
24-
# }
25-
# resource "aws_subnet" "public" {
26-
# count = length(var.subnet_cidr_public)
27-
# vpc_id = aws_vpc.this.id
28-
# cidr_block = var.subnet_cidr_public[count.index]
29-
# availability_zone = data.aws_availability_zones.available.names[(count.index) % length(data.aws_availability_zones.available.names)]
30-
# tags = {
31-
# "Name" = "app-4-public-${count.index + 1}"
32-
# }
33-
# }
34-
# resource "aws_route_table" "private" {
35-
# count = length(var.subnet_cidr_private)
36-
# vpc_id = aws_vpc.this.id
37-
# tags = {
38-
# "Name" = "app-4-private-route-table-${count.index + 1}"
39-
# }
40-
# }
41-
# resource "aws_route_table" "public" {
42-
# vpc_id = aws_vpc.this.id
43-
# tags = {
44-
# "Name" = "app-4-public"
45-
# }
46-
# }
47-
# resource "aws_route_table_association" "private" {
48-
# count = length(var.subnet_cidr_private)
49-
# subnet_id = element(aws_subnet.private.*.id, count.index)
50-
# route_table_id = aws_route_table.private[count.index].id
51-
# }
52-
# resource "aws_route_table_association" "public" {
53-
# count = length(var.subnet_cidr_public)
54-
# subnet_id = element(aws_subnet.public.*.id, count.index)
55-
# route_table_id = aws_route_table.public.id
56-
# }
57-
581
module "vpc" {
592
source = "github.com/kunduso/terraform-aws-vpc?ref=v1.0.0"
3+
region = var.region
604
vpc_cidr = var.vpc_cidr
615
enable_dns_support = "true"
626
enable_dns_hostnames = "true"

0 commit comments

Comments
 (0)