-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(aws-apigateway): "LambdaRestApi" and "addProxy" routes (#867)
`LambdaRestApi` is a higher level construct for defining Lambda-backed REST APIs: const backend = new lambda.Function(...); const api = new apigw.LambdaRestApi({ handler: backend, proxyPath: '/' }); `resource.addProxy` allows mounting a {proxy+} route on a path, including an "ANY" method integration: const api = new apigw.RestApi(this, 'api'); api.root.addProxy({ defaultIntegration: new apigw.HttpIntegration('http://foo/bar') });
- Loading branch information
Elad Ben-Israel
authored
Oct 8, 2018
1 parent
2d63a35
commit aa76305
Showing
9 changed files
with
482 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,64 @@ | ||
import lambda = require('@aws-cdk/aws-lambda'); | ||
import cdk = require('@aws-cdk/cdk'); | ||
import { LambdaIntegration } from './integrations'; | ||
import { RestApi, RestApiProps } from './restapi'; | ||
|
||
export interface LambdaRestApiProps { | ||
/** | ||
* The default Lambda function that handles all requests from this API. | ||
* | ||
* This handler will be used as a the default integration for all methods in | ||
* this API, unless specified otherwise in `addMethod`. | ||
*/ | ||
handler: lambda.Function; | ||
|
||
/** | ||
* An API path for a greedy proxy with an "ANY" method, which will route all | ||
* requests under that path to the defined handler. | ||
* | ||
* If not defined, you will need to explicitly define the API model using | ||
* `addResource` and `addMethod` (or `addProxy`). | ||
* | ||
* @default undefined | ||
*/ | ||
proxyPath?: string; | ||
|
||
/** | ||
* Further customization of the REST API. | ||
* | ||
* @default defaults | ||
*/ | ||
options?: RestApiProps; | ||
} | ||
|
||
/** | ||
* Defines an API Gateway REST API with AWS Lambda proxy integration. | ||
* | ||
* Use the `proxyPath` property to define a greedy proxy ("{proxy+}") and "ANY" | ||
* method from the specified path. If not defined, you will need to explicity | ||
* add resources and methods to the API. | ||
*/ | ||
export class LambdaRestApi extends RestApi { | ||
constructor(parent: cdk.Construct, id: string, props: LambdaRestApiProps) { | ||
if (props.options && props.options.defaultIntegration) { | ||
throw new Error(`Cannot specify "options.defaultIntegration" since Lambda integration is automatically defined`); | ||
} | ||
|
||
super(parent, id, { | ||
defaultIntegration: new LambdaIntegration(props.handler), | ||
...props.options | ||
}); | ||
|
||
// if proxyPath is specified, add a proxy at the specified path | ||
// we will need to create all resources along the path. | ||
const proxyPath = props.proxyPath; | ||
if (proxyPath) { | ||
const route = proxyPath.split('/').filter(x => x); | ||
let curr = this.root; | ||
for (const part of route) { | ||
curr = curr.addResource(part); | ||
} | ||
curr.addProxy(); | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.