Skip to content
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

chore(apigatewayv2): move lambda and http proxy integrations to the 'integrations' module #11339

Merged
merged 4 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 58 additions & 8 deletions packages/@aws-cdk/aws-apigatewayv2-integrations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,74 @@

## Table of Contents

- [Introduction](#introduction)
- [Private Integration](#private-integration)
- [HTTP APIs](#http-apis)
- [Lambda Integration](#lambda)
- [HTTP Proxy Integration](#http-proxy)
- [Private Integration](#private-integration)

## Introduction
## 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.

Currently the following integrations are supported by this construct library.
### Lambda

## Private Integration
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 is 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
const booksFn = new lambda.Function(stack, 'BooksDefaultFn', { ... });
const booksIntegration = new LambdaProxyIntegration({
handler: booksDefaultFn,
});

const httpApi = new HttpApi(stack, 'HttpApi');

httpApi.addRoutes({
path: '/books',
methods: [ 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.myproxy.internal`.

```ts
const booksIntegration = new HttpProxyIntegration({
url: 'https://get-books-proxy.myproxy.internal',
});

const httpApi = new HttpApi(stack, 'HttpApi');

httpApi.addRoutes({
path: '/books',
methods: [ HttpMethod.GET ],
integration: booksIntegration,
});
```

### 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
#### Application Load Balancer

The following code is a basic application load balancer private integration of HTTP API:

Expand All @@ -47,7 +97,7 @@ const httpEndpoint = new HttpApi(stack, 'HttpProxyPrivateApi', {
});
```

### Network Load Balancer
#### Network Load Balancer

The following code is a basic network load balancer private integration of HTTP API:

Expand All @@ -66,7 +116,7 @@ const httpEndpoint = new HttpApi(stack, 'HttpProxyPrivateApi', {
});
```

### Cloud Map Service Discovery
#### Cloud Map Service Discovery

The following code is a basic discovery service private integration of HTTP API:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { HttpIntegrationType, HttpRouteIntegrationBindOptions, HttpRouteIntegrationConfig, IHttpRouteIntegration, PayloadFormatVersion } from '../integration';
import { HttpMethod } from '../route';
import {
HttpIntegrationType,
HttpRouteIntegrationBindOptions,
HttpRouteIntegrationConfig,
HttpMethod,
IHttpRouteIntegration,
PayloadFormatVersion,
} from '@aws-cdk/aws-apigatewayv2';

/**
* Properties to initialize a new `HttpProxyIntegration`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export * from './base-types';
export * from './alb';
export * from './nlb';
export * from './service-discovery';
export * from './http-proxy';
export * from './lambda';
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import {
HttpIntegrationType,
HttpRouteIntegrationBindOptions,
HttpRouteIntegrationConfig,
IHttpRouteIntegration,
PayloadFormatVersion,
} from '@aws-cdk/aws-apigatewayv2';
import { ServicePrincipal } from '@aws-cdk/aws-iam';
import { IFunction } from '@aws-cdk/aws-lambda';
import { Names, Stack } from '@aws-cdk/core';
import { HttpIntegrationType, HttpRouteIntegrationBindOptions, HttpRouteIntegrationConfig, IHttpRouteIntegration, PayloadFormatVersion } from '../integration';

/**
* Lambda Proxy integration properties
Expand Down
4 changes: 4 additions & 0 deletions packages/@aws-cdk/aws-apigatewayv2-integrations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
"@aws-cdk/aws-apigatewayv2": "0.0.0",
"@aws-cdk/aws-ec2": "0.0.0",
"@aws-cdk/aws-elasticloadbalancingv2": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-lambda": "0.0.0",
"@aws-cdk/aws-servicediscovery": "0.0.0",
"@aws-cdk/core": "0.0.0",
"constructs": "^3.2.0"
Expand All @@ -89,6 +91,8 @@
"@aws-cdk/aws-apigatewayv2": "0.0.0",
"@aws-cdk/aws-ec2": "0.0.0",
"@aws-cdk/aws-elasticloadbalancingv2": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-lambda": "0.0.0",
"@aws-cdk/aws-servicediscovery": "0.0.0",
"@aws-cdk/core": "0.0.0",
"constructs": "^3.2.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import '@aws-cdk/assert/jest';
import { HttpApi, HttpIntegration, HttpIntegrationType, HttpMethod, HttpRoute, HttpRouteKey, PayloadFormatVersion } from '@aws-cdk/aws-apigatewayv2';
import { Stack } from '@aws-cdk/core';
import { HttpApi, HttpIntegration, HttpIntegrationType, HttpMethod, HttpProxyIntegration, HttpRoute, HttpRouteKey, PayloadFormatVersion } from '../../../lib';
import { HttpProxyIntegration } from '../../lib';

describe('HttpProxyIntegration', () => {
test('default', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HttpApi } from '@aws-cdk/aws-apigatewayv2';
import * as lambda from '@aws-cdk/aws-lambda';
import { App, CfnOutput, Stack } from '@aws-cdk/core';
import { HttpApi, HttpProxyIntegration, LambdaProxyIntegration } from '../../../lib';
import { HttpProxyIntegration, LambdaProxyIntegration } from '../../lib';

/*
* Stack verification steps:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HttpApi } from '@aws-cdk/aws-apigatewayv2';
import * as lambda from '@aws-cdk/aws-lambda';
import { App, CfnOutput, Stack } from '@aws-cdk/core';
import { HttpApi, LambdaProxyIntegration } from '../../../lib';
import { LambdaProxyIntegration } from '../../lib';

/*
* Stack verification steps:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import '@aws-cdk/assert/jest';
import { HttpApi, HttpRoute, HttpRouteKey, PayloadFormatVersion } from '@aws-cdk/aws-apigatewayv2';
import { Code, Function, Runtime } from '@aws-cdk/aws-lambda';
import { App, Stack } from '@aws-cdk/core';
import { HttpApi, HttpRoute, HttpRouteKey, LambdaProxyIntegration, PayloadFormatVersion } from '../../../lib';
import { LambdaProxyIntegration } from '../../lib';

describe('LambdaProxyIntegration', () => {
test('default', () => {
Expand Down
15 changes: 8 additions & 7 deletions packages/@aws-cdk/aws-apigatewayv2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,13 @@ 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).
Integrations define how the HTTP API responds when a client reaches a specific Route. HTTP APIs support Lambda proxy
integration, HTTP proxy integration and, AWS service integrations, also known as private integrations. 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.

The URL to the endpoint can be retrieved via the `apiEndpoint` attribute.
Integrations are available at the `aws-apigatewayv2-integrations` module and more information is available in that module.
As an early example, the following code snippet configures a route `GET /books` with an HTTP proxy integration all
configures all other HTTP method calls to `/books` to a lambda proxy.

```ts
const getBooksIntegration = new HttpProxyIntegration({
Expand All @@ -85,6 +84,8 @@ httpApi.addRoutes({
});
```

The URL to the endpoint can be retrieved via the `apiEndpoint` attribute.

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.

Expand Down
1 change: 0 additions & 1 deletion packages/@aws-cdk/aws-apigatewayv2/lib/http/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export * from './api';
export * from './route';
export * from './integration';
export * from './integrations';
export * from './stage';
export * from './api-mapping';
export * from './vpc-link';

This file was deleted.

2 changes: 0 additions & 2 deletions packages/@aws-cdk/aws-apigatewayv2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,13 @@
"@aws-cdk/aws-certificatemanager": "0.0.0",
"@aws-cdk/aws-ec2": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-lambda": "0.0.0",
"@aws-cdk/aws-cloudwatch": "0.0.0",
"@aws-cdk/core": "0.0.0",
"constructs": "^3.2.0"
},
"peerDependencies": {
"@aws-cdk/aws-ec2": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-lambda": "0.0.0",
"@aws-cdk/aws-certificatemanager": "0.0.0",
"@aws-cdk/aws-cloudwatch": "0.0.0",
"@aws-cdk/core": "0.0.0",
Expand Down
37 changes: 14 additions & 23 deletions packages/@aws-cdk/aws-apigatewayv2/test/http/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import '@aws-cdk/assert/jest';
import { ABSENT } from '@aws-cdk/assert';
import { Metric } from '@aws-cdk/aws-cloudwatch';
import * as ec2 from '@aws-cdk/aws-ec2';
import { Code, Function, Runtime } from '@aws-cdk/aws-lambda';
import { Duration, Stack } from '@aws-cdk/core';
import { HttpApi, HttpMethod, LambdaProxyIntegration } from '../../lib';
import { HttpApi, HttpIntegrationType, HttpMethod, HttpRouteIntegrationBindOptions, HttpRouteIntegrationConfig, IHttpRouteIntegration, PayloadFormatVersion } from '../../lib';

describe('HttpApi', () => {
test('default', () => {
Expand Down Expand Up @@ -50,13 +49,7 @@ describe('HttpApi', () => {
test('default integration', () => {
const stack = new Stack();
const httpApi = new HttpApi(stack, 'api', {
defaultIntegration: new LambdaProxyIntegration({
handler: new Function(stack, 'fn', {
code: Code.fromInline('foo'),
runtime: Runtime.NODEJS_12_X,
handler: 'index.handler',
}),
}),
defaultIntegration: new DummyRouteIntegration(),
});

expect(stack).toHaveResourceLike('AWS::ApiGatewayV2::Route', {
Expand All @@ -76,13 +69,7 @@ describe('HttpApi', () => {
httpApi.addRoutes({
path: '/pets',
methods: [HttpMethod.GET, HttpMethod.PATCH],
integration: new LambdaProxyIntegration({
handler: new Function(stack, 'fn', {
code: Code.fromInline('foo'),
runtime: Runtime.NODEJS_12_X,
handler: 'index.handler',
}),
}),
integration: new DummyRouteIntegration(),
});

expect(stack).toHaveResourceLike('AWS::ApiGatewayV2::Route', {
Expand All @@ -102,13 +89,7 @@ describe('HttpApi', () => {

httpApi.addRoutes({
path: '/pets',
integration: new LambdaProxyIntegration({
handler: new Function(stack, 'fn', {
code: Code.fromInline('foo'),
runtime: Runtime.NODEJS_12_X,
handler: 'index.handler',
}),
}),
integration: new DummyRouteIntegration(),
});

expect(stack).toHaveResourceLike('AWS::ApiGatewayV2::Route', {
Expand Down Expand Up @@ -281,3 +262,13 @@ describe('HttpApi', () => {
expect(api.apiEndpoint).toBeDefined();
});
});

class DummyRouteIntegration implements IHttpRouteIntegration {
public bind(_: HttpRouteIntegrationBindOptions): HttpRouteIntegrationConfig {
return {
payloadFormatVersion: PayloadFormatVersion.VERSION_2_0,
type: HttpIntegrationType.HTTP_PROXY,
uri: 'some-uri',
};
}
}
Loading