Skip to content

Instantly share code, notes, and snippets.

@yarbelk yarbelk/
Last active Jun 17, 2017

What would you like to do?
Create, partiton and format EBS volume in a script for AWS EBS
resource "aws_launch_configuration" "test" {
name_prefix = "lc-test-"
image_id = "${data.aws_ami.ubuntu.image_id}"
instance_type = "t2.small"
security_groups = ["${}"]
associate_public_ip_address = false
key_name = "${var.bastion_keypair_name}" /* bastion == bounce host since there is no public ip */
user_data = "${data.template_file.user_data.rendered}"
iam_instance_profile = "${aws_iam_instance_profile.test.arn}"
ebs_optimized = true
ebs_block_device {
device_name = "/dev/sdf"
volume_type = "standard"
volume_size = "${var.storage_size}" /* number in GB */
delete_on_termination = false
encrypted = true /* no excuses. though, you should manage your keys as well */
lifecycle {
create_before_destroy = true /* bad things happen if you don't do this */
# This is your 'template' in its rendered state, comments for unrendered
# In your launch configuration, attach a device with name /dev/sdf
# In terraform
# Question - how do i know the device is ready?
# Answer: web calls
export AWS_DEFAULT_REGION=${region}
aws ec2 wait volume-in-use --filters "Name=attachment.instance-id,Values=$INSTANCE_ID,Name=attachment.device,Values=/dev/sdf,Name=attachment.status,Values=attached"
# This is hard coded, because I'm too lazy to on the fly translate the /dev/sdaf into /dev/xvdf
# But it would be something like `echo ${device_name} | sed -- "s/sda/xvd/g"`
# < <( ... ) is process substitution. Kinda like a backwards pipe:
sudo sfdisk /dev/xvdf < <(echo "start=2048, type=83")
sudo mkfs.ext4 /dev/xvdf1
BLOCK_ID=`blkid /dev/xvdf1 -s UUID -o value`
# Setup your mount points. For example, (read man hier !), to store cassandra data in /var/opt/
echo "UUID=$BLOCK_ID /var/opt/cassandra ext4 default,nofail,nobootwait 0 2" >> /etc/fstab
sudo mount -a
# do rest of setup stuff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.