Skip to content

Commit 970440e

Browse files
committed
first commit
0 parents  commit 970440e

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.terraform
2+
terraform*
3+

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
terraform-aws-ami-ids
2+
================
3+
4+
Terraform module to lookup AMI IDs for use in other resources
5+
6+
Usage
7+
-----
8+
9+
```hcl
10+
module "ami_id" {
11+
source =
12+
distribution = "ecs"
13+
}
14+
```

main.tf

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Lookups on marketplace seem slower than others
2+
3+
locals {
4+
names = "amazonlinux,ecs,ubuntu1604"
5+
6+
owners = "${join(",", list(
7+
var.ami_owners["amazon"],
8+
var.ami_owners["amazon"],
9+
var.ami_owners["canonical"]
10+
))}"
11+
12+
patterns = "${join(",", list(
13+
"amzn-ami-hvm-*-x86_64-gp2",
14+
"amzn-ami-${var.ami_version_ecs}-amazon-ecs-optimized",
15+
"ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"
16+
))}"
17+
}
18+
19+
# CentOS: CentOS Linux 7 x86_64 HVM EBS*
20+
# Fedora: Fedora-Cloud-Base-27-1.6.x86_64-us-west-2-HVM-gp2-0
21+
22+
data "aws_ami" "ami" {
23+
count = "${length(split(",", local.names))}"
24+
most_recent = true
25+
owners = ["${element(split(",", local.owners),count.index)}"]
26+
27+
filter {
28+
name = "name"
29+
values = ["${element(split(",", local.patterns),count.index)}"]
30+
}
31+
32+
filter {
33+
name = "architecture"
34+
values = ["x86_64"]
35+
}
36+
37+
filter {
38+
name = "virtualization-type"
39+
values = ["hvm"]
40+
}
41+
}

outputs.tf

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
output "ami_id" {
2+
description = "AMI ID for Linux distribution"
3+
value = "${element(data.aws_ami.ami.*.id, index(split(",", local.names), var.distribution))}"
4+
}
5+
6+
output "ami_ids" {
7+
description = "All AMI IDs"
8+
value = "${data.aws_ami.ami.*.id}"
9+
}
10+
11+
output "ami_names" {
12+
description = "All AMI Names"
13+
value = "${data.aws_ami.ami.*.name}"
14+
}
15+
16+
output "distro_names" {
17+
description = "All distribution names. Can be used to index the other lists"
18+
value = "${split(",", local.names)}"
19+
}

variables.tf

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
variable "ami_owners" {
2+
description = "AMI Owner IDs"
3+
type = "map"
4+
5+
default = {
6+
amazon = "amazon"
7+
centos = "679593333241" # Marketplace
8+
canonical = "099720109477"
9+
fedora = "125523088429"
10+
}
11+
}
12+
13+
variable "ami_version_ecs" {
14+
description = "Amazon ECS Optimized version to get"
15+
default = "*"
16+
}
17+
18+
variable "distribution" {
19+
description = "Linux distribution for AWS AMI, supported: amazonlinux, ecs, ubuntu1604"
20+
}

0 commit comments

Comments
 (0)