如何从 NodeJS 调用 AWS ALB?

发布于 2025-01-20 10:51:46 字数 1674 浏览 3 评论 0原文

如何从后端 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文