流程就像
1。vpc-->vpc_endpoint(com.amazonaws.us-east-1.transfer.server) --> [subnet_1, subnet_2]
2。net --> nlb --> targetgroups --> [subnet_ip_1, subnet_ip_2]
我正在创建一个NLB,目标组指向为“用于sftp的AWS传输” com.amazonaws.us-east-1.transfer.server
创建的VPC端点,但是terraform不会返回与VPC端点集成的子网的ips
因此,当前,我正在从vpc端点下的“子网”选项卡中手动复制ips。 但是,我想使用terraform
自动化此完整过程任何帮助将不胜感激
resource "aws_eip" "nlb" {
count = length(var.public_subnet_ids)
vpc = true
}
resource "aws_lb" "network" {
name = "${var.service_name}-${var.env}-nlb"
load_balancer_type = "network"
dynamic subnet_mapping {
for_each = [for i in range(length(module.vpc.public_subnet_ids)) : {
subnet_id = var.public_subnet_ids[i]
allocation_id = aws_eip.nlb[i].id
}]
content {
subnet_id = subnet_mapping.value.subnet_id
allocation_id = subnet_mapping.value.allocation_id
}
}
}
resource "aws_lb_target_group" "target-group" {
name = "${var.service_name}-${var.env}-nlb-target-group"
port = 22
protocol = "TCP"
target_type = "ip"
vpc_id = var.vpc_id
}
// TODO need to add vpc endpoint subnet ip addresses manually to nlb target group as terraform doesn't export the subnet ip addresses
//resource "aws_lb_target_group_attachment" "vpc-endpoint" {
// count = length(var.public_subnet_ids)
// target_group_arn = aws_lb_target_group.target-group.arn
// target_id = this needs ip of subnets intgerated with vpc endpoint
// port = 22
//}
resource "aws_vpc_endpoint" "transfer" {
vpc_id = var.vpc_id
service_name = "com.amazonaws.${var.aws_region}.transfer.server"
vpc_endpoint_type = "Interface"
subnet_ids = var.public_subnet_ids
private_dns_enabled = true
}
resource "aws_transfer_server" "sftp" {
identity_provider_type = "API_GATEWAY"
endpoint_type = "VPC_ENDPOINT"
endpoint_details {
vpc_endpoint_id = aws_vpc_endpoint.transfer.id
}
url = aws_api_gateway_deployment.deploy.invoke_url
invocation_role = aws_iam_role.transfer-identity-provider-role.arn
logging_role = aws_iam_role.transfer-logging-role.arn
depends_on = [aws_vpc_endpoint.transfer]
}
答案 0 :(得分:0)
在the docs中,似乎VPC端点具有主机名而不是静态IP地址,这通常意味着主机名可能引用多个IP地址或随时间而变化。因此,您可能无法实现此处的操作,或者至少随着时间的推移,随着情况的变化,该功能可能会变得不可靠。
话虽如此,只要使用dns
提供程序,您就可以在Terraform运行时获取与这些主机名相对应的IP地址。 :
resource "aws_vpc_endpoint" "transfer" {
vpc_id = var.vpc_id
service_name = "com.amazonaws.${var.aws_region}.transfer.server"
vpc_endpoint_type = "Interface"
subnet_ids = var.public_subnet_ids
private_dns_enabled = true
}
data "dns_a_record_set" "endpoints" {
# dns_a_record_set.endpoints is a map from hostname to an object containing "addrs"
for_each = { for e in aws_vpc_endpoint.transfer.dns_entry : e.dns_name => e }
host = each.key
}
locals {
# Flatten the IP addresses down into a single set.
endpoint_ip_addrs = toset(flatten(dns_a_record_set.endpoints[*].addrs))
}
resource "aws_lb_target_group_attachment" "vpc-endpoint" {
for_each = local.endpoint_ip_addrs
target_group_arn = aws_lb_target_group.target-group.arn
target_id = each.value
port = 22
}
不幸的是,我希望aws_vpc_endpoint
的{{1}}属性中的主机名仅在应用后才知道,因此它们不是用于dns_entry
的好键。如果是这样,则以上内容将产生错误,指出除非您使用for_each
逐步应用for_each
值,否则该值不合适。
-target
的设计似乎不太适合您在此处尝试做的事情,因为它没有提供任何明确的指示,表明哪些主机名属于哪个子网,因此没有足够的信息可用于构建具有配置中已知键的映射或设置值。可能值得考虑从等式中完全删除负载均衡器的替代方法,而仅使用aws_vpc_endpoint
提供的DNS主机名,因为考虑到资源类型的设计,这似乎是预期的用法。
答案 1 :(得分:0)
## Data Section
data "aws_network_interface" "eni_0" {
id = "${aws_vpc_endpoint.transfer.network_interface.ids {0}"
}
data "aws_network_interface" "eni_1" {
id = "${aws_vpc_endpoint.transfer.network_interface.ids {1}"
}
## Resource Section
resource "aws_alb_target_group_attachment" "tg_att_0" {
target_group_arn = "$aws_lb_target_group.group.arn}"
target_id = "${data.aws_network_interface.eni_0.private_ips[0]}"
port = 22
}
resource "aws_alb_target_group_attachment" "tg_att_1" {
target_group_arn = "$aws_lb_target_group.group.arn}"
target_id = "${data.aws_network_interface.eni_1.private_ips[0]}"
port = 22
}
这确实有效,但是还没有时间优化代码... 它将允许您将NLB附加到VPC端点内部地址。
祝你好运。