oveits / openshift-terraform-ansible

Automate provisioning of OpenShift Origin/Enterprise with infrastructure as code
0 stars 0 forks source link

Replace static ami by map {regions => ami of CentOS 7} #1

Open oveits opened 8 years ago

oveits commented 8 years ago

Today we are using static AMI images:

variable "aws_region" { default = "eu-central-1" }
variable "aws_ami_master" {default = "ami-9bf712f4" }
variable "aws_ami_node" {default = "ami-9bf712f4" }
resource "aws_instance" "ose-master" {
    count = "${var.num_masters}"
    ami = "${var.aws_ami_master}"
...
}
resource "aws_instance" "ose-node" {
    count = "${var.num_nodes}"
    ami = "${var.aws_ami_node}"
...
}

However, ami IDs are region specific. Therefore this works in eu-central-1 only.

Instead of a static value, I would like to use a map like follows (list found on https://gist.github.com/gene1wood/a00d0b9d029f40e866df):

variable "aws_region" { default = "eu-central-1" }
variable "centos7-images" {
  type = map
  default = {
            "us-east-1" = "ami-6d1c2007"
            "us-west-2" = "ami-d2c924b2"
            "us-west-1" = "ami-af4333cf"
            "eu-central-1" = "ami-9bf712f4"
            "eu-west-1" = "ami-7abd0209"
            "ap-southeast-1" = "ami-f068a193"
            "ap-southeast-2" = "ami-fedafc9d"
            "ap-northeast-1" = "ami-eec1c380"
            "ap-northeast-2" = "ami-c74789a9"
            "sa-east-1" = "ami-26b93b4a"
            }
}
variable "aws_ami_master" { default = "${centos7-images.${aws_region}}" }

or

variable "aws_ami_master" { default = "${lookup(var.centos7-images, var.aws_region)}"

as I have found 'hostname = "${lookup(var.hostnames, count.index)}"' on the terraform interpolation documentation page

And then:

resource "aws_instance" "ose-master" {
    count = "${var.num_masters}"
    ami = "${var.aws_ami_master}"
...
}

However, it might be that such an interpolation of the aws_ami_master variable is not possible. In this case, we could try using the map directly:

resource "aws_instance" "ose-master" {
    count = "${var.num_masters}"
#    ami = "${var.centos7-images.eu-central-1}"
# or
    ami = "${lookup(var.centos7-images, var.aws_region)}"
...
}

drawback: if we change the region, we still need to change the file... I need to test interpolation in more detail...

oveits commented 8 years ago

As I thought: If I define

variable "aws_ami_master" {default = "${lookup(var.centos7-images, var.region)}" }

I get the error

Variable 'aws_ami_master': cannot contain interpolations

Now trying by skipping the intermediate step and directly defining:

resource "aws_instance" "ose-master" {
    ami = "${lookup(var.centos7-images, var.aws_region)}"
...
}

This seems to work...

oveits commented 8 years ago

Replaced name of variable "centos7-images" to just "images". added a comment within the map, that this is the map for CentOS 7