aws apigateway 使用httpapi私有集成ecs服务
迪丽瓦拉
2024-05-29 13:04:03
0

参考资料

  • https://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/http-api-private-integration.html
  • https://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/http-api-develop-integrations-private.html

在《aws apigateway 基础概念和入门示例》中我们提到apigateway支持的集成方式

在这里插入图片描述

本文介绍的私有集成是http api集成的一种,目的是通过apigateway将ecs服务暴露为api接口。具体的架构如下图所示

       您在本教程中创建的 API 的架构概述。客户端使用 API 网关 HTTP API 通过 VPC 链接访问您的 Amazon ECS 服务。

注意点

  • 私有集成,所有资源必须归同一AWS账户所有(包括负载均衡器或 AWS Cloud Map 服务、VPC 链接和 HTTP API)
  • 默认私有集成流量使用 HTTP 协议
  • 私有集成使用 VPC link来封装 API Gateway 与目标 VPC 资源之间的连接。创建link时会创建托管eni。如果 60 天内未通过 VPC link发送任何流量,其状态会变为 INACTIVE,并删除 VPC link的所有网络接口。请求恢复时再创建eni
  • 创建 VPC link后,无法更改其子网或安全组

官方提供了cloudmation模板,创建vpc基础设施和ecs服务,并将ecs任务挂载到elb上,我们可以使用现有的集群,手动完成这一步骤。

ecs任务定义相关的模板如下,使用该模板创建任务定义

AWSTemplateFormatVersion: "2010-09-09"Resources:PrivateIntegrationsTutorialServiceTaskDef914930A0:Type: "AWS::ECS::TaskDefinition"Properties:ContainerDefinitions:- Essential: trueImage: public.ecr.aws/nginx/nginx:perlLogConfiguration:LogDriver: awslogsOptions:awslogs-group: !Ref PrivateIntegrationsTutorialServiceTaskDefwebLogGroupawslogs-stream-prefix: PrivateIntegrationsTutorialServiceawslogs-region: cn-north-1Name: webPortMappings:- ContainerPort: 80Protocol: tcpCpu: "512"ExecutionRoleArn: "arn:aws-cn:iam::xxxxxxxxxxx:role/ecsTaskExecutionRole"Memory: "1024"NetworkMode: awsvpcRequiresCompatibilities:- FARGATE# TaskRoleArn: "arn:aws-cn:iam::xxxxxxxxxxx:role/ecsTaskRole"PrivateIntegrationsTutorialServiceTaskDefwebLogGroup:Type: "AWS::Logs::LogGroup"DeletionPolicy: Delete

查看堆栈任务定义创建

aaa

使用任务定义创建服务,手动挂载到alb的8097端口上,尝试访问

http://main-alb-1897344746.cn-north-1.elb.amazonaws.com.cn:8097/

创建vpclink,apigateway在子网中创建对应的网卡,作为访问vpc内ecs任务的入口
aaa

创建的网卡如下

aaa

创建http api,设置ANY方法贪婪匹配,为方法配置iam授权

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bDPhCbn1-1678034556865)(assets/image-20230305233544966.png)]

配置api私有集成

在这里插入图片描述

将集成添加到路由中

http api会自动部署,直接测试api

在这里插入图片描述

终端访问,不知为何,请求一直卡住,最后一直报错503

最后检查发现,文档中提到要将网卡创建在私有子网中。我这里创建的alb是internet-facing,由于这两个网卡不会分配公有ip,因此无法访问lb,导致503

$ awscurl --service execute-api -X GET --region cn-north-1  https://5o8m4t2r57.execute-api.cn-north-1.amazonaws.com.cn/hello
{"message":"Service Unavailable"}
requests.exceptions.HTTPError: 503 Server Error: Service Unavailable for url: https://5o8m4t2r57.execute-api.cn-north-1.amazonaws.com.cn/hello

重新创建vpc link选择带nat的私有子网试试,报错变成了500

$ awscurl --service execute-api -X GET --region cn-north-1  https://5o8m4t2r57.execute-api.cn-north-1.amazonaws.com.cn
{"message":"Internal Server Error"}
requests.exceptions.HTTPError: 500 Server Error: Internal Server Error for url: https://5o8m4t2r57.execute-api.cn-north-1.amazonaws.com.cn/

只能试试是不是alb internet-facing的问题,创建一个internal的alb,貌似还是不行。

暂时不知道怎么解决

相关内容