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}"
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}"
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}"
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$