-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat(apigatewayv2): http api #6432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
52 commits
Select commit
Hold shift + click to select a range
d310726
(feat)apigatewayv2: initial L2 support
pahud bef371a
Merge branch 'master' into apigatewayv2-L2
pahud 2ba95d8
add the expected.json for integ test
pahud 9f08ed4
fix package.json
pahud 075e564
merge from master
pahud febd6a3
udpate README
pahud 33dead4
update README
pahud 02f96c2
update README
pahud 46d3be4
remove comments and minor fix
pahud 7265479
fix package.json
pahud 35b8ddf
add integration and tests
pahud 66c5022
update README
pahud 87fbe4b
minor fix
pahud d26ad33
support HttpApi with no integration and no route
pahud 4b278c0
update tests
pahud bef8974
remove unused type
pahud 068b476
update integ test
pahud 6f8f64d
support addLambdaRoute and addHttpRoute from HttpApi
pahud 9d986d5
update README sample
pahud d338c41
Merge branch 'master' into apigatewayv2-L2
pahud 32b6a31
Merge branch 'master' into apigatewayv2-L2
pahud b200c9f
Update packages/@aws-cdk/aws-apigatewayv2/README.md
pahud 0bea7fc
Update packages/@aws-cdk/aws-apigatewayv2/README.md
pahud 822cfe8
Update packages/@aws-cdk/aws-apigatewayv2/README.md
pahud c20a448
minor fix
pahud b6415e3
Merge branch 'master' into apigatewayv2-L2
pahud 1965f59
update package.json
pahud 6547860
update integ
pahud 0989585
support api.addRootRoute()
pahud 330b4af
Merge branch 'master' of https://github.com/aws/aws-cdk into apigatew…
pahud 90204bb
support addRoutes() fro `HttpApi` and `Route` resources.
pahud 818af1d
Merge branch 'master' into apigatewayv2-L2
3568779
trying to move this forward
7b64fd0
working basic routes and lambda integration
bb8857f
Working stage
5f44349
default integration
9e4deb8
docs updated & tweaks
35d6341
the massive restructure
cce22c1
Added IHttpRoute and fixed up HttpProxyIntegration
edba63c
http proxy now works
0b404dc
move stage name back to string
d9cd335
unit and integ tests
a5e2b20
addRoutes()
48aa1af
tests for integrations
ea8bea9
Merge pull request #1 from pahud/nija-at/apigwv2-pattern-setup
pahud c18a7d0
update README
pahud 113053d
Merge branch 'master' into apigatewayv2-L2
pahud 8ded78b
Update README.md
pahud df21f0f
Update package.json
pahud b808736
language and structural adjustments to the README
7cc71e0
adjust http integration
64606f3
Merge branch 'master' into apigatewayv2-L2
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,110 @@ | ||
| ## AWS::APIGatewayv2 Construct Library | ||
|
|
||
| <!--BEGIN STABILITY BANNER--> | ||
| --- | ||
|
|
||
|  | ||
|
|
||
| > 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. | ||
|
|
||
|  | ||
|
|
||
| > 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. | ||
|
|
||
| --- | ||
| <!--END STABILITY BANNER--> | ||
|
|
||
| This module is part of the [AWS Cloud Development Kit](https://github.com/aws/aws-cdk) project. | ||
| ## Table of Contents | ||
|
|
||
| - [Introduction](#introduction) | ||
| - [HTTP API](#http-api) | ||
| - [Defining HTTP APIs](#defining-http-apis) | ||
| - [Publishing HTTP APIs](#publishing-http-apis) | ||
|
|
||
| ## Introduction | ||
|
|
||
| Amazon API Gateway is an AWS service for creating, publishing, maintaining, monitoring, and securing REST, HTTP, and WebSocket | ||
| 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. | ||
| As an API Gateway API developer, you can create APIs for use in your own client applications. Read the | ||
| [Amazon API Gateway Developer Guide](https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html). | ||
|
|
||
| This module supports features under [API Gateway v2](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/AWS_ApiGatewayV2.html) | ||
| that lets users set up Websocket and HTTP APIs. | ||
| REST APIs can be created using the `@aws-cdk/aws-apigateway` module. | ||
|
|
||
| ## HTTP API | ||
|
|
||
| HTTP APIs enable creation of RESTful APIs that integrate with AWS Lambda functions, known as Lambda proxy integration, | ||
| or to any routable HTTP endpoint, known as HTTP proxy integration. | ||
|
|
||
| ### Defining HTTP APIs | ||
|
|
||
| HTTP APIs have two fundamental concepts - Routes and Integrations. | ||
|
|
||
| Routes direct incoming API requests to backend resources. Routes consist of two parts: an HTTP method and a resource | ||
| path, such as, `GET /books`. Learn more at [Working with | ||
| routes](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-routes.html). Use the `ANY` method | ||
| to match any methods for a route that are not explicitly defined. | ||
|
|
||
| Integrations define how the HTTP API responds when a client reaches a specific Route. HTTP APIs support two types of | ||
| integrations - Lambda proxy integration and HTTP proxy integration. Learn more at [Configuring | ||
| integrations](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations.html). | ||
|
|
||
| The code snippet below configures a route `GET /books` with an HTTP proxy integration and uses the `ANY` method to | ||
| proxy all other HTTP method calls to `/books` to a lambda proxy. | ||
|
|
||
| ```ts | ||
| import * as apigatewayv2 from '@aws-cdk/aws-apigatewayv2'; | ||
| const getBooksIntegration = new HttpProxyIntegration({ | ||
| url: 'https://get-books-proxy.myproxy.internal', | ||
| }); | ||
|
|
||
| const booksDefaultFn = new lambda.Function(stack, 'BooksDefaultFn', { ... }); | ||
| const booksDefaultIntegration = new LambdaProxyIntegration({ | ||
| handler: booksDefaultFn, | ||
| }); | ||
|
|
||
| const httpApi = new HttpApi(stack, 'HttpApi'); | ||
|
|
||
| httpApi.addRoutes({ | ||
| path: '/books', | ||
| methods: [ HttpMethod.GET ], | ||
| integration: getBooksIntegration, | ||
| }); | ||
| httpApi.addRoutes({ | ||
| path: '/books', | ||
| methods: [ HttpMethod.ANY ], | ||
| integration: booksDefaultIntegration, | ||
| }); | ||
| ``` | ||
|
|
||
| The `defaultIntegration` option while defining HTTP APIs lets you create a default catch-all integration that is | ||
| matched when a client reaches a route that is not explicitly defined. | ||
|
|
||
| ```ts | ||
| new HttpApi(stack, 'HttpProxyApi', { | ||
| defaultIntegration: new HttpProxyIntegration({ | ||
| url:'http://example.com', | ||
| }), | ||
| }); | ||
| ``` | ||
|
|
||
| ### Publishing HTTP APIs | ||
|
|
||
| A Stage is a logical reference to a lifecycle state of your API (for example, `dev`, `prod`, `beta`, or `v2`). API | ||
| stages are identified by their stage name. Each stage is a named reference to a deployment of the API made available for | ||
| client applications to call. | ||
|
|
||
| Use `HttpStage` to create a Stage resource for HTTP APIs. The following code sets up a Stage, whose URL is available at | ||
| `https://{api_id}.execute-api.{region}.amazonaws.com/beta`. | ||
|
|
||
| ```ts | ||
| new HttpStage(stack, 'Stage', { | ||
| httpApi: api, | ||
| stageName: 'beta', | ||
| }); | ||
| ``` | ||
|
|
||
| If you omit the `stageName` will create a `$default` stage. A `$default` stage is one that is served from the base of | ||
| the API's URL - `https://{api_id}.execute-api.{region}.amazonaws.com/`. | ||
|
|
||
| Note that, `HttpApi` will always creates a `$default` stage, unless the `createDefaultStage` property is unset. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export * from './integration'; | ||
| export * from './route'; | ||
| export * from './stage'; |
12 changes: 12 additions & 0 deletions
12
packages/@aws-cdk/aws-apigatewayv2/lib/common/integration.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { IResource } from '@aws-cdk/core'; | ||
|
|
||
| /** | ||
| * Represents an integration to an API Route. | ||
| */ | ||
| export interface IIntegration extends IResource { | ||
| /** | ||
| * Id of the integration. | ||
| * @attribute | ||
| */ | ||
| readonly integrationId: string; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { IResource } from '@aws-cdk/core'; | ||
|
|
||
| /** | ||
| * Represents a route. | ||
| */ | ||
| export interface IRoute extends IResource { | ||
| /** | ||
| * Id of the Route | ||
| * @attribute | ||
| */ | ||
| readonly routeId: string; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { IResource } from '@aws-cdk/core'; | ||
|
|
||
| /** | ||
| * Represents a Stage. | ||
| */ | ||
| export interface IStage extends IResource { | ||
| /** | ||
| * The name of the stage; its primary identifier. | ||
| * @attribute | ||
| */ | ||
| readonly stageName: string; | ||
| } | ||
|
|
||
| /** | ||
| * Options required to create a new stage. | ||
| * Options that are common between HTTP and Websocket APIs. | ||
| */ | ||
| export interface CommonStageOptions { | ||
| /** | ||
| * The name of the stage. See `StageName` class for more details. | ||
| * @default '$default' the default stage of the API. This stage will have the URL at the root of the API endpoint. | ||
| */ | ||
| readonly stageName?: string; | ||
|
|
||
| /** | ||
| * Whether updates to an API automatically trigger a new deployment. | ||
| * @default false | ||
| */ | ||
| readonly autoDeploy?: boolean; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import { Construct, IResource, Resource } from '@aws-cdk/core'; | ||
| import { CfnApi, CfnApiProps } from '../apigatewayv2.generated'; | ||
| import { IHttpRouteIntegration } from './integration'; | ||
| import { BatchHttpRouteOptions, HttpMethod, HttpRoute, HttpRouteKey } from './route'; | ||
| import { HttpStage, HttpStageOptions } from './stage'; | ||
|
|
||
| /** | ||
| * Represents an HTTP API | ||
| */ | ||
| export interface IHttpApi extends IResource { | ||
| /** | ||
| * The identifier of this API Gateway HTTP API. | ||
| * @attribute | ||
| */ | ||
| readonly httpApiId: string; | ||
| } | ||
|
|
||
| /** | ||
| * Properties to initialize an instance of `HttpApi`. | ||
| */ | ||
| export interface HttpApiProps { | ||
| /** | ||
| * Name for the HTTP API resoruce | ||
| * @default - id of the HttpApi construct. | ||
| */ | ||
| readonly apiName?: string; | ||
|
|
||
| /** | ||
| * An integration that will be configured on the catch-all route ($default). | ||
| * @default - none | ||
| */ | ||
| readonly defaultIntegration?: IHttpRouteIntegration; | ||
|
|
||
| /** | ||
| * Whether a default stage and deployment should be automatically created. | ||
| * @default true | ||
| */ | ||
| readonly createDefaultStage?: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * Options for the Route with Integration resoruce | ||
| */ | ||
| export interface AddRoutesOptions extends BatchHttpRouteOptions { | ||
| /** | ||
| * The path at which all of these routes are configured. | ||
| */ | ||
| readonly path: string; | ||
|
|
||
| /** | ||
| * The HTTP methods to be configured | ||
| * @default HttpMethod.ANY | ||
| */ | ||
| readonly methods?: HttpMethod[]; | ||
| } | ||
|
|
||
| /** | ||
| * Create a new API Gateway HTTP API endpoint. | ||
| * @resource AWS::ApiGatewayV2::Api | ||
| */ | ||
| export class HttpApi extends Resource implements IHttpApi { | ||
| /** | ||
| * Import an existing HTTP API into this CDK app. | ||
| */ | ||
| public static fromApiId(scope: Construct, id: string, httpApiId: string): IHttpApi { | ||
| class Import extends Resource implements IHttpApi { | ||
| public readonly httpApiId = httpApiId; | ||
| } | ||
| return new Import(scope, id); | ||
| } | ||
|
|
||
| public readonly httpApiId: string; | ||
| private readonly defaultStage: HttpStage | undefined; | ||
|
|
||
| constructor(scope: Construct, id: string, props?: HttpApiProps) { | ||
| super(scope, id); | ||
|
|
||
| const apiName = props?.apiName ?? id; | ||
|
|
||
| const apiProps: CfnApiProps = { | ||
| name: apiName, | ||
| protocolType: 'HTTP', | ||
| }; | ||
| const resource = new CfnApi(this, 'Resource', apiProps); | ||
| this.httpApiId = resource.ref; | ||
|
|
||
| if (props?.defaultIntegration) { | ||
| new HttpRoute(this, 'DefaultRoute', { | ||
| httpApi: this, | ||
| routeKey: HttpRouteKey.DEFAULT, | ||
| integration: props.defaultIntegration, | ||
| }); | ||
| } | ||
|
|
||
| if (props?.createDefaultStage === undefined || props.createDefaultStage === true) { | ||
| this.defaultStage = new HttpStage(this, 'DefaultStage', { | ||
| httpApi: this, | ||
| autoDeploy: true, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get the URL to the default stage of this API. | ||
| * Returns `undefined` if `createDefaultStage` is unset. | ||
| */ | ||
| public get url(): string | undefined { | ||
| return this.defaultStage ? this.defaultStage.url : undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Add a new stage. | ||
| */ | ||
| public addStage(id: string, options: HttpStageOptions): HttpStage { | ||
| return new HttpStage(this, id, { | ||
| httpApi: this, | ||
| ...options, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Add multiple routes that uses the same configuration. The routes all go to the same path, but for different | ||
| * methods. | ||
| */ | ||
| public addRoutes(options: AddRoutesOptions): HttpRoute[] { | ||
| const methods = options.methods ?? [ HttpMethod.ANY ]; | ||
| return methods.map((method) => new HttpRoute(this, `${method}${options.path}`, { | ||
| httpApi: this, | ||
| routeKey: HttpRouteKey.with(options.path, method), | ||
| integration: options.integration, | ||
| })); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export * from './api'; | ||
| export * from './route'; | ||
| export * from './integration'; | ||
| export * from './integrations'; | ||
| export * from './stage'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.