1

I'm trying to design it so that all configurable parameters exist within variables.tf...the issue I'm running into currently is for provisioning EC2 instances. I'd like to be able to define the security group to use, but the input variable only accepts strings, so I wasn't able to reference the security group id.

I moved on to trying this, which is looking up the resource id dynamically by inserting the name (look at security_groups_name and subnet_name:

variable "ansible_ami" { 
    type =  "map"
    default = {
            name = "ansible"
            ami = "ami-4836a428"
            size = "t2.micro"
            ebs_size = "8"
            ebs_type = "gp2" #general purpose ssd
            enable_public_ip = true
            security_groups_name = "JumpBox"
            subnet_name = "pubnet1"
    }
}


resource "aws_instance" "ansible" {
    ami = "${var.ansible_ami["ami"]}"
    instance_type = "${var.ansible_ami["size"]}"
    security_groups = "${aws_security_group.${var.ansible_ami["security_groups_name"]}.id}"
    subnet_id = "${aws_subnet.${var.ansible_ami["subnet_name"]}.id}"
    associate_public_ip_address = "${var.ansible_ami["enable_public_ip"]}"
    #user_data = ""

    ebs_block_device {
        device_name = "/dev/sdb"
        volume_size = "${var.ansible_ami["ebs_size"]}"
        volume_type = "${var.ansible_ami["ebs_type"]}"
    }

    tags {
        Name = "${var.ansible_ami["name"]}"
    }
}

However, that is not working either since i'm getting syntax errors from the lines where I'm trying to next variables. I'm looking for ideas or direction on how to accomplish this.

1

Largely inspired by the documentation the idea is to create a terraform datasource from a filter of the available resources and then use this as entry point in your ami resource:

variable "security_group_id" {}
variable "subnet_id" {}

data "aws_security_group" "selected" {
  filter {
    name = "${var.ansible_ami["security_groups_name"]}}"
  }
}

data "aws_subnet" "selected" {
  filter {
    name = "${var.ansible_ami["subnet_name"]}"
  }
}

resource "aws_instance" "ansible" {
    ami = "${var.ansible_ami["ami"]}"
    instance_type = "${var.ansible_ami["size"]}"
    security_groups = "${data.aws_security_group.selected.id}"
    subnet_id = "${data.aws_subnet.selected.id}"
    associate_public_ip_address = "${var.ansible_ami["enable_public_ip"]}"
    #user_data = ""

I'm not using terraform myself, so this could be plain wrong.

  • Thanks. I think there is some merit to this. But, it seems like it needs to be done with filter {} inside of the data source since there is no name parameter for it. The only other option is id but then im back to variable inside of variable to get that. I ended up just putting the security group inside of the the ec2 objects but may revisit this in the future. Upvoted for now. – user3633188 Jun 15 '17 at 14:26
  • aww, indeed, missed the line about it in the documentation. edited to fix this. – Tensibai Jun 15 '17 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.