Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 78c5fef

Browse files
authoredJul 23, 2021
ami-centos: added new aws provided images (#332)
The only thing missing from this MR was to add a validation for when image_provider is CentOS and release is 8, as there is no image for that case and an error for no result will be shown, but there is no easy way to add the check in terraform right now
1 parent 53dd29d commit 78c5fef

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed
 

‎modules/ami-centos/README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,27 @@ release of Centos. Use it like:
55

66
```
77
module "centos-7-ami" {
8-
source = "../../modules/ami-centos"
8+
source = "../../modules/ami-centos"
9+
release = 7
10+
image_provider = "CentOS"
911
}
1012
```
1113

1214
Or:
1315

1416
```
15-
module "centos-6-ami" {
16-
source = "../../modules/ami-centos"
17-
release = "6"
17+
module "centos-8-ami" {
18+
source = "../../modules/ami-centos"
19+
release = 8
20+
image_provider = "AWS"
1821
}
1922
```
2023

2124
To use the AMI on EC2, reference it by ID like this: `${module.centos-7-ami.id}`
2225

2326
The module will filter the AMI by the following criteria:
2427

25-
* provided by Centos.org
28+
* provided by Centos.org or AWS
2629
* the most recent release
2730
* hvm-type AMIs
2831
* amd64

‎modules/ami-centos/main.tf

+27-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,20 @@ variable "most_recent" {
66

77
variable "release" {
88
description = "default centos release to target"
9+
type = number
10+
validation {
11+
condition = var.release >= 6 && var.release <= 8
12+
error_message = "CentOS version number (6 | 7 | 8)."
13+
}
14+
}
15+
16+
variable "image_provider" {
17+
description = "The image provider (AWS | CentOS)."
918
type = string
19+
validation {
20+
condition = var.image_provider == "CentOS" || var.image_provider == "AWS"
21+
error_message = "The provider value must be either \"CentOS\" or \"AWS\"."
22+
}
1023
}
1124

1225
data "aws_ami" "centos" {
@@ -26,20 +39,28 @@ data "aws_ami" "centos" {
2639
# we can be sure of the ami's authenticity by filtering by the products id's unique to
2740
# CentOS.org, which can be found on their web site at https://wiki.centos.org/Cloud/AWS
2841
filter {
29-
name = "product-code"
30-
values = [
31-
"aw0evgkw8e5c1q413zgy5pjce", # Official `CentOS 7 (x86_64) - with Updates HVM` product id
32-
"6x5jmcajty9edm3f211pqjfn2" # Official `CentOS 6 (x86_64) - with Updates HVM` product id
33-
]
42+
name = "product-code"
43+
values = compact(var.image_provider == "AWS" ? [
44+
var.release == 8 ? "47k9ia2igxpcce2bzo8u3kj03" : "", # Official `CentOS 8 (x86_64) - with Updates HVM` by AWS product id
45+
var.release == 7 ? "cvugziknvmxgqna9noibqnnsy" : "", # Official `CentOS 7 (x86_64) - with Updates HVM` by AWS product id
46+
var.release == 6 ? "ckx0h8ljio731afm2k92jtg62" : "" # Official `CentOS 6 (x86_64) - with Updates HVM` by AWS product id
47+
] : [
48+
var.release == 7 ? "aw0evgkw8e5c1q413zgy5pjce" : "", # Official `CentOS 7 (x86_64) - with Updates HVM` by CentOS product id
49+
var.release == 6 ? "6x5jmcajty9edm3f211pqjfn2" : "" # Official `CentOS 6 (x86_64) - with Updates HVM` by CentOS product id
50+
])
3451
}
3552

3653
owners = ["679593333241"]
3754

38-
name_regex = "^CentOS Linux ${var.release} x86_64 HVM EBS ENA"
3955
}
4056

4157
output "id" {
4258
value = data.aws_ami.centos.id
4359
description = "ID of the AMI"
4460
}
4561

62+
output "name" {
63+
value = data.aws_ami.centos.name
64+
description = "Name of the AMI"
65+
}
66+

‎modules/ami-centos/versions.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22
terraform {
3-
required_version = ">= 0.12"
3+
required_version = ">= 0.13" # minimun level has to be 0.13 because of validations.
44
}

0 commit comments

Comments
 (0)
This repository has been archived.