Inappropriate value for attribute "instance_id": string required to attach EBS volume

2,404 views
Skip to first unread message

Suresh Akkemgari

unread,
Aug 12, 2019, 9:50:38 AM8/12/19
to Terraform
Here is my terraform code:


module "ec2-server" {
    source = "./modules/ec2"

    name                 = "SC-Jenkins-Server"
    project              = "${var.project}"
    environment          = "${var.environment}"
    ec2_ssh_user         = "${var.ec2_ssh_user}"
    ec2_ssh_key_path     = "${var.ec2_ssh_key_path}"
    ec2_ssh_key_name     = "${var.ec2_ssh_key_name}"
    ami_distro           = "${var.ami_distro}"
    ami_id               = "${var.ami_id}"
    ami_owner            = "${var.ami_owner}"
    instance_type        = "${var.server_instance_type}"
    user_data            = "${data.template_file.server_user_data.rendered}"
    iam_instance_profile = "${data.aws_iam_instance_profile.iamrole.name}"
    security_groups      = "${data.aws_security_group.sg.id}"
    subnet_ids           = "${list(element(split(",", var.private_subnet_ids), var.server_subnet_element))}"
    ssh_key              = "${var.ec2_ssh_key_path}"
    files_local_path     = "${path.root}/secrets"
    alarm_arn            = "${var.alarm_arn}"
}

resource "aws_volume_attachment" "ebs_backup" {
    count =  1
    device_name  = "${var.server_backup_volume_dev}"
    volume_id    = "${data.aws_ebs_volume.ebs_volume.id}"
    instance_id  = "${element(module.ec2-server.ec2_id, 1)}"
    #instance_id  = "${module.ec2-server.aws_instance.ec2.*.id[count.index]}"
    skip_destroy = "true"
}


resource "aws_instance" "ec2" {
    count                       = "${var.instance_count == "0" ? "1" : var.instance_count}"
    ami                         = "${data.aws_ami.ami.id}"
    instance_type               = "${var.instance_type}"
    key_name                    = "${var.ec2_ssh_key_name}"
    vpc_security_group_ids      = "${split(",", var.security_groups)}"
    subnet_id                   = "${element(var.subnet_ids, count.index)}"
    associate_public_ip_address = "${var.associate_public_ip_address}"
    iam_instance_profile        = "${var.iam_instance_profile}"
    user_data                   = "${var.user_data}"

    root_block_device  {
        volume_type = "gp2"
        volume_size = "50" // gigabytes
    }

    tags = {
        Name        = "${var.name}${var.instance_count == "0" ? "" : format("%02d", count.index + 1)}-${var.environment}"
        Application = "${var.environment}"
        Project     = "${var.project}"
        CountIndex  = "${var.instance_count == "0" ? "" : format("%02d", count.index + 1)}"
    }

  } 

output "ec2_id" {
    value = ["${aws_instance.ec2.*.id}"]
}


I am getting below to get ec2 instance id. Please let me the right syntax to read the ec2 id here. Thanks in advance!

------------------------------------------------------------------------

Error: Incorrect attribute value type

  on server.tf line 65, in resource "aws_volume_attachment" "ebs_backup":
  65:     instance_id  = "${element(module.ec2-server.ec2_id, 1)}"

Inappropriate value for attribute "instance_id": string required.

Makefile:35: recipe for target 'plan' failed
make: *** [plan] Error 1
sai9@LP5-SAI9-DSA:/apps/git/sc-jenkins-tf$

Suresh Akkemgari

unread,
Aug 12, 2019, 10:47:14 PM8/12/19
to Terraform

I got it working.
Actually, the output result was in a tuple with [ [ id-xxxxx ] ] and the element fn. was unable to retrieve the value because there is no right index to find.

Removed outside [ ] from module/ec2/ec2.tf output variable then it works as I expected.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/2e450748-09e3-402f-a4d5-04282e8a86ef%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages