我如何制作一个定义aws_network_interface
资源的Terraform模块来有选择地分配静态私有IP?
在模块中,资源定义为
# Use this block if private_ips have been provided
resource "aws_network_interface" "management_interface" {
count = "${var.count}"
subnet_id = "${var.management_net}"
# Workaround, see https://github.com/hashicorp/terraform/issues/12472
private_ips = [ "${compact(list(element(split(",",var.private_ips), count.index)))}" ]
tags {
Name = "if-management"
}
}
private_ips
是一个空字符串(如果请求动态私有IP地址),或者是一个逗号分隔的列表(每个实例一个元素)(如果应分配一个静态私有IP)。
像这样调用模块
module "dns" {
count = 2
source = "./modules/linux-system"
[…]
private_ips = "${cidrhost(var.management_subnet_cidr, 12)},${cidrhost(var.management_subnet_cidr, 13)}"
[…]
}
或
module "jumphost" {
source = "./modules/linux-system"
[…]
}
此解决方案似乎不是幂等的:虽然第一个terraform apply
成功完成,但下一次运行会导致
* module.jumphost.aws_network_interface.management_interface: 1 error(s) occurred:
* aws_network_interface.management_interface: Failure to unassign Private IPs: InvalidParameterValue: Value (10.128.16.139) for parameter privateIpAddress is invalid. The primary IP address of an interface cannot be unassigned.
status code: 400, request id: 672b6d7d-6396-48bf-8bd0-77ce764709be
有人对如何正确做有想法吗?看来这里需要类似Ansible / Jinja2的omit
。