1
1
# create a security group
2
+ # https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group
2
3
resource "aws_security_group" "ec2_instance" {
3
4
name = " ${ var . name } -ec2"
4
5
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
6
7
}
8
+ # https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule
7
9
resource "aws_security_group_rule" "ec2_instance_ingress" {
8
10
type = " ingress"
9
11
security_group_id = aws_security_group. ec2_instance . id
@@ -13,7 +15,7 @@ resource "aws_security_group_rule" "ec2_instance_ingress" {
13
15
cidr_blocks = [var . vpc_cidr ]
14
16
description = " Enable access from any resource inside the VPC."
15
17
}
16
-
18
+ # https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule
17
19
resource "aws_security_group_rule" "ec2_instance_egress" {
18
20
type = " egress"
19
21
security_group_id = aws_security_group. ec2_instance . id
@@ -24,7 +26,7 @@ resource "aws_security_group_rule" "ec2_instance_egress" {
24
26
description = " Enable access to the internet."
25
27
}
26
28
27
- # create an EC2 in a public subnet
29
+ # https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami
28
30
data "aws_ami" "amazon_ami" {
29
31
filter {
30
32
name = " name"
@@ -37,14 +39,16 @@ data "aws_ami" "amazon_ami" {
37
39
most_recent = true
38
40
owners = [" amazon" ]
39
41
}
42
+ # create an EC2 in a public subnet
43
+ # https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance
40
44
resource "aws_instance" "app-server-read" {
41
45
instance_type = var. instance_type
42
46
ami = data. aws_ami . amazon_ami . id
43
47
vpc_security_group_ids = [aws_security_group . ec2_instance . id ]
44
48
iam_instance_profile = aws_iam_instance_profile. ec2_profile . name
45
49
associate_public_ip_address = true
46
50
# 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
48
52
ebs_optimized = true
49
53
monitoring = true
50
54
root_block_device {
@@ -65,14 +69,15 @@ resource "aws_instance" "app-server-read" {
65
69
elasticache_auth_token = aws_secretsmanager_secret.elasticache_auth.name
66
70
})
67
71
}
72
+ # https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance
68
73
resource "aws_instance" "app-server-write" {
69
74
instance_type = var. instance_type
70
75
ami = data. aws_ami . amazon_ami . id
71
76
vpc_security_group_ids = [aws_security_group . ec2_instance . id ]
72
77
iam_instance_profile = aws_iam_instance_profile. ec2_profile . name
73
78
associate_public_ip_address = true
74
79
# 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
76
81
ebs_optimized = true
77
82
monitoring = true
78
83
root_block_device {
0 commit comments