@@ -8,3 +8,60 @@ resource "aws_instance" "ci_cd_demo" {
8
8
Name = " Instance-${ var . environment } "
9
9
}, local. common_tags )
10
10
}
11
+
12
+ # For deploying same EC2 instance into private subnet modify the subnet_id to point to private subnet
13
+
14
+ # resource "aws_instance" "ci_cd_demo" {
15
+ # ami = lookup(var.ami_ids, var.environment)
16
+ # instance_type = lookup(var.instance_types, var.environment, "t3.micro")
17
+ # subnet_id = aws_subnet.private_subnet[0].id # Now using the first private subnet
18
+ # vpc_security_group_ids = [aws_security_group.ci_cd_demo.id]
19
+
20
+ # tags = merge({
21
+ # Name = "Instance-${var.environment}"
22
+ # }, local.common_tags)
23
+ # }
24
+
25
+
26
+ # bastion host: ec2 instance to access private instances.
27
+
28
+
29
+ resource "aws_instance" "bastion_host" {
30
+ ami = lookup (var. ami_ids , " bastion" ) // Make sure to have an AMI for bastion hosts
31
+ instance_type = " t2.micro"
32
+ subnet_id = aws_subnet. public_subnet [0 ]. id // Assumes the first defined public subnet is used
33
+
34
+ vpc_security_group_ids = [aws_security_group . bastion_sg . id ]
35
+
36
+ key_name = " your-key-pair-name" // Ensure you have a key pair created and available
37
+
38
+ tags = merge ({
39
+ Name = " BastionHost-${ var . environment } "
40
+ }, local. common_tags )
41
+ }
42
+
43
+ resource "aws_security_group" "bastion_sg" {
44
+ name = " sg-bastion-${ var . environment } "
45
+ description = " Security Group for Bastion Host"
46
+ vpc_id = aws_vpc. ci_cd_demo_vpc . id
47
+
48
+ ingress {
49
+ from_port = 22
50
+ to_port = 22
51
+ protocol = " tcp"
52
+ cidr_blocks = [" your-ip-address/32" ] // Your office or home IP to restrict access
53
+ }
54
+
55
+ egress {
56
+ from_port = 0
57
+ to_port = 0
58
+ protocol = " -1"
59
+ cidr_blocks = [" 0.0.0.0/0" ]
60
+ }
61
+
62
+ tags = {
63
+ Name = " sg-bastion-${ var . environment } "
64
+ Terraform = " true"
65
+ Environment = var.environment
66
+ }
67
+ }
0 commit comments