如何从 NodeJS 调用 AWS ALB?
如何从后端 NodeJS/Express 应用程序调用 AWS Application Load Balancer (ALB)? ALB 确实有 IP 和 DNS 名称,因此我应该能够使用其中之一。
我的解决方案的结构如下:
Public Subnet
ALB
Welcome Page (web app)
Members UX API
Admin UX API
Private Subnet
ALB
Auth Data API
Logging Data API
Ticketing Data API
Messaging Data API
所有 API 都是分布在两个可用区的自动缩放的 ECS 实例。目标是任一 UX API 都能够调用任何数据 API。
目标是在任务定义中引用上游端点。 Terraform 片段类似于:
{
"name" : "UPSTREAM_DATAAPI",
"value" : "${aws_lb.private_lb.dns_name}"
}
...或者,如果更容易查看的话,任务定义是...
resource "aws_ecs_task_definition" "memberuxapi_task" {
family = "memberuxapi_task"
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE", "EC2"]
cpu = 512
memory = 2048
execution_role_arn = aws_iam_role.crms_task_execution_role.arn
depends_on = [aws_lb.private_lb]
container_definitions = jsonencode([
{
name = "memberuxapi"
image = "123456789012.dkr.ecr.us-gov-west-1.amazonaws.com/complexapi:latest"
cpu = 512
memory = 2048
essential = true
environment = [
{
"name" : "NODE_PORT",
"value" : "80"
},
{
"name" : "NODE_ALIAS",
"value" : "MEMBER_UX_API"
},
{
"name" : "UPSTREAM_DATAAPI",
"value" : "${aws_lb.private_lb.dns_name}"
}
],
portMappings = [
{
containerPort = 80
hostPort = 80
}
]
}
])
}
How can I call an AWS Application Load Balancer (ALB) from a back-end NodeJS/Express app? The ALB does have an IP and DNS Name so I should be able to use one of those.
My solution is structured as follows:
Public Subnet
ALB
Welcome Page (web app)
Members UX API
Admin UX API
Private Subnet
ALB
Auth Data API
Logging Data API
Ticketing Data API
Messaging Data API
All of the APIs are auto-scaled ECS instances spread across two availability zones. The goal is for either of the UX APIs to be able to call any of the data APIs.
The goal is to reference the upstream endpoint in the task definition. The Terraform snippet would be something like:
{
"name" : "UPSTREAM_DATAAPI",
"value" : "${aws_lb.private_lb.dns_name}"
}
... or, if it's easier to look at, the task definition is ...
resource "aws_ecs_task_definition" "memberuxapi_task" {
family = "memberuxapi_task"
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE", "EC2"]
cpu = 512
memory = 2048
execution_role_arn = aws_iam_role.crms_task_execution_role.arn
depends_on = [aws_lb.private_lb]
container_definitions = jsonencode([
{
name = "memberuxapi"
image = "123456789012.dkr.ecr.us-gov-west-1.amazonaws.com/complexapi:latest"
cpu = 512
memory = 2048
essential = true
environment = [
{
"name" : "NODE_PORT",
"value" : "80"
},
{
"name" : "NODE_ALIAS",
"value" : "MEMBER_UX_API"
},
{
"name" : "UPSTREAM_DATAAPI",
"value" : "${aws_lb.private_lb.dns_name}"
}
],
portMappings = [
{
containerPort = 80
hostPort = 80
}
]
}
])
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论