<p>Largely inspired by the <a href="https://www.terraform.io/docs/providers/aws/d/security_group.html" rel="nofollow noreferrer">documentation</a> 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:</p>

<pre><code>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 = ""
</code></pre>

<p>I'm not using terraform myself, so this could be plain wrong.</p>

CC BY-SA 3.0
2017-06-15T14:26:08.070Z
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
CC BY-SA 3.0
2017-06-15T14:30:57.407Z
aww, indeed, missed the line about it in the documentation. edited to fix this.
Tensibai
CC BY-SA 3.0
2017-06-14T08:45:14.623Z
2017-06-15T14:32:29.783Z
2017-06-15T14:32:29.783Z
Tensibai
<p>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.</p>

<p>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:</p>

<pre><code>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"]}"
    }
}
</code></pre>

<p>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.</p>

CC BY-SA 3.0
2017-06-14T08:39:59.727Z
May worth a try from the example in the documentation: https://www.terraform.io/docs/providers/aws/d/security_group.html
Tensibai
<amazon-web-services><terraform>
Terraform: How to use a variable inside of another variable?
user3633188
# Terraform: How to use a variable inside of another variable?

(Asked by: username_0 on 2017-06-11)

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.

**Tags:** amazon-web-services, terraform

> **Comments:**
> **username_1**: May worth a try from the example in the documentation: https://www.terraform.io/docs/providers/aws/d/security_group.html  

---

## 1 answers

### Answer by username_1 on 2017-06-14. Score: 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.

> **Comments:**
> **username_0**: 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.  
> **username_1**: aww, indeed, missed the line about it in the documentation. edited to fix this.  

---

