Last active
June 17, 2017 08:11
-
-
Save yarbelk/546bf144f023d601036c41671ab79073 to your computer and use it in GitHub Desktop.
Create, partiton and format EBS volume in a script for AWS EBS
This file contains hidden or 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_launch_configuration" "test" { | |
name_prefix = "lc-test-" | |
image_id = "${data.aws_ami.ubuntu.image_id}" | |
instance_type = "t2.small" | |
security_groups = ["${aws_security_group.test.id}"] | |
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 file contains hidden or 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 | |
# 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 | |
INSTANCE_ID=`curl http://169.254.169.254/latest/meta-data/instance-id/` | |
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: | |
# https://stackoverflow.com/questions/2443085/what-does-cmd-args-mean-in-the-shell | |
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