AWS APIGatewayv2 集成
AWS CDK v1在2023-06-01上达到了支持结束. 此软件包不再更新,用户应该迁移到 AWS CDK v2.
关于如何移徙的更多信息,请参见: 移动到 AWS CDK v2 指南。 。 。 .
目录
HTTP API 软件
整合连接到后端资源的路由. HTTP APIs支持Lambda代理,AWS服务,以及HTTP代理集成. HTTP代理集成也被称为私人集成.
兰布达
Lambda集成可以将HTTP API路由与Lambda功能相融合. 当客户援引路线时 API Gateway服务将请求转发到Lambda函数,并返回该函数对客户端的响应.
API Gateway服务将引用有特定格式的事件有效载荷的lambda函数。 服务预期 以特定格式响应的函数。 有关该格式的详情,请访问 与AWS Lambda合作 代理集成。 。 。 .
以下代码配置一个路由 GET /books 一个Lambda代理集成.
import { HttpLambdaIntegration } from '@aws-cdk/aws-apigatewayv2-integrations';
declare const booksDefaultFn: lambda.Function;
const booksIntegration = new HttpLambdaIntegration('BooksIntegration', booksDefaultFn);
const httpApi = new apigwv2.HttpApi(this, 'HttpApi');
httpApi.addRoutes({
path: '/books',
methods: [ apigwv2.HttpMethod.GET ],
integration: booksIntegration,
});
HTTP 代理服务器
HTTP代理集成使得一个HTTP API路由能够连接到一个公开的routable HTTP端点. 当客户端 引用路由, API Gateway 服务在 API Gateway 端口之间转发整个请求和响应 和整合 HTTP 端点。 更多信息可在 使用 HTTP 代理服务器 整合。 。 。 .
以下代码配置一个路由 GET /books 将 HTTP 代理集成到 HTTP 端点
get-books-proxy.myproxy.internal。 。 。 .
import { HttpUrlIntegration } from '@aws-cdk/aws-apigatewayv2-integrations';
const booksIntegration = new HttpUrlIntegration('BooksIntegration', 'https://get-books-proxy.myproxy.internal');
const httpApi = new apigwv2.HttpApi(this, 'HttpApi');
httpApi.addRoutes({
path: '/books',
methods: [ apigwv2.HttpMethod.GET ],
integration: booksIntegration,
});
私营一体化
私有集成可以将 HTTP API 路由与私有资源整合到 VPC 中,例如应用程序负载平衡器或 亚马逊ECS容器应用. 利用私人集成,VPC中的资源可以被曝出,供 在VPC以外的客户.
支持将以下整合纳入一项自愿参与方案,用于私人资源.
应用程序负载平衡器
以下代码是HTTP API的基本应用程序负载平衡器的私人集成:
import { HttpAlbIntegration } from '@aws-cdk/aws-apigatewayv2-integrations';
const vpc = new ec2.Vpc(this, 'VPC');
const lb = new elbv2.ApplicationLoadBalancer(this, 'lb', { vpc });
const listener = lb.addListener('listener', { port: 80 });
listener.addTargets('target', {
port: 80,
});
const httpEndpoint = new apigwv2.HttpApi(this, 'HttpProxyPrivateApi', {
defaultIntegration: new HttpAlbIntegration('DefaultIntegration', listener),
});
当使用导入的负载平衡器时 vpc 选项必须指定 HttpAlbIntegration。 。 。 .
网络负载平衡器
以下代码是 HTTP API 的基本网络负载平衡器私人集成:
import { HttpNlbIntegration } from '@aws-cdk/aws-apigatewayv2-integrations';
const vpc = new ec2.Vpc(this, 'VPC');
const lb = new elbv2.NetworkLoadBalancer(this, 'lb', { vpc });
const listener = lb.addListener('listener', { port: 80 });
listener.addTargets('target', {
port: 80,
});
const httpEndpoint = new apigwv2.HttpApi(this, 'HttpProxyPrivateApi', {
defaultIntegration: new HttpNlbIntegration('DefaultIntegration', listener),
});
当使用导入的负载平衡器时 vpc 选项必须指定 HttpNlbIntegration。 。 。 .
云图服务发现
以下代码是 HTTP API 的基本发现服务私人集成:
import * as servicediscovery from '@aws-cdk/aws-servicediscovery';
import { HttpServiceDiscoveryIntegration } from '@aws-cdk/aws-apigatewayv2-integrations';
const vpc = new ec2.Vpc(this, 'VPC');
const vpcLink = new apigwv2.VpcLink(this, 'VpcLink', { vpc });
const namespace = new servicediscovery.PrivateDnsNamespace(this, 'Namespace', {
name: 'boobar.com',
vpc,
});
const service = namespace.createService('Service');
const httpEndpoint = new apigwv2.HttpApi(this, 'HttpProxyPrivateApi', {
defaultIntegration: new HttpServiceDiscoveryIntegration('DefaultIntegration', service, {
vpcLink,
}),
});
请求参数
请求参数映射允许客户端的API请求在达到后端集成之前被修改. 参数映射可用于指定对请求参数的修改. 见 转换 API 请求和 答复。 。 。 .
以下示例创建了新标题 - header2 - 作为副本 header1 并删除 header1。 。 。 .
import { HttpAlbIntegration } from '@aws-cdk/aws-apigatewayv2-integrations';
declare const lb: elbv2.ApplicationLoadBalancer;
const listener = lb.addListener('listener', { port: 80 });
listener.addTargets('target', {
port: 80,
});
const httpEndpoint = new apigwv2.HttpApi(this, 'HttpProxyPrivateApi', {
defaultIntegration: new HttpAlbIntegration('DefaultIntegration', listener, {
parameterMapping: new apigwv2.ParameterMapping()
.appendHeader('header2', apigwv2.MappingValue.requestHeader('header1'))
.removeHeader('header1'),
}),
});
要添加 CDK 尚未支持的映射密钥和值,请使用 custom() 方法 :
import { HttpAlbIntegration } from '@aws-cdk/aws-apigatewayv2-integrations';
declare const lb: elbv2.ApplicationLoadBalancer;
const listener = lb.addListener('listener', { port: 80 });
listener.addTargets('target', {
port: 80,
});
const httpEndpoint = new apigwv2.HttpApi(this, 'HttpProxyPrivateApi', {
defaultIntegration: new HttpAlbIntegration('DefaultIntegration', listener, {
parameterMapping: new apigwv2.ParameterMapping().custom('myKey', 'myValue'),
}),
});
网络锁定 APIs
WebSocket集成将一条路由连接到后端资源. CDK支持以下整合.
Lambda WebSocket 集成系统
Lambda集成使WebSocket API路由与Lambda功能相融合成为可能. 当客户端连接/ 断开连接时 或发送特定路由的信息,API Gateway 服务将把请求转发到 Lambda 函数
API Gateway服务将引用有特定格式的事件有效载荷的lambda函数.
以下代码配置 a sendmessage 与Lambda集成的路线
import { WebSocketLambdaIntegration } from '@aws-cdk/aws-apigatewayv2-integrations';
const webSocketApi = new apigwv2.WebSocketApi(this, 'mywsapi');
new apigwv2.WebSocketStage(this, 'mystage', {
webSocketApi,
stageName: 'dev',
autoDeploy: true,
});
declare const messageHandler: lambda.Function;
webSocketApi.addRoute('sendmessage', {
integration: new WebSocketLambdaIntegration('SendMessageIntegration', messageHandler),
});