【发布时间】:2019-03-13 14:41:41
【问题描述】:
在 Terraform 支持 AWS 中的 Storage Gateway 之前,我通过其他方式创建了三个文件网关。本质上,我使用 Terraform 来启动支持的位(iam 策略、s3 存储桶、ec2 实例、缓存卷),并使用 bash 脚本进行 cli 调用以将它们整合在一起。效果很好。
现在 Terraform 支持创建/激活文件网关(包括配置缓存卷),我重构了 Terraform 以消除 bash 脚本。
网关实例和缓存卷是使用以下 Terraform 创建的:
resource "aws_instance" "gateway" {
ami = "${var.instance_ami}"
instance_type = "${var.instance_type}"
# Refer to AWS File Gateway documentation for minimum system requirements.
ebs_optimized = true
subnet_id = "${element(data.aws_subnet_ids.subnets.ids, random_integer.priority.result)}"
ebs_block_device {
device_name = "/dev/xvdf"
volume_size = "${var.ebs_cache_volume_size}"
volume_type = "gp2"
delete_on_termination = true
}
key_name = "${var.key_name}"
vpc_security_group_ids = [
"${aws_security_group.storage_gateway.id}",
]
}
实例启动并运行后,bash 脚本中的以下 sn-p 会查找卷 ID 并将卷配置为网关缓存:
# gets the gateway_arn and uses that to lookup the volume ID
gateway_arn=$(aws storagegateway list-gateways --query "Gateways[*].{arn:GatewayARN,name:GatewayName}" --output text | grep ${gateway_name} | awk '{print $1}')
volume_id=$(aws storagegateway list-local-disks --gateway-arn ${gateway_arn} --query "Disks[*].{id:DiskId}" --output text)
echo "the volume ID is $volume_id"
# add the gateway cache
echo "adding cache to the gateway"
aws storagegateway add-cache --gateway-arn ${gateway_arn} --disk-id ${volume_id}
这个过程的最终结果是网关在线,配置了缓存卷,但是 Terraform 状态只知道实例。我随后重构了 Terraform 以包括以下内容:
resource "aws_storagegateway_gateway" "nfs_file_gateway" {
gateway_ip_address = "${aws_instance.gateway.private_ip}"
gateway_name = "${var.gateway_name}"
gateway_timezone = "${var.gateway_time_zone}"
gateway_type = "FILE_S3"
}
resource "aws_storagegateway_cache" "nfs_cache_volume" {
disk_id = "${aws_instance.gateway.ebs_block_device.volume_id}"
gateway_arn = "${aws_storagegateway_gateway.nfs_file_gateway.id}"
}
从那里,我运行以下命令来获取缓存卷的disk_id(注意我已经编辑了帐户 ID 和网关 ID:
aws storagegateway list-local-disks --gateway-arn arn:aws:storagegateway:us-east-1:[account_id]:gateway/[gateway_id] --region us-east-1
这会返回:
{
"GatewayARN": "arn:aws:storagegateway:us-east-1:[account_id]:gateway/[gateway_id]",
"Disks": [
{
"DiskId": "xen-vbd-51792",
"DiskPath": "/dev/xvdf",
"DiskNode": "/dev/sdf",
"DiskStatus": "present",
"DiskSizeInBytes": 161061273600,
"DiskAllocationType": "CACHE STORAGE"
}
]
}
然后我对 aws_storagegateway_cache 资源运行 Terraform 导入命令,以将现有资源拉入状态文件。
我运行的命令:
terraform_11.5 import module.sql_backup_file_gateway.module.storage_gateway.aws_storagegateway_cache.nfs_cache_volume arn:aws:storagegateway:us-east-1:[account_id]:gateway/[gateway_id]:xen-vbd-51792
导入成功完成。然后我运行一个 Terraform init 和一个 Terraform 计划,这表明如果我要运行一个应用程序,缓存卷将被重新创建。
计划的输出:
-/+ module.sql_backup_file_gateway.module.storage_gateway.aws_storagegateway_cache.nfs_cache_volume (new resource required)
id: "arn:aws:storagegateway:us-east-1:[account_id]:gateway/[gateway_id]:xen-vbd-51792" => <computed> (forces new resource)
disk_id: "xen-vbd-51792" => "1" (forces new resource)
gateway_arn: "arn:aws:storagegateway:us-east-1:[account_id]:gateway/[gateway_id]" => "arn:aws:storagegateway:us-east-1:[account_id]:gateway/[gateway_id]"
我可以在导入语句中提供的disk_id 没有其他值允许导入完成。如果运行后续的terraform apply,我不确定我可以进行哪些更改以避免重新创建缓存卷。
【问题讨论】:
-
您的 Terraform 代码是什么样的?如果您也可以发布状态文件(或至少是相关部分),那也很有用。
-
欢迎来到 Stack Overflow!其他用户将您的问题标记为低质量和需要改进。我重新措辞/格式化您的输入,使其更易于阅读/理解。请查看我的更改以确保它们反映您的意图。但我认为你的问题仍然无法回答。 你现在应该edit你的问题,添加缺失的细节(见minimal reproducible example)。如果您对我有其他问题或反馈,请随时给我留言。
-
@ydaetskcoR - 我已经更新了原始请求以包含更多细节
-
这类问题现在有devops.stackexchange.com
-
感谢您的更新。能否也显示
terraform state show aws_instance.gateway的输出?
标签: amazon-web-services terraform terraform-provider-aws