Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
snemetz committed Feb 17, 2018
0 parents commit 970440e
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.terraform
terraform*

14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
terraform-aws-ami-ids
================

Terraform module to lookup AMI IDs for use in other resources

Usage
-----

```hcl
module "ami_id" {
source =
distribution = "ecs"
}
```
41 changes: 41 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Lookups on marketplace seem slower than others

locals {
names = "amazonlinux,ecs,ubuntu1604"

owners = "${join(",", list(
var.ami_owners["amazon"],
var.ami_owners["amazon"],
var.ami_owners["canonical"]
))}"

patterns = "${join(",", list(
"amzn-ami-hvm-*-x86_64-gp2",
"amzn-ami-${var.ami_version_ecs}-amazon-ecs-optimized",
"ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"
))}"
}

# CentOS: CentOS Linux 7 x86_64 HVM EBS*
# Fedora: Fedora-Cloud-Base-27-1.6.x86_64-us-west-2-HVM-gp2-0

data "aws_ami" "ami" {
count = "${length(split(",", local.names))}"
most_recent = true
owners = ["${element(split(",", local.owners),count.index)}"]

filter {
name = "name"
values = ["${element(split(",", local.patterns),count.index)}"]
}

filter {
name = "architecture"
values = ["x86_64"]
}

filter {
name = "virtualization-type"
values = ["hvm"]
}
}
19 changes: 19 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
output "ami_id" {
description = "AMI ID for Linux distribution"
value = "${element(data.aws_ami.ami.*.id, index(split(",", local.names), var.distribution))}"
}

output "ami_ids" {
description = "All AMI IDs"
value = "${data.aws_ami.ami.*.id}"
}

output "ami_names" {
description = "All AMI Names"
value = "${data.aws_ami.ami.*.name}"
}

output "distro_names" {
description = "All distribution names. Can be used to index the other lists"
value = "${split(",", local.names)}"
}
20 changes: 20 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
variable "ami_owners" {
description = "AMI Owner IDs"
type = "map"

default = {
amazon = "amazon"
centos = "679593333241" # Marketplace
canonical = "099720109477"
fedora = "125523088429"
}
}

variable "ami_version_ecs" {
description = "Amazon ECS Optimized version to get"
default = "*"
}

variable "distribution" {
description = "Linux distribution for AWS AMI, supported: amazonlinux, ecs, ubuntu1604"
}

0 comments on commit 970440e

Please sign in to comment.