@@ -40,43 +40,35 @@ resource "local_file" "TF_private_key" {
40
40
content = tls_private_key. rsa . private_key_pem
41
41
filename = " tfkey.private"
42
42
}
43
-
44
43
resource "aws_security_group" "ssh_security_group" {
45
44
description = " security group to configure ports for ssh"
46
- ingress {
47
- from_port = 22
48
- to_port = 22
49
- protocol = " tcp"
45
+ name_prefix = " ssh_security_group"
46
+ }
50
47
51
- # # CHANGE THE IP CIDR BLOCK BELOW TO ALL YOUR OWN SSH PORT ##
52
- cidr_blocks = [" a.b.c.d/x" ]
53
- }
48
+ # Modify the `ingress_rules` variable in the variables.tf file to allow the required ports for your CIDR ranges
49
+ resource "aws_security_group_rule" "ingress_rules" {
50
+ count = length (var. ingress_rules )
51
+ type = " ingress"
52
+ security_group_id = aws_security_group. ssh_security_group . id
53
+ from_port = var. ingress_rules [count . index ]. from_port
54
+ to_port = var. ingress_rules [count . index ]. to_port
55
+ protocol = var. ingress_rules [count . index ]. protocol
56
+ cidr_blocks = [var . ingress_rules [count . index ]. cidr_blocks ]
54
57
}
55
58
56
59
resource "aws_network_interface_sg_attachment" "sg_attachment" {
60
+ count = length (module. ec2-vm )
57
61
security_group_id = aws_security_group. ssh_security_group . id
58
- network_interface_id = module. ec2-vm . primary_network_interface_id
59
- }
60
-
61
- # # Get latest Ubuntu 22.04 AMI in AWS for x86
62
- data "aws_ami" "ubuntu-linux-2204" {
63
- most_recent = true
64
- owners = [" 099720109477" ] # Canonical
65
- filter {
66
- name = " name"
67
- values = [" ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*" ]
68
- }
69
- filter {
70
- name = " virtualization-type"
71
- values = [" hvm" ]
72
- }
62
+ network_interface_id = module. ec2-vm [count . index ]. primary_network_interface_id
73
63
}
74
64
65
+ # Modify the `vm_count` variable in the variables.tf file to create the required number of EC2 instances
75
66
module "ec2-vm" {
67
+ count = var. vm_count
76
68
source = " intel/aws-vm/intel"
77
69
key_name = aws_key_pair. TF_key . key_name
78
70
instance_type = " m7i.4xlarge"
79
- availability_zone = " us-east-1a "
71
+ availability_zone = " us-east-1c "
80
72
ami = data. aws_ami . ubuntu-linux-2204 . id
81
73
user_data = data. cloudinit_config . ansible . rendered
82
74
@@ -85,7 +77,7 @@ module "ec2-vm" {
85
77
}]
86
78
87
79
tags = {
88
- Name = " my-test-vm-${ random_id . rid . dec } "
80
+ Name = " my-test-vm-${ count . index } - ${ random_id . rid . dec } "
89
81
Owner = " OwnerName-${ random_id . rid . dec } " ,
90
82
Duration = " 2"
91
83
}
0 commit comments