Last active
June 14, 2021 23:20
-
-
Save phinze/6610c1dd727ccfdb810a to your computer and use it in GitHub Desktop.
Terraform Example: ebs_block_device that remains after instance termination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resource "aws_instance" "web" { | |
ami = "ami-7f89a64f" | |
instance_type = "t1.micro" | |
ebs_block_device { | |
device_name = "/dev/sdg" | |
volume_size = 5 | |
volume_type = "gp2" | |
delete_on_termination = false | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get EC2 Instance ID from Terraform Output | |
INSTANCE_ID=$(terraform show | grep -A 1 aws_instance.web | tail -n 1 | awk '{print $3}') | |
# Get the /dev/sdg volume details from AWS CLI | |
aws ec2 describe-volumes --filters Name=attachment.instance-id,Values=$INSTANCE_ID --filters Name=attachment.device,Values=/dev/sdg | |
# Use jq to scope those details down to just Volume ID | |
aws ec2 describe-volumes --filters Name=attachment.instance-id,Values=$INSTANCE_ID --filters Name=attachment.device,Values=/dev/sdg | jq -r '.Volumes[].VolumeId' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is old, but I came across this and thought I'd help anyone who comes across it in the future.
@mikeab2685 One way to create device mappings using terraform is with a user_data script. This is a script that runs once after the server instance is created, so it's a good opportunity to mount drives and fix permissions errors.
https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-windows-user-data.html