terraform-aws-modules / terraform-aws-ec2-instance

Terraform module to create AWS EC2 instance(s) resources πŸ‡ΊπŸ‡¦
https://registry.terraform.io/modules/terraform-aws-modules/ec2-instance/aws
Apache License 2.0
754 stars 1.87k forks source link

Root_Block_Device is not expected here #292

Closed andreprawira closed 1 year ago

andreprawira commented 2 years ago

Description

Getting a Root_Block_Device is not expected here error message when using an example from this repo

Versions

Reproduction Code [Required]

root_block_device = [ { encrypted = true volume_type = "gp3" throughput = 200 volume_size = 50 tags = { Name = "my-root-block" } }, ]

Steps to reproduce the behavior:

no yes

Expected behavior

Root block device should have been succesfully created

Actual behavior

When copy pasting the example directly, i would get that error

Terminal Output Screenshot(s)

An argument named "root_block_device" is not expected here. Did you mean to define a block of type "root_block_device"?

Additional context

This is the fix

root_block_device { volume_size = "${var.EC2_ROOT_VOLUME_SIZE}" volume_type = "${var.EC2_ROOT_VOLUME_TYPE}" delete_on_termination = "${var.EC2_ROOT_VOLUME_DELETE_ON_TERMINATION}" }

tofupup commented 2 years ago

Can you confirm you're trying to use the terraform-aws-ec2-instance module, and not the aws_instance resource directly from the terraform-provider-aws provider? The terraform-aws-ec2-instance module expects root_block_device to be a list, as your reproduction code shows, while the aws_instance resource expects it as a nested configuration block. Your fix is not a list, and since it works for you it makes me think you're not using terraform-aws-ec2-instance.

Here's a simplified reproduction of creating an instance using terraform-aws-ec2-instance that can be run with terraform init && terraform apply that will hopefully help you.

data "aws_ssm_parameter" "ubuntu-jammy-amd64" {
    name = "/aws/service/canonical/ubuntu/server/22.04/stable/current/amd64/hvm/ebs-gp2/ami-id"
}

module "ec2_instance" {
  source  = "terraform-aws-modules/ec2-instance/aws"
  version = "4.1.4"

  name = "t2-root-block"

  ami                    = data.aws_ssm_parameter.ubuntu-jammy-amd64.value
  instance_type          = "t2.micro"
  monitoring             = false

  enable_volume_tags = false
  root_block_device = [
    {
      encrypted   = false
      volume_type = "gp3"
      throughput  = 200
      volume_size = 50
      tags = {
        Name = "my-root-block"
      }
    },
  ]

  tags = {
    Test = "root-block"
  }
}

output "instance_id" {
  value = module.ec2_instance.id
}
❯ terraform apply
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

instance_id = "i-04b6f31fdb3db51cb"
❯ aws ec2 describe-volumes --filters Name=attachment.instance-id,Values=i-04b6f31fdb3db51cb
{
    "Volumes": [
        {
            "Attachments": [
                {
                    "AttachTime": "2022-09-06T00:06:33+00:00",
                    "Device": "/dev/sda1",
                    "InstanceId": "i-04b6f31fdb3db51cb",
                    "State": "attached",
                    "VolumeId": "vol-059f0dd86d2429d73",
                    "DeleteOnTermination": true
                }
            ],
            "AvailabilityZone": "us-east-1a",
            "CreateTime": "2022-09-06T00:06:33.375000+00:00",
            "Encrypted": false,
            "Size": 50,
            "SnapshotId": "snap-0da515346cb3d642b",
            "State": "in-use",
            "VolumeId": "vol-059f0dd86d2429d73",
            "Iops": 3000,
            "Tags": [
                {
                    "Key": "Name",
                    "Value": "my-root-block"
                }
            ],
            "VolumeType": "gp3",
            "MultiAttachEnabled": false,
            "Throughput": 200
        }
    ]
}
github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has been open 30 days with no activity. Remove stale label or comment or this issue will be closed in 10 days

github-actions[bot] commented 1 year ago

This issue was automatically closed because of stale in 10 days

github-actions[bot] commented 1 year ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.