forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.md
309 lines (232 loc) · 11.3 KB
/
README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# AWS APIGatewayv2 Integrations
## Table of Contents
- [HTTP APIs](#http-apis)
- [Lambda Integration](#lambda)
- [HTTP Proxy Integration](#http-proxy)
- [Private Integration](#private-integration)
- [Request Parameters](#request-parameters)
- [WebSocket APIs](#websocket-apis)
- [Lambda WebSocket Integration](#lambda-websocket-integration)
- [AWS WebSocket Integration](#aws-websocket-integration)
## HTTP APIs
Integrations connect a route to backend resources. HTTP APIs support Lambda proxy, AWS service, and HTTP proxy integrations. HTTP proxy integrations are also known as private integrations.
### Lambda
Lambda integrations enable integrating an HTTP API route with a Lambda function. When a client invokes the route, the
API Gateway service forwards the request to the Lambda function and returns the function's response to the client.
The API Gateway service will invoke the Lambda function with an event payload of a specific format. The service expects
the function to respond in a specific format. The details on this format are available at [Working with AWS Lambda
proxy integrations](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html).
The following code configures a route `GET /books` with a Lambda proxy integration.
```ts
import { HttpLambdaIntegration } from 'aws-cdk-lib/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 Proxy
HTTP Proxy integrations enables connecting an HTTP API route to a publicly routable HTTP endpoint. When a client
invokes the route, the API Gateway service forwards the entire request and response between the API Gateway endpoint
and the integrating HTTP endpoint. More information can be found at [Working with HTTP proxy
integrations](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-http.html).
The following code configures a route `GET /books` with an HTTP proxy integration to an HTTP endpoint
`get-books-proxy.example.com`.
```ts
import { HttpUrlIntegration } from 'aws-cdk-lib/aws-apigatewayv2-integrations';
const booksIntegration = new HttpUrlIntegration('BooksIntegration', 'https://get-books-proxy.example.com');
const httpApi = new apigwv2.HttpApi(this, 'HttpApi');
httpApi.addRoutes({
path: '/books',
methods: [ apigwv2.HttpMethod.GET ],
integration: booksIntegration,
});
```
### StepFunctions Integration
Step Functions integrations enable integrating an HTTP API route with AWS Step Functions.
This allows the HTTP API to start state machine executions synchronously or asynchronously, or to stop executions.
When a client invokes the route configured with a Step Functions integration, the API Gateway service interacts with the specified state machine according to the integration subtype (e.g., starts a new execution, synchronously starts an execution, or stops an execution) and returns the response to the client.
The following code configures a Step Functions integrations:
```ts
import { HttpStepFunctionsIntegration } from 'aws-cdk-lib/aws-apigatewayv2-integrations';
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';
declare const stateMachine: sfn.StateMachine;
declare const httpApi: apigwv2.HttpApi;
httpApi.addRoutes({
path: '/start',
methods: [ apigwv2.HttpMethod.POST ],
integration: new HttpStepFunctionsIntegration('StartExecutionIntegration', {
stateMachine,
subtype: apigwv2.HttpIntegrationSubtype.STEPFUNCTIONS_START_EXECUTION,
}),
});
httpApi.addRoutes({
path: '/start-sync',
methods: [ apigwv2.HttpMethod.POST ],
integration: new HttpStepFunctionsIntegration('StartSyncExecutionIntegration', {
stateMachine,
subtype: apigwv2.HttpIntegrationSubtype.STEPFUNCTIONS_START_SYNC_EXECUTION,
}),
});
httpApi.addRoutes({
path: '/stop',
methods: [ apigwv2.HttpMethod.POST ],
integration: new HttpStepFunctionsIntegration('StopExecutionIntegration', {
stateMachine,
subtype: apigwv2.HttpIntegrationSubtype.STEPFUNCTIONS_STOP_EXECUTION,
// For the `STOP_EXECUTION` subtype, it is necessary to specify the `executionArn`.
parameterMapping: new apigwv2.ParameterMapping()
.custom('ExecutionArn', '$request.querystring.executionArn'),
}),
});
```
**Note**:
- The `executionArn` parameter is required for the `STOP_EXECUTION` subtype. It is necessary to specify the `executionArn` in the `parameterMapping` property of the `HttpStepFunctionsIntegration` object.
- `START_SYNC_EXECUTION` subtype is only supported for EXPRESS type state machine.
### Private Integration
Private integrations enable integrating an HTTP API route with private resources in a VPC, such as Application Load Balancers or
Amazon ECS container-based applications. Using private integrations, resources in a VPC can be exposed for access by
clients outside of the VPC.
The following integrations are supported for private resources in a VPC.
#### Application Load Balancer
The following code is a basic application load balancer private integration of HTTP API:
```ts
import { HttpAlbIntegration } from 'aws-cdk-lib/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),
});
```
When an imported load balancer is used, the `vpc` option must be specified for `HttpAlbIntegration`.
#### Network Load Balancer
The following code is a basic network load balancer private integration of HTTP API:
```ts
import { HttpNlbIntegration } from 'aws-cdk-lib/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),
});
```
When an imported load balancer is used, the `vpc` option must be specified for `HttpNlbIntegration`.
#### Cloud Map Service Discovery
The following code is a basic discovery service private integration of HTTP API:
```ts
import * as servicediscovery from 'aws-cdk-lib/aws-servicediscovery';
import { HttpServiceDiscoveryIntegration } from 'aws-cdk-lib/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,
}),
});
```
### Request Parameters
Request parameter mapping allows API requests from clients to be modified before they reach backend integrations.
Parameter mapping can be used to specify modifications to request parameters. See [Transforming API requests and
responses](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-parameter-mapping.html).
The following example creates a new header - `header2` - as a copy of `header1` and removes `header1`.
```ts
import { HttpAlbIntegration } from 'aws-cdk-lib/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'),
}),
});
```
To add mapping keys and values not yet supported by the CDK, use the `custom()` method:
```ts
import { HttpAlbIntegration } from 'aws-cdk-lib/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'),
}),
});
```
## WebSocket APIs
WebSocket integrations connect a route to backend resources. The following integrations are supported in the CDK.
### Lambda WebSocket Integration
Lambda integrations enable integrating a WebSocket API route with a Lambda function. When a client connects/disconnects
or sends a message specific to a route, the API Gateway service forwards the request to the Lambda function
The API Gateway service will invoke the Lambda function with an event payload of a specific format.
The following code configures a `sendMessage` route with a Lambda integration
```ts
import { WebSocketLambdaIntegration } from 'aws-cdk-lib/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),
});
```
### AWS WebSocket Integration
AWS type integrations enable integrating with any supported AWS service. This is only supported for WebSocket APIs. When a client
connects/disconnects or sends a message specific to a route, the API Gateway service forwards the request to the specified AWS service.
The following code configures a `$connect` route with a AWS integration that integrates with a dynamodb table. On websocket api connect,
it will write new entry to the dynamodb table.
```ts
import { WebSocketAwsIntegration } from 'aws-cdk-lib/aws-apigatewayv2-integrations';
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
import * as iam from 'aws-cdk-lib/aws-iam';
const webSocketApi = new apigwv2.WebSocketApi(this, 'mywsapi');
new apigwv2.WebSocketStage(this, 'mystage', {
webSocketApi,
stageName: 'dev',
autoDeploy: true,
});
declare const apiRole: iam.Role;
declare const table: dynamodb.Table;
webSocketApi.addRoute('$connect', {
integration: new WebSocketAwsIntegration('DynamodbPutItem', {
integrationUri: `arn:aws:apigateway:${this.region}:dynamodb:action/PutItem`,
integrationMethod: apigwv2.HttpMethod.POST,
credentialsRole: apiRole,
requestTemplates: {
'application/json': JSON.stringify({
TableName: table.tableName,
Item: {
id: {
S: '$context.requestId',
},
},
}),
},
}),
});
```
You can also set additional properties to change the behavior of your integration, such as `contentHandling`.
See [Working with binary media types for WebSocket APIs](https://docs.aws.amazon.com/apigateway/latest/developerguide/websocket-api-develop-binary-media-types.html).