Skip to content

Instantly share code, notes, and snippets.

@codeinthehole
Created February 2, 2016 21:21
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save codeinthehole/c91e48f341af36162f12 to your computer and use it in GitHub Desktop.
Save codeinthehole/c91e48f341af36162f12 to your computer and use it in GitHub Desktop.
Terraform config for an EC2 instance with a replaceable EBS volume
#!/bin/bash
DEVICE=/dev/$(lsblk -n | awk '$NF != "/" {print $1}')
FS_TYPE=$(file -s $DEVICE | awk '{print $2}')
MOUNT_POINT=/data
# If no FS, then this output contains "data"
if [ "$FS_TYPE" = "data" ]
then
echo "Creating file system on $DEVICE"
mkfs -t ext4 $DEVICE
fi
mkdir $MOUNT_POINT
mount $DEVICE $MOUNT_POINT
resource "aws_ebs_volume" "test" {
availability_zone = "eu-west-1a"
size = 1
}
resource "template_file" "userdata" {
filename = "userdata.sh"
}
resource "aws_instance" "test" {
ami = "ami-be5cf7cd"
availability_zone = "eu-west-1a"
instance_type = "t1.micro"
key_name = "${module.network.key_pair_id}"
user_data = "${template_file.userdata.rendered}"
tags {
Name = "test-box"
}
}
resource "aws_volume_attachment" "ebs_att" {
device_name = "/dev/sdh"
volume_id = "${aws_ebs_volume.test.id}"
instance_id = "${aws_instance.test.id}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment