Amazon web services 如何使用Terraform格式化和装载临时磁盘?
我正在编写打包器和地形代码,以便在aws上创建一个不变的infra。然而,在磁盘上安装ext4并装载它似乎不是很简单 步骤似乎很简单:Amazon web services 如何使用Terraform格式化和装载临时磁盘?,amazon-web-services,amazon-ec2,terraform,ec2-ami,Amazon Web Services,Amazon Ec2,Terraform,Ec2 Ami,我正在编写打包器和地形代码,以便在aws上创建一个不变的infra。然而,在磁盘上安装ext4并装载它似乎不是很简单 步骤似乎很简单: 在t2.micro上使用packer创建ami,其中包含所有软件,首先用于测试,然后用于生产 从这个具有300GB临时磁盘的ami启动一个r3.4XL大型实例。将该磁盘格式化为ext4,装载它并将/var/lib/docker重定向到新的文件系统,以提高性能 完成应用程序启动的其余部分 首先: 最好的做法是创建与您将使用它的实例类型相同的ami,还是创建一个
- 在t2.micro上使用packer创建ami,其中包含所有软件,首先用于测试,然后用于生产
- 从这个具有300GB临时磁盘的ami启动一个r3.4XL大型实例。将该磁盘格式化为ext4,装载它并将/var/lib/docker重定向到新的文件系统,以提高性能
- 完成应用程序启动的其余部分
- 打包机(软件版本)->terraform(实例+装载磁盘)->部署
- packer(软件版本)->packer(实例类型特定安装)->terraform(实例)->部署
- 打包机(软件版本、实例特定装载)->terraform->deploy
# 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
main.tf
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"
}
}
模板/cloud 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.
是否要连接临时磁盘?您是否尝试添加一个根\u块\u设备
,并将true
作为值添加到delete\u终端
?这样,在销毁aws ec2实例资源后,磁盘将被删除。这是在AWS上节省成本的一个好方法,但是要小心,如果存储在AWS上的数据不重要或者您已经备份了,请使用它
如果您需要在此实例上连接外部ebs磁盘,可以使用AWS API,确保您的机器与您可以使用的磁盘位于相同的AZ
如果您需要一些bash脚本,请告诉我,但这很简单。在这种情况下,我会将
format\u disk.sh
脚本与packer集成到您的AMI中,并进行一些配置,以使其在引导时运行,并将其从Terraform手中完全移除。Terraform将只安排连接磁盘,AMI中的软件将负责将其格式化,作为启动的一部分。通过这种方式,您还可以更轻松地将该操作与任何其他应用程序启动任务进行协调,以确保需要文件系统的应用程序在文件系统格式化和装入后才能启动。这确实很有帮助,谢谢@MartinAtkins。我目前正在做一个变通方法,在安装/配置完所有内容后启动实例,然后运行两个ansible角色来格式化/映射和移动需要在本地磁盘上运行的应用程序。不幸的是,这意味着Terraform无法对所有实例进行滚动更新。我会看看是否能让你的建议发挥作用。@Evert你能不能在这里分享这个角色,这样我就可以重复使用我有类似的要求。Hello@SantoshGarole很遗憾,我不再在同一家公司工作,所以无法将这些角色发送给你。不过,它们非常基本,我相信你可以在其他地方找到类似的例子。祝你好运