[英]How to format and mount an ephemeral disk with Terraform?
我正在編寫 Packer 和 Terraform 代碼以在 aws 上創建一個不可變的基礎設施。 但是,在磁盤上安裝 ext4 並掛載它似乎不是很簡單。
步驟看起來很簡單:
最佳實踐是使用您將使用它的相同實例類型創建 ami 還是擁有一個“通用”映像並從中啟動多個實例類型? 什么哲學是最好的?
后者看起來越來越好,但每個實例類型都需要一個 ami。
根據這個答案,最好使用 user_data 工作方式而不是供應商方式。 所以我要走那條路。
這個答案似乎很有希望,但太舊了,不再有效。 我可以更新它,但可能有不同的更好的方法。
這個答案似乎也很有希望,但抱怨的是 ${DEVICE}。 我想知道該變量來自哪里,因為 template_file 中沒有指定變量。 如果我將自己的 DEVICE 變量設置為 xvdb,則它會運行,但不會產生結果,因為 xvdb 在 lsblk 中可見但在 blkid 中不可見。
這是我的代碼。 format_disks.sh 文件與上述文件相同。 非常感謝任何幫助。
# Create a new instance of the latest Ubuntu 16.04 on an
# t2.micro node with an AWS Tag naming it "test1"
provider "aws" {
region = "us-east-1"
}
data "template_file" "format-disks" {
template = "${file("format_disk.sh")}"
vars {
DEVICE = "xvdb"
}
}
resource "aws_instance" "test1" {
ami = "ami-98181234"
instance_type = "r3.4xlarge"
key_name = "keypair-1" # This needs to be changed so multiple users can use this
subnet_id = "subnet-a0aeb123" # maps to the vpc for the us production
associate_public_ip_address = "true"
vpc_security_group_ids = ["sg-f3e91234"] #backendservers
user_data = "${data.template_file.format-disks.rendered}"
tags {
Name = "test1"
}
ephemeral_block_device {
device_name = "xvdb"
virtual_name = "ephemeral0"
}
}
讓我談談我對這個話題的看法。
我認為 cloud-init 是 AWS 的關鍵,因為您可以動態創建您想要的機器。 首先,嘗試更改一些全局腳本,將在您的機器啟動時使用。 然后,您應該將該腳本添加為用戶數據 我建議您同時使用 ec2 自動縮放,因此,如果您更改 cloud-init 腳本,您可能會終止該實例,另一個將自動創建。
我的結構目錄。
.
|____main.tf
|____templates
| |____cloud-init.tpl
主文件
provider "aws" {
region = "us-east-1"
}
data "template_file" "cloud_init" {
template = file("${path.module}/templates/cloud-init.tpl")
}
data "aws_ami" "linux_ami" {
most_recent = "true"
owners = ["amazon"]
filter {
name = "name"
values = ["amzn2-ami-hvm-2.0.????????.?-x86_64-gp2"]
}
}
resource "aws_instance" "test1" {
ami = data.aws_ami.linux_ami.image_id
instance_type = "r3.4xlarge"
key_name = "keypair-1"
subnet_id = "subnet-xxxxxx"
associate_public_ip_address = true
vpc_security_group_ids = ["sg-xxxxxxx"]
user_data = data.template_file.cloud_init.rendered
root_block_device {
delete_on_termination = true
encrypted = true
volume_size = 10
volume_type = "gp2"
}
ebs_block_device {
device_name = "ebs-block-device-name"
delete_on_termination = true
encrypted = true
volume_size = 10
volume_type = "gp2"
}
network_interface {
device_index = 0
network_interface_id = var.network_interface_id
delete_on_termination = true
}
tags = {
Name = "test1"
costCenter = "xxxxx"
owner = "xxxxx"
}
}
模板/雲init.tpl
#!/bin/bash -x
yum update -y
yum install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm
systemctl enable amazon-ssm-agent
systemctl start amazon-ssm-agent
pip install aws-ssm-tunnel-agent
echo "[INFO] SSM agent has been installed!"
# More scripts here.
您想要附加一個臨時磁盤嗎? 您是否嘗試使用delete_on_termination
添加一個root_block_device
true
delete_on_termination
? 這樣在銷毀aws ec2實例資源后,磁盤將被刪除。 這是在 AWS 上節省成本的好方法,但要小心,如果存儲的數據不重要或已備份,請使用它。
如果您需要在這種情況下連接外接硬盤EBS,您可以使用AWS API,請確保您有在同一機器AZ
磁盤,你可以使用它。
如果您需要一些 bash 腳本,請告訴我,但這很簡單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.