Created
February 2, 2016 21:21
-
-
Save codeinthehole/c91e48f341af36162f12 to your computer and use it in GitHub Desktop.
Terraform config for an EC2 instance with a replaceable EBS volume
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
#!/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 |
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_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