使用 Terraform 在 EC2 Windows 实例中初始化 EBS 卷 [英] Initializing EBS Volume in EC2 Windows Instance using Terraform
本文介绍了使用 Terraform 在 EC2 Windows 实例中初始化 EBS 卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个自定义的 Windows AMI,每当我使用它创建一个新实例时,我都想根据要求自动初始化一个额外的驱动器.我正在尝试使用 Terraform 执行此操作.
I have a custom windows AMI and whenever i create a new Instance using it I want to Initialize an additional drive automatically depending on the requirements. I am trying to do this using Terraform.
我正在获取驱动器,但必须手动对其进行初始化.
I am getting the drive but had to manually initialize it.
创建和 ec2 实例并附加卷.
create and ec2 instance and attach volume.
resource "aws_instance" "example" {
ami = "ami-098792953bbd38e18"
instance_type = "t2.micro"
}
resource "aws_ebs_volume" "ebs-volume-1" {
availability_zone = "eu-west-1a"
size = 160
type = "st1"
tags {
Name = "more volume"
}
}
resource "aws_volume_attachment" "ebs-volume-1-attachment" {
device_name = "/dev/sdf"
volume_id = "${aws_ebs_volume.ebs-volume-1.id}"
instance_id = "${aws_instance.example.id}"
}
推荐答案
我在 Ubuntu 18 上做这个,所以你必须修改但把它放在这里,因为这不是显而易见的 imo.
I'm doing this on Ubuntu 18 so you will have to modify but putting this here because this is non-obvious imo.
### instance.tf
resource "aws_instance" "main" {
user_data = data.local_file.user_data
# ...
}
### user_data.sh
DEVICE="/dev/xvdf"
# Get device id
DEVICE_FS=`sudo lsblk -o UUID -d ${DEVICE} -n`
# Create a file system on the volume if one does not already exist
if [ "`echo -n $DEVICE_FS`" == "" ] ; then
mkfs.ext4 ${DEVICE}
fi
# Create a mount point directory
sudo mkdir /data
# Backup fstab
sudo cp /etc/fstab /etc/fstab.orig
# Setup auto mount on reboot
echo "UUID=${DEVICE_FS} /data xfs defaults,nofail 0 2" | sudo tee -a /etc/fstab
# Unmount
sudo umount /data
# Mount and exit on error
sudo mount -a
if [ $? -eq 0 ]
then
echo "Mounted"
else
exit 1
fi
这篇关于使用 Terraform 在 EC2 Windows 实例中初始化 EBS 卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文