Eve*_*ert 5 amazon-ec2 amazon-web-services ec2-ami 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"
}
}
Run Code Online (Sandbox Code Playgroud)
让我谈谈我对这个话题的看法。
我认为cloud-init是AWS的关键,因为你可以动态创建你想要的机器。首先,尝试更改一些全局脚本,这些脚本将在您的计算机启动时使用。然后,您应该将该脚本添加为用户数据。我建议您同时使用 ec2 自动缩放,因此,如果您更改 cloud-init 脚本,您可能会终止该实例,将自动创建另一个实例。
我的结构目录。
.
|____main.tf
|____templates
| |____cloud-init.tpl
Run Code Online (Sandbox Code Playgroud)
主.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"
}
}
Run Code Online (Sandbox Code Playgroud)
模板/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.
Run Code Online (Sandbox Code Playgroud)
您想要附加一个临时磁盘吗?您是否尝试过添加root_block_device
带有delete_on_termination
atrue
作为值的 a ?这样在销毁aws ec2实例资源后,磁盘将被删除。这是节省 AWS 成本的好方法,但要小心,如果存储的数据不重要或者您已经备份,请使用它。
如果您需要在此实例上附加外部 ebs 磁盘,您可以使用 AWS API,确保您的计算机与AZ
可以使用它的磁盘相同。
如果您需要一些 bash 脚本,请告诉我,但这很简单。
归档时间: |
|
查看次数: |
2719 次 |
最近记录: |