Skip to content

Instantly share code, notes, and snippets.

@Alexhha
Created April 22, 2019 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alexhha/d87f754f0640800aaa5d08a6e96bd679 to your computer and use it in GitHub Desktop.
Save Alexhha/d87f754f0640800aaa5d08a6e96bd679 to your computer and use it in GitHub Desktop.
terraform-aws-dynamic-ami-id.tf
data "aws_ami" "linux_ami" {
most_recent = true
owners = ["${element(split(";", var.ami_id_filter[var.linux_distro]), 1)}"]
filter {
name = "name"
values = ["${element(split(";", var.ami_id_filter[var.linux_distro]), 0)}"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "architecture"
values = ["x86_64"]
}
}
resource "aws_instance" "master" {
ami = "${data.aws_ami.linux_ami.id}"
instance_type = "t2.micro"
key_name = "test"
tags {
Name = "test"
}
lifecycle {
ignore_changes = ["user_data"]
}
}
variable "linux_distro" {
type = "string"
default = "ubuntu16"
}
variable "ami_id_filter" {
type = "map"
default = {
"ubuntu19" = "ubuntu/images/hvm-ssd/ubuntu-*-19.04-amd64-server-*;099720109477"
"ubuntu18" = "ubuntu/images/hvm-ssd/ubuntu-*-18.04-amd64-server-*;099720109477"
"ubuntu16" = "ubuntu/images/hvm-ssd/ubuntu-*-16.04-amd64-server-*;099720109477"
"ubuntu14" = "ubuntu/images/hvm-ssd/ubuntu-*-14.04-amd64-server-*;099720109477"
"debian9" = "debian-stretch-*;379101102735"
"debian8" = "debian-jessie-*;379101102735"
"centos7" = "CentOS Linux 7*;679593333241"
"centos6" = "CentOS Linux 6*;679593333241"
"rhel8" = "RHEL-8.?*;309956199498"
"rhel7" = "RHEL-7.?*GA*;309956199498"
"rhel6" = "RHEL-6.?*GA*;309956199498"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment