Skip to content

Commit b0a5e62

Browse files
authored
Add support for multiple VMs and multiple ingress rules (#19)
1 parent 9f44886 commit b0a5e62

File tree

2 files changed

+55
-26
lines changed

2 files changed

+55
-26
lines changed

examples/gen-ai-fastchat/main.tf

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,43 +40,35 @@ resource "local_file" "TF_private_key" {
4040
content = tls_private_key.rsa.private_key_pem
4141
filename = "tfkey.private"
4242
}
43-
4443
resource "aws_security_group" "ssh_security_group" {
4544
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+
}
5047

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]
5457
}
5558

5659
resource "aws_network_interface_sg_attachment" "sg_attachment" {
60+
count = length(module.ec2-vm)
5761
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
7363
}
7464

65+
# Modify the `vm_count` variable in the variables.tf file to create the required number of EC2 instances
7566
module "ec2-vm" {
67+
count = var.vm_count
7668
source = "intel/aws-vm/intel"
7769
key_name = aws_key_pair.TF_key.key_name
7870
instance_type = "m7i.4xlarge"
79-
availability_zone = "us-east-1a"
71+
availability_zone = "us-east-1c"
8072
ami = data.aws_ami.ubuntu-linux-2204.id
8173
user_data = data.cloudinit_config.ansible.rendered
8274

@@ -85,7 +77,7 @@ module "ec2-vm" {
8577
}]
8678

8779
tags = {
88-
Name = "my-test-vm-${random_id.rid.dec}"
80+
Name = "my-test-vm-${count.index}-${random_id.rid.dec}"
8981
Owner = "OwnerName-${random_id.rid.dec}",
9082
Duration = "2"
9183
}

examples/gen-ai-fastchat/variables.tf

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,41 @@ variable "region" {
22
description = "Target AWS region to deploy EC2 in."
33
type = string
44
default = "us-east-1"
5-
}
5+
}
6+
7+
# Variable to add ingress rules to the security group. Replace the default values with the required ports and CIDR ranges.
8+
variable "ingress_rules" {
9+
type = list(object({
10+
from_port = number
11+
to_port = number
12+
protocol = string
13+
cidr_blocks = string
14+
}))
15+
default = [
16+
{
17+
from_port = 22
18+
to_port = 22
19+
protocol = "tcp"
20+
cidr_blocks = "0.0.0.0/0"
21+
},
22+
{
23+
from_port = 7860
24+
to_port = 7860
25+
protocol = "tcp"
26+
cidr_blocks = "0.0.0.0/0"
27+
},
28+
{
29+
from_port = 5000
30+
to_port = 5000
31+
protocol = "tcp"
32+
cidr_blocks = "0.0.0.0/0"
33+
}
34+
]
35+
}
36+
37+
# Variable for how many VMs to build
38+
variable "vm_count" {
39+
description = "Number of VMs to build."
40+
type = number
41+
default = 1
42+
}

0 commit comments

Comments
 (0)