我有一个在 ECS Fargate
上运行的 API,它接受 GET
方法请求。我将 API Gateway
端点放在它前面,并在私有(private)子网中将 VPC_LINK
与 NLB
集成。当我使用调用 url 发送 GET
请求时,我收到 404 页面未找到
错误。我很困惑为什么我得到这个。我在 8000/tcp
上设置了每个组件 - NLB 监听器、目标组以及我的任务定义中的主机和容器端口。所以,我不确定为什么会发生此错误。我的 Fargate
任务也成功运行并通过了所有健康检查。当我在本地执行 curl -X GET localhost/nmapscan:8000
测试容器时,它工作正常。以下是我在 Terraform
中的配置以及来自控制台的屏幕截图。
地形:
resource "aws_lb" "myapis" {
name = "my-apis"
internal = true
load_balancer_type = "network"
subnets = ["${module.vpc.private_subnets}"]
enable_deletion_protection = false
tags = {
Environment = "dev"
}
}
resource "aws_security_group" "ecs_tasks" {
name = "ecs-tasks"
description = "allow inbound access from the NLB only"
vpc_id = "${module.vpc.vpc_id}"
ingress {
protocol = "tcp"
from_port = 8000
to_port = 8000
cidr_blocks = ["10.10.11.0/24", "10.10.12.0/24", "10.10.13.0/24"]
}
egress {
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_lb_target_group" "test" {
name = "test-api"
port = 8000
protocol = "TCP"
target_type = "ip"
vpc_id = "${module.vpc.vpc_id}"
stickiness{
enabled = false
type = "lb_cookie"
}
health_check{
interval = 30
port = 8000
protocol = "tcp"
healthy_threshold = 2
unhealthy_threshold = 2
}
}
resource "aws_lb_listener" "test" {
load_balancer_arn = "${aws_lb.myapis.id}"
port = "8000"
protocol = "TCP"
default_action {
target_group_arn = "${aws_lb_target_group.test.id}"
type = "forward"
}
}
resource "aws_api_gateway_vpc_link" "myapi" {
name = "my_api_link"
description = "VPC link for API NLB"
target_arns = ["${aws_lb.myapis.arn}"]
}
resource "aws_api_gateway_rest_api" "GOAPI" {
name = "GO"
description = "REST API for GO APIs"
}
resource "aws_api_gateway_resource" "test" {
rest_api_id = "${aws_api_gateway_rest_api.GOAPI.id}"
parent_id = "${aws_api_gateway_rest_api.GOAPI.root_resource_id}"
path_part = "nmapscan"
}
resource "aws_api_gateway_method" "testmethod" {
rest_api_id = "${aws_api_gateway_rest_api.GOAPI.id}"
resource_id = "${aws_api_gateway_resource.test.id}"
http_method = "GET"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "integrationtest" {
connection_type = "VPC_LINK"
connection_id = "${aws_api_gateway_vpc_link.myapi.id}"
type = "HTTP"
integration_http_method = "GET"
rest_api_id = "${aws_api_gateway_rest_api.GOAPI.id}"
resource_id = "${aws_api_gateway_resource.test.id}"
http_method = "${aws_api_gateway_method.testmethod.http_method}"
uri = "${format("https://%s:8000/", aws_lb.myapis.dns_name)}"
}
resource "aws_api_gateway_method_response" "test-200" {
rest_api_id = "${aws_api_gateway_rest_api.GOAPI.id}"
resource_id = "${aws_api_gateway_resource.test.id}"
http_method = "${aws_api_gateway_method.testmethod.http_method}"
status_code = "200"
response_models = {
"application/json" = "Empty"
}
}
resource "aws_api_gateway_integration_response" "testintegrationresponse" {
rest_api_id = "${aws_api_gateway_rest_api.GOAPI.id}"
resource_id = "${aws_api_gateway_resource.test.id}"
http_method = "${aws_api_gateway_method.testmethod.http_method}"
status_code = "${aws_api_gateway_method_response.test-200.status_code}"
response_templates = {
"application/json" = ""
}
}
resource "aws_api_gateway_deployment" "testdeploy" {
depends_on = ["aws_api_gateway_integration.integrationtest"]
rest_api_id = "${aws_api_gateway_rest_api.GOAPI.id}"
stage_name = "v1"
}
resource "aws_ecs_cluster" "goapi" {
name = "goapis"
}
data "aws_iam_role" "ecs_task_execution_role" {
name = "ecsTaskExecutionRole"
}
resource "aws_ecs_task_definition" "test" {
family = "test"
requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"
cpu = 256
memory = 512
execution_role_arn = "${data.aws_iam_role.ecs_task_execution_role.arn}"
container_definitions = "${file("test-service.json")}"
}
resource "aws_ecs_service" "test" {
name = "test-service"
cluster = "${aws_ecs_cluster.goapi.id}"
task_definition = "${aws_ecs_task_definition.test.arn}"
launch_type = "FARGATE"
desired_count = 1
network_configuration {
subnets = ["${module.vpc.private_subnets}"]
security_groups = ["${aws_security_group.ecs_tasks.id}"]
}
load_balancer {
target_group_arn = "${aws_lb_target_group.test.id}"
container_name = "test-service"
container_port = 8000
}
depends_on = [
"aws_lb_listener.test",
]
}
任务定义:
[
{
"name": "test-service",
"image": "12345678910.dkr.ecr.us-east-1.amazonaws.com/myimages:latest",
"cpu": 256,
"memory": 512,
"essential": true,
"portMappings": [
{
"containerPort": 8000,
"hostPort": 8000
}
]
}
]
截图:
最佳答案
编辑 看起来答案来自 here ,可能值得弃用这篇文章。
除非您在方法请求上使用 IAM 身份验证成功对您的请求进行 SIGV4 签名,或者在身份验证失败的情况下映射了自定义网关响应,否则 API 网关不会返回 404,NLB 也不会返回,因此此响应必须到来来自此堆栈中的 NLB 之外。您应该能够通过查看您的 API Gateway CloudWatch logs 来确认这一点(在舞台上启用完整的请求/响应数据)。
注意:您应该将 CloudWatch 日志中的请求 ID 与 API GW 响应 header “x-amzn-requestid”相匹配,例如:
x-amzn-requestid: 7cc765bb-4086-45f4-9450-1a2a3c4e67d5
在日志中,您应该会看到它说类似“将请求发送到...”或关于从该请求返回的响应的行。这表明 API 网关已收到请求,将其发送到后端集成并收到 404 返回。
我想说接下来的步骤是在您的集成上实现调试日志记录以记录传入的请求以及这些请求的属性,以便您可以确定故障所在。该请求似乎是针对您的应用程序未配置为响应的 URI。
关于amazon-web-services - 调用端点 url 时找不到 AWS API Gateway 404 页面错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58089140/
相关文章:
c# - 配置生成器 - 无法加载程序集 - .Net Framework
visual-studio-code - 如何使用 Visual Studio Code + VSC
java - 尝试通过运行 `mvn test` 来运行测试时 JVM 崩溃
reactjs - 如何使用带有反应最终形式的自定义 radio 组件?
php - 未定义的方法 Laravel\Lumen\Application::booted()
azure-sql-database - 目前无法检索该地区的定价配置数据。请重试