I am getting Error in terraform execution with “Can’t access attributes on a primitive-typed value (number).”
Getting Error on ebs_block_mapping variables , I have requirement to add the ebs volumes with different size & type so i have included with mappings .
However ebs_block_mapping variable doesn’t get substituted properly.
Could you please help on this ?
main.tf :
### EC2 Instance creation
resource "aws_instance" "instance" {
ami = var.ami_id
subnet_id = var.subnet_id
instance_type = var.instance_type
key_name = var.key_pair
iam_instance_profile = var.iam_instance_profile
vpc_security_group_ids = var.security_group_id
root_block_device {
volume_type = var.root_volume_type
volume_size = var.root_volume_size
delete_on_termination = var.delete_on_termination
encrypted = "true"
tags = {
"Name" = "${var.instance_name}-root"
}
}
dynamic "ebs_block_device" {
for_each = [for s in var.ebs_device_mappings != null ? [0] : [] : {
name = s.name
size = s.size
type = s.type
}]
content {
device_name = ebs_block_device.value["name"]
volume_size = ebs_block_device.value["size"]
volume_type = ebs_block_device.value["type"]
delete_on_termination = true
encrypted = true
tags = {
"Name" = "${var.instance_name}-ebs"
}
}
}
}
test.tfvars :
instance_type = "t2.xlarge"
ami_id = "ami-05de6de"
ebs_device_mappings = [{ name = "/dev/sdd", size = 50, type = "gp3" }]
security_group_id = ["sg-xxxxxxxxxxx"]
root_volume_type = "gp3"
root_volume_size = "60"
delete_on_termination = "true"
subnet_id = "subnet-xxxxxxxxxxxx"
iam_instance_profile = "test-role"
instance_name = "test"
variables.tf :
variable "instance_name" {
description = "Name tag for EC2 instance"
}
variable "ami_id" {
description = "AMI ID for EC2 instance"
}
variable "subnet_id" {
description = "Subnet ID for EC2 instance"
}
variable "instance_type" {
description = "Instance type for EC2 instance"
}
variable "key_pair" {
description = "Key pair name for EC2 instance"
default = null
}
variable "iam_instance_profile" {
description = "IAM Instance Profile to launch the instance with"
}
variable "security_group_id" {
description = "Security group ID for EC2"
}
variable "root_volume_type" {
description = "EBS root volume type"
}
variable "root_volume_size" {
description = "EBS root volume size"
}
variable "delete_on_termination" {
description = "EBS volume deletion on EC2 termination"
}
variable "ebs_device_mappings" {
default = null
description = "Need to provide the device name,volume size and volume type"
}