ranjeet guha
ranjeet guha

Reputation: 1

how to put a hook in terraform main.tf as shell script to enable the additional EBS volume for delete_on_termination

I need a help to create the hook in shell script under main.tf file so that whenever i spin the server and additional ebs volume should get enabled for deleteontermination. below i have shared the code which i have created as shellscript.


  provisioner "local-exec" {
    interpreter = ["bash", "-x"]
    command = <<EOT
      #!/bin/bash -x
      echo "Getting device name..."
      DEVICE_NAME=$(lsblk -o NAME,MOUNTPOINT | awk '/^sdf[b-z]/ && $2=="" {print "/dev/"$1}' | head -1)
      echo "Device name is: $DEVICE_NAME"
      echo "Getting volume ID..."
      VOLUME_ID=$(aws ec2 describe-volumes --filters Name=attachment.instance-id,Values=$INSTANCE_ID Name=attachment.device,Values=$DEVICE_NAME --query 'Volumes[*].VolumeId' --output text)
      echo "Volume ID is: $VOLUME_ID"
      if [[ -n "$VOLUME_ID" ]]; then
        aws ec2 modify-instance-attribute --instance-id $INSTANCE_ID --block-device-mappings "[{\"DeviceName\": \"$DEVICE_NAME\", \"Ebs\":{\"VolumeId\":\"${aws_ebs_volume.this[each.key].id}\",\"DeleteOnTermination\":true}}]"
      else
        echo "Failed to find EBS volume attached to instance $INSTANCE_ID with device name $DEVICE_NAME" >&2
        exit 1
      fi
    EOT
  }
}

also i have shared the error message which iit shows while running spinning the server.

module.EC2_TERRAFORMTESTING.aws_volume_attachment.this["_0~_0"]: Provisioning with 'local-exec'...

[2023-04-11T21:23:30.099Z] module.EC2_TERRAFORMTESTING.aws_volume_attachment.this["_0~_0"] (local-exec): Executing: ["bash" "-x" "      #!/bin/bash -x\n      echo \"Getting device name...\"\n      DEVICE_NAME=$(lsblk -o NAME,MOUNTPOINT | awk '/^sdf[b-z]/ && $2==\"\" {print \"/dev/\"$1}' | head -1)\n      echo \"Device name is: $DEVICE_NAME\"\n      echo \"Getting volume ID...\"\n      VOLUME_ID=$(aws ec2 describe-volumes --filters Name=attachment.instance-id,Values=$INSTANCE_ID Name=attachment.device,Values=$DEVICE_NAME --query 'Volumes[*].VolumeId' --output text)\n      echo \"Volume ID is: $VOLUME_ID\"\n      if [[ -n \"$VOLUME_ID\" ]]; then\n        aws ec2 modify-instance-attribute --instance-id $INSTANCE_ID --block-device-mappings \"[{\\\"DeviceName\\\": \\\"$DEVICE_NAME\\\", \\\"Ebs\\\":{\\\"VolumeId\\\":\\\"vol-091235c2b5d98d1c6\\\",\\\"DeleteOnTermination\\\":true}}]\"\n      else\n        echo \"Failed to find EBS volume attached to instance $INSTANCE_ID with device name $DEVICE_NAME\" >&2\n        exit 1\n      fi\n"]

[2023-04-11T21:23:30.099Z] module.EC2_TERRAFORMTESTING.aws_volume_attachment.this["_0~_0"] (local-exec): bash:       #!/bin/bash -x

[2023-04-11T21:23:30.099Z] module.EC2_TERRAFORMTESTING.aws_volume_attachment.this["_0~_0"] (local-exec):       echo "Getting device name..."

[2023-04-11T21:23:30.099Z] module.EC2_TERRAFORMTESTING.aws_volume_attachment.this["_0~_0"] (local-exec):       DEVICE_NAME=$(lsblk -o NAME,MOUNTPOINT | awk '/^sdf[b-z]/ && $2=="" {print "/dev/"$1}' | head -1)

[2023-04-11T21:23:30.099Z] module.EC2_TERRAFORMTESTING.aws_volume_attachment.this["_0~_0"] (local-exec):       echo "Device name is: $DEVICE_NAME"

[2023-04-11T21:23:30.099Z] module.EC2_TERRAFORMTESTING.aws_volume_attachment.this["_0~_0"] (local-exec):       echo "Getting volume ID..."

[2023-04-11T21:23:30.099Z] module.EC2_TERRAFORMTESTING.aws_volume_attachment.this["_0~_0"] (local-exec):       VOLUME_ID=$(aws ec2 describe-volumes --filters Name=attachment.instance-id,Values=$INSTANCE_ID Name=attachment.device,Values=$DEVICE_NAME --query 'Volumes[*].VolumeId' --output text)

[2023-04-11T21:23:30.099Z] module.EC2_TERRAFORMTESTING.aws_volume_attachment.this["_0~_0"] (local-exec):       echo "Volume ID is: $VOLUME_ID"

[2023-04-11T21:23:30.099Z] module.EC2_TERRAFORMTESTING.aws_volume_attachment.this["_0~_0"] (local-exec):       if [[ -n "$VOLUME_ID" ]]; then

[2023-04-11T21:23:30.099Z] module.EC2_TERRAFORMTESTING.aws_volume_attachment.this["_0~_0"] (local-exec):         aws ec2 modify-instance-attribute --instance-id $INSTANCE_ID --block-device-mappings "[{\"DeviceName\": \"$DEVICE_NAME\", \"Ebs\":{\"VolumeId\":\"vol-091235c2b5d98d1c6\",\"DeleteOnTermination\":true}}]"

[2023-04-11T21:23:30.099Z] module.EC2_TERRAFORMTESTING.aws_volume_attachment.this["_0~_0"] (local-exec):       else

[2023-04-11T21:23:30.099Z] module.EC2_TERRAFORMTESTING.aws_volume_attachment.this["_0~_0"] (local-exec):         echo "Failed to find EBS volume attached to instance $INSTANCE_ID with device name $DEVICE_NAME" >&2

[2023-04-11T21:23:30.099Z] module.EC2_TERRAFORMTESTING.aws_volume_attachment.this["_0~_0"] (local-exec):         exit 1

[2023-04-11T21:23:30.099Z] module.EC2_TERRAFORMTESTING.aws_volume_attachment.this["_0~_0"] (local-exec):       fi

[2023-04-11T21:23:30.099Z] module.EC2_TERRAFORMTESTING.aws_volume_attachment.this["_0~_0"] (local-exec): : No such file or directory

[2023-04-11T21:23:30.099Z] ╷

[2023-04-11T21:23:30.099Z] │ Warning: Deprecated attribute

[2023-04-11T21:23:30.099Z] │ 

[2023-04-11T21:23:30.099Z] │   on .terraform/modules/S3_TF_TESTING_BUCKET_FDL001/modules/s3/main.tf line 27, in resource "aws_s3_bucket" "this":

[2023-04-11T21:23:30.099Z] │   27:       acceleration_status,

[2023-04-11T21:23:30.099Z] │ 

[2023-04-11T21:23:30.099Z] │ The attribute "acceleration_status" is deprecated. Refer to the provider

[2023-04-11T21:23:30.099Z] │ documentation for details.

[2023-04-11T21:23:30.099Z] │ 

[2023-04-11T21:23:30.099Z] │ (and 2 more similar warnings elsewhere)

[2023-04-11T21:23:30.099Z] ╵

[2023-04-11T21:23:30.099Z] ╷

[2023-04-11T21:23:30.099Z] │ Error: local-exec provisioner error

[2023-04-11T21:23:30.099Z] │ 

[2023-04-11T21:23:30.099Z] │   with module.EC2_TERRAFORMTESTING.aws_volume_attachment.this["_0~_0"],

[2023-04-11T21:23:30.099Z] │   on .terraform/modules/EC2_TERRAFORMTESTING/modules/ec2/main.tf line 197, in resource "aws_volume_attachment" "this":

[2023-04-11T21:23:30.099Z] │  197:   provisioner "local-exec" {

[2023-04-11T21:23:30.099Z] │ 

[2023-04-11T21:23:30.099Z] │ Error running command '      #!/bin/bash -x

[2023-04-11T21:23:30.099Z] │       echo "Getting device name..."

[2023-04-11T21:23:30.099Z] │       DEVICE_NAME=$(lsblk -o NAME,MOUNTPOINT | awk '/^sdf[b-z]/ && $2=="" {print "/dev/"$1}' | head -1)

[2023-04-11T21:23:30.099Z] │       echo "Device name is: $DEVICE_NAME"

[2023-04-11T21:23:30.099Z] │       echo "Getting volume ID..."

[2023-04-11T21:23:30.099Z] │       VOLUME_ID=$(aws ec2 describe-volumes --filters Name=attachment.instance-id,Values=$INSTANCE_ID Name=attachment.device,Values=$DEVICE_NAME --query 'Volumes[*].VolumeId' --output text)

[2023-04-11T21:23:30.099Z] │       echo "Volume ID is: $VOLUME_ID"

[2023-04-11T21:23:30.099Z] │       if [[ -n "$VOLUME_ID" ]]; then

[2023-04-11T21:23:30.099Z] │         aws ec2 modify-instance-attribute --instance-id $INSTANCE_ID --block-device-mappings "[{\"DeviceName\": \"$DEVICE_NAME\", \"Ebs\":{\"VolumeId\":\"vol-091235c2b5d98d1c6\",\"DeleteOnTermination\":true}}]"

[2023-04-11T21:23:30.099Z] │       else

[2023-04-11T21:23:30.099Z] │         echo "Failed to find EBS volume attached to instance $INSTANCE_ID with device name $DEVICE_NAME" >&2

[2023-04-11T21:23:30.099Z] │         exit 1

[2023-04-11T21:23:30.099Z] │       fi

[2023-04-11T21:23:30.099Z] │ ': exit status 127. Output: bash:       #!/bin/bash -x

[2023-04-11T21:23:30.099Z] │       echo "Getting device name..."

[2023-04-11T21:23:30.099Z] │       DEVICE_NAME=$(lsblk -o NAME,MOUNTPOINT | awk '/^sdf[b-z]/ && $2=="" {print "/dev/"$1}' | head -1)

[2023-04-11T21:23:30.099Z] │       echo "Device name is: $DEVICE_NAME"

[2023-04-11T21:23:30.099Z] │       echo "Getting volume ID..."

[2023-04-11T21:23:30.099Z] │       VOLUME_ID=$(aws ec2 describe-volumes --filters Name=attachment.instance-id,Values=$INSTANCE_ID Name=attachment.device,Values=$DEVICE_NAME --query 'Volumes[*].VolumeId' --output text)

[2023-04-11T21:23:30.099Z] │       echo "Volume ID is: $VOLUME_ID"

[2023-04-11T21:23:30.099Z] │       if [[ -n "$VOLUME_ID" ]]; then

[2023-04-11T21:23:30.099Z] │         aws ec2 modify-instance-attribute --instance-id $INSTANCE_ID --block-device-mappings "[{\"DeviceName\": \"$DEVICE_NAME\", \"Ebs\":{\"VolumeId\":\"vol-091235c2b5d98d1c6\",\"DeleteOnTermination\":true}}]"

[2023-04-11T21:23:30.099Z] │       else

[2023-04-11T21:23:30.100Z] │         echo "Failed to find EBS volume attached to instance $INSTANCE_ID with device name $DEVICE_NAME" >&2

[2023-04-11T21:23:30.100Z] │         exit 1

[2023-04-11T21:23:30.100Z] │       fi

[2023-04-11T21:23:30.100Z] │ : No such file or directory

Upvotes: 0

Views: 263

Answers (1)

Mark B
Mark B

Reputation: 201138

The method you are using, with a provisioner that calls the AWS CLI, is extremely error prone, and entirely unneeded. Terraform provides a way to set the delete_on_termination setting on any block devices created as part of an EC2 instance creation.

To specify delete_on_termination in Terraform for EBS volumes created during an EC2 instance creation, you do that like this:

resource "aws_instance" "instance" {
  # Your other instance settings here

  # To specify settings for the root block device
  root_block_device {
    # Other block device settings
    delete_on_termination = true
  }

  # To specify settings for additional block devices
  ebs_block_device {
    # Other block device settings
    delete_on_termination = true
  }
}

Also, note that the default value for this setting is true, so your script was entirely unnecessary anyway.

Upvotes: 0

Related Questions