Created
April 22, 2019 14:22
-
-
Save Alexhha/d87f754f0640800aaa5d08a6e96bd679 to your computer and use it in GitHub Desktop.
terraform-aws-dynamic-ami-id.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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