|
1 | 1 | ## AWS::APIGatewayv2 Construct Library |
| 2 | + |
2 | 3 | <!--BEGIN STABILITY BANNER--> |
3 | 4 | --- |
4 | 5 |
|
5 | 6 |  |
6 | 7 |
|
7 | 8 | > All classes with the `Cfn` prefix in this module ([CFN Resources](https://docs.aws.amazon.com/cdk/latest/guide/constructs.html#constructs_lib)) are always stable and safe to use. |
8 | 9 |
|
| 10 | + |
| 11 | + |
| 12 | +> The APIs of higher level constructs in this module are experimental and under active development. They are subject to non-backward compatible changes or removal in any future version. These are not subject to the [Semantic Versioning](https://semver.org/) model and breaking changes will be announced in the release notes. This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package. |
| 13 | +
|
9 | 14 | --- |
10 | 15 | <!--END STABILITY BANNER--> |
11 | 16 |
|
12 | | -This module is part of the [AWS Cloud Development Kit](https://github.com/aws/aws-cdk) project. |
| 17 | +## Table of Contents |
| 18 | + |
| 19 | +- [Introduction](#introduction) |
| 20 | +- [HTTP API](#http-api) |
| 21 | + - [Defining HTTP APIs](#defining-http-apis) |
| 22 | + - [Publishing HTTP APIs](#publishing-http-apis) |
| 23 | + |
| 24 | +## Introduction |
| 25 | + |
| 26 | +Amazon API Gateway is an AWS service for creating, publishing, maintaining, monitoring, and securing REST, HTTP, and WebSocket |
| 27 | +APIs at any scale. API developers can create APIs that access AWS or other web services, as well as data stored in the AWS Cloud. |
| 28 | +As an API Gateway API developer, you can create APIs for use in your own client applications. Read the |
| 29 | +[Amazon API Gateway Developer Guide](https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html). |
| 30 | + |
| 31 | +This module supports features under [API Gateway v2](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/AWS_ApiGatewayV2.html) |
| 32 | +that lets users set up Websocket and HTTP APIs. |
| 33 | +REST APIs can be created using the `@aws-cdk/aws-apigateway` module. |
| 34 | + |
| 35 | +## HTTP API |
| 36 | + |
| 37 | +HTTP APIs enable creation of RESTful APIs that integrate with AWS Lambda functions, known as Lambda proxy integration, |
| 38 | +or to any routable HTTP endpoint, known as HTTP proxy integration. |
| 39 | + |
| 40 | +### Defining HTTP APIs |
| 41 | + |
| 42 | +HTTP APIs have two fundamental concepts - Routes and Integrations. |
| 43 | + |
| 44 | +Routes direct incoming API requests to backend resources. Routes consist of two parts: an HTTP method and a resource |
| 45 | +path, such as, `GET /books`. Learn more at [Working with |
| 46 | +routes](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-routes.html). Use the `ANY` method |
| 47 | +to match any methods for a route that are not explicitly defined. |
| 48 | + |
| 49 | +Integrations define how the HTTP API responds when a client reaches a specific Route. HTTP APIs support two types of |
| 50 | +integrations - Lambda proxy integration and HTTP proxy integration. Learn more at [Configuring |
| 51 | +integrations](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations.html). |
| 52 | + |
| 53 | +The code snippet below configures a route `GET /books` with an HTTP proxy integration and uses the `ANY` method to |
| 54 | +proxy all other HTTP method calls to `/books` to a lambda proxy. |
13 | 55 |
|
14 | 56 | ```ts |
15 | | -import * as apigatewayv2 from '@aws-cdk/aws-apigatewayv2'; |
| 57 | +const getBooksIntegration = new HttpProxyIntegration({ |
| 58 | + url: 'https://get-books-proxy.myproxy.internal', |
| 59 | +}); |
| 60 | + |
| 61 | +const booksDefaultFn = new lambda.Function(stack, 'BooksDefaultFn', { ... }); |
| 62 | +const booksDefaultIntegration = new LambdaProxyIntegration({ |
| 63 | + handler: booksDefaultFn, |
| 64 | +}); |
| 65 | + |
| 66 | +const httpApi = new HttpApi(stack, 'HttpApi'); |
| 67 | + |
| 68 | +httpApi.addRoutes({ |
| 69 | + path: '/books', |
| 70 | + methods: [ HttpMethod.GET ], |
| 71 | + integration: getBooksIntegration, |
| 72 | +}); |
| 73 | +httpApi.addRoutes({ |
| 74 | + path: '/books', |
| 75 | + methods: [ HttpMethod.ANY ], |
| 76 | + integration: booksDefaultIntegration, |
| 77 | +}); |
| 78 | +``` |
| 79 | + |
| 80 | +The `defaultIntegration` option while defining HTTP APIs lets you create a default catch-all integration that is |
| 81 | +matched when a client reaches a route that is not explicitly defined. |
| 82 | + |
| 83 | +```ts |
| 84 | +new HttpApi(stack, 'HttpProxyApi', { |
| 85 | + defaultIntegration: new HttpProxyIntegration({ |
| 86 | + url:'http://example.com', |
| 87 | + }), |
| 88 | +}); |
| 89 | +``` |
| 90 | + |
| 91 | +### Publishing HTTP APIs |
| 92 | + |
| 93 | +A Stage is a logical reference to a lifecycle state of your API (for example, `dev`, `prod`, `beta`, or `v2`). API |
| 94 | +stages are identified by their stage name. Each stage is a named reference to a deployment of the API made available for |
| 95 | +client applications to call. |
| 96 | + |
| 97 | +Use `HttpStage` to create a Stage resource for HTTP APIs. The following code sets up a Stage, whose URL is available at |
| 98 | +`https://{api_id}.execute-api.{region}.amazonaws.com/beta`. |
| 99 | + |
| 100 | +```ts |
| 101 | +new HttpStage(stack, 'Stage', { |
| 102 | + httpApi: api, |
| 103 | + stageName: 'beta', |
| 104 | +}); |
16 | 105 | ``` |
| 106 | + |
| 107 | +If you omit the `stageName` will create a `$default` stage. A `$default` stage is one that is served from the base of |
| 108 | +the API's URL - `https://{api_id}.execute-api.{region}.amazonaws.com/`. |
| 109 | + |
| 110 | +Note that, `HttpApi` will always creates a `$default` stage, unless the `createDefaultStage` property is unset. |
0 commit comments