Skip to content

Commit 91737f2

Browse files
committed
Enable dynamic AMI ID input based on the OS type specified
1 parent 31aee62 commit 91737f2

File tree

4 files changed

+133
-10
lines changed

4 files changed

+133
-10
lines changed

comet-infrastructure/main.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ data "aws_availability_zones" "available" {}
22

33
data "aws_eks_cluster_auth" "this" {
44
count = var.enable_eks ? 1 : 0
5-
name = module.comet_eks[0].cluster_name
5+
name = module.comet_eks[0].cluster_name
66
}
77

88
locals {
@@ -13,8 +13,8 @@ locals {
1313
#set environment here, and use local.environment for the environment variables in all of the module calls
1414

1515
tags = {
16-
Terraform = "true"
17-
Environment = var.environment
16+
Terraform = "true"
17+
Environment = var.environment
1818
}
1919
}
2020

@@ -55,7 +55,7 @@ module "comet_ec2" {
5555

5656
vpc_id = module.vpc.vpc_id
5757
comet_ec2_subnet = module.vpc.public_subnets[count.index % length(module.vpc.public_subnets)]
58-
comet_ec2_ami = var.comet_ec2_ami
58+
comet_ec2_ami_type = var.comet_ec2_ami_type
5959
comet_ec2_instance_type = var.comet_ec2_instance_type
6060
comet_ec2_instance_count = var.comet_ec2_instance_count
6161
comet_ec2_volume_type = var.comet_ec2_volume_type

comet-infrastructure/modules/comet_ec2/main.tf

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,127 @@ locals {
1111
}
1212
}
1313

14+
data "aws_ami" "al2" {
15+
most_recent = true
16+
17+
filter {
18+
name = "name"
19+
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
20+
}
21+
22+
filter {
23+
name = "root-device-type"
24+
values = ["ebs"]
25+
}
26+
27+
owners = ["amazon"]
28+
}
29+
30+
data "aws_ami" "rhel7" {
31+
most_recent = true
32+
33+
filter {
34+
name = "name"
35+
values = ["RHEL-7*_HVM-*"]
36+
}
37+
38+
filter {
39+
name = "root-device-type"
40+
values = ["ebs"]
41+
}
42+
43+
owners = ["amazon"]
44+
}
45+
46+
data "aws_ami" "rhel8" {
47+
most_recent = true
48+
49+
filter {
50+
name = "name"
51+
values = ["RHEL-8*_HVM-*"]
52+
}
53+
54+
filter {
55+
name = "root-device-type"
56+
values = ["ebs"]
57+
}
58+
59+
owners = ["amazon"]
60+
}
61+
62+
data "aws_ami" "rhel9" {
63+
most_recent = true
64+
65+
filter {
66+
name = "name"
67+
values = ["RHEL-9*_HVM-*"]
68+
}
69+
70+
filter {
71+
name = "root-device-type"
72+
values = ["ebs"]
73+
}
74+
75+
owners = ["amazon"]
76+
}
77+
78+
data "aws_ami" "ubuntu18" {
79+
most_recent = true
80+
81+
filter {
82+
name = "name"
83+
values = ["ubuntu/images/hvm-ssd/ubuntu-*-18.04-amd64-server-*"]
84+
}
85+
86+
filter {
87+
name = "root-device-type"
88+
values = ["ebs"]
89+
}
90+
91+
owners = ["amazon"]
92+
}
93+
94+
data "aws_ami" "ubuntu20" {
95+
most_recent = true
96+
97+
filter {
98+
name = "name"
99+
values = ["ubuntu/images/hvm-ssd/ubuntu-*-20.04-amd64-server-*"]
100+
}
101+
102+
filter {
103+
name = "root-device-type"
104+
values = ["ebs"]
105+
}
106+
107+
owners = ["amazon"]
108+
}
109+
110+
data "aws_ami" "ubuntu22" {
111+
most_recent = true
112+
113+
filter {
114+
name = "name"
115+
values = ["ubuntu/images/hvm-ssd/ubuntu-*-22.04-amd64-server-*"]
116+
}
117+
118+
filter {
119+
name = "root-device-type"
120+
values = ["ebs"]
121+
}
122+
123+
owners = ["amazon"]
124+
}
125+
14126
resource "aws_instance" "comet_ec2" {
15-
ami = var.comet_ec2_ami
127+
ami = var.comet_ec2_ami_type == "al2" ? data.aws_ami.al2.id : (
128+
var.comet_ec2_ami_type == "rhel7" ? data.aws_ami.rhel7.id : (
129+
var.comet_ec2_ami_type == "rhel8" ? data.aws_ami.rhel8.id : (
130+
var.comet_ec2_ami_type == "rhel9" ? data.aws_ami.rhel9.id : (
131+
var.comet_ec2_ami_type == "ubuntu18" ? data.aws_ami.ubuntu18.id : (
132+
var.comet_ec2_ami_type == "ubuntu20" ? data.aws_ami.ubuntu20.id : (
133+
var.comet_ec2_ami_type == "ubuntu22" ? data.aws_ami.ubuntu22.id : (
134+
null)))))))
16135
instance_type = var.comet_ec2_instance_type
17136
key_name = var.comet_ec2_key
18137
count = var.comet_ec2_instance_count

comet-infrastructure/modules/comet_ec2/variables.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ variable "vpc_id" {
2121
type = string
2222
}
2323

24-
variable "comet_ec2_ami" {
25-
description = "AMI for the EC2 instance"
24+
variable "comet_ec2_ami_type" {
25+
description = "Operating system type for the EC2 instance AMI"
2626
type = string
2727
}
2828

comet-infrastructure/variables.tf

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,14 @@ variable "comet_ec2_subnet" {
4848
default = null
4949
}
5050

51-
variable "comet_ec2_ami" {
52-
description = "AMI for Comet EC2 instance"
51+
variable "comet_ec2_ami_type" {
5352
type = string
54-
default = "ami-05842f1afbf311a43"
53+
description = "Operating system type for the EC2 instance AMI"
54+
default = "ubuntu22"
55+
validation {
56+
condition = can(regex("^al2$|^rhel(7|8|9)$|^ubuntu(18|20|22)$", var.comet_ec2_ami_type))
57+
error_message = "Invalid OS type. Allowed values are 'al2', 'rhel7', 'rhel8', 'rhel9', 'ubuntu18', 'ubuntu20', 'ubuntu22'."
58+
}
5559
}
5660

5761
variable "comet_ec2_instance_type" {

0 commit comments

Comments
 (0)