Skip to content

Commit affc135

Browse files
committed
example deploying proxy APIs
1 parent 1871faa commit affc135

File tree

10 files changed

+143
-0
lines changed

10 files changed

+143
-0
lines changed

Diff for: README.md

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
* [Web API](web-api) – a simple REST api, shows how to configure and deploy an API Gateway interface along with the Lambda function
1818
* [Web Serving HTML](web-serving-html) – shows how to change error and success content types and response codes, and how to perform browser redirects
19+
* [Generic handlers](web-api-generic-handlers) – shows how to capture dynamic paths and set up handlers for any content type easily
20+
* [Custom response codes](web-api-custom-status-code) – shows how to change status codes for response
1921
* [Custom Headers](web-api-custom-headers) – shows how to return custom headers from API responses
2022
* [Custom CORS origins](web-api-custom-cors) – shows how to control allowed CORS origins/headers
2123
* [Intercepting requests](intercepting-requests) – an example showing how to prevent or modify requests
@@ -28,6 +30,10 @@
2830
* [Using AWS Mobile Analytics for server-side events](aws-mobile-analytics) – log events into AWS Mobile Analytics for easy internal telemetry
2931
* [Custom Authorizers](custom-authorizers) – a simple example of how to set up custom authorizers in API Gateway
3032

33+
## Proxy API
34+
35+
* [Deploying a Proxy API](deploy-proxy-api) – an example of how to create an API Gateway that will proxy all requests directly to a Lambda function
36+
3137
## Chat-bots
3238

3339
* [Simple Bot](simple-bot) – an example demonstrating how to receive and respond with simple text messages

Diff for: deploy-proxy-api/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Deploying Proxy APIs easily
2+
3+
Since September 2017, API Gateway supports proxying requests to Lambda without having to set up specific handlers, methods or conversion templates. This can be a good alternative to using Claudia API Builder if you need to set up something more generic. Claudia 2.0 can automatically deploy a proxy API for your Lambda function -- just add `--deploy-proxy-api` when creating it.
4+
5+
To try out this example, use:
6+
7+
1. `npm install` to grab the dependencies
8+
2. `npm start` to deploy the [api.js](api.js) to Lambda and create a proxy api
9+
10+
The procedure will print out a URL. You can send HTTP requests to it, and any sub-URLs, and get back the full request object as the response.
11+
12+
For more information on this feature, check out [Build an API Gateway API Using Proxy Integration and a Proxy Resource](http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy.html ) from AWS documentation.

Diff for: deploy-proxy-api/main.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*global exports*/
2+
exports.handler = function (event, context) {
3+
'use strict';
4+
context.succeed({
5+
statusCode: 200,
6+
headers: { 'Content-Type': 'application/json' },
7+
body: JSON.stringify(event)
8+
});
9+
};

Diff for: deploy-proxy-api/package.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "deploy-proxy-api",
3+
"version": "1.0.0",
4+
"description": "a simple example deploying a proxy API with a Lambda function",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "claudia create --region us-east-1 --deploy-proxy-api --handler main.handler"
8+
},
9+
"author": "Gojko Adzic <[email protected]>",
10+
"license": "MIT",
11+
"devDependencies": {
12+
"claudia": "^2.0.1"
13+
}
14+
}

Diff for: web-api-custom-status-code/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
A simple example demonstrating how to use custom response codes in API responses.
2+
3+
1. run `npm install` to grab the dependencies
4+
2. run `npm start` to set up the lambda project under the default name on AWS
5+
3. Check out the API ID in `claudia.json` (the previous step creates the file)
6+
4. Open https://API_ID.execute-api.us-east-1.amazonaws.com/latest/status-code in a browser (replace API_ID with the API ID from `claudia.json`) and inspect the headers. You should see the status code of 201
7+
8+
Check out [web.js](web.js) to see how the paths are set up. For more information, see the [Claudia Api Builder](https://github.com/claudiajs/claudia-api-builder) documentation.
9+

Diff for: web-api-custom-status-code/package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "web-api-custom-headers",
3+
"description": "An example for returning custom headers from API Gateway responses",
4+
"private": true,
5+
"files": [
6+
"*.js"
7+
],
8+
"scripts": {
9+
"start": "claudia create --name web-api-custom-headers --region us-east-1 --api-module web",
10+
"deploy": "claudia update"
11+
},
12+
"devDependencies": {
13+
"claudia": "^2.0.0"
14+
},
15+
"dependencies": {
16+
"bluebird": "^3.3.4",
17+
"claudia-api-builder": "^2.0.0"
18+
}
19+
}

Diff for: web-api-custom-status-code/web.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*global require, module*/
2+
var ApiBuilder = require('claudia-api-builder'),
3+
api = new ApiBuilder(),
4+
Promise = require('bluebird');
5+
module.exports = api;
6+
7+
// set headers as key-value pairs using the success.headers property */
8+
api.get('/status-code', function () {
9+
'use strict';
10+
return 'OK';
11+
}, {success: 201 });
12+
13+
api.get('/error-code', function () {
14+
'use strict';
15+
throw 'Abort';
16+
}, {error: 404});
17+
18+
// or use dynamic codes returning new api.ApiResponse(content, headers)
19+
api.get('/programmatic-codes', function () {
20+
'use strict';
21+
return new api.ApiResponse('OK', {'Content-Type': 'text/plain'}, 202);
22+
});
23+
24+
// dynamic headers also work with promises, just resolve with new api.ApiResponse
25+
26+
api.get('/programmatic-codes-promise', function () {
27+
'use strict';
28+
return Promise.delay(100).then(function () {
29+
return new api.ApiResponse('OK', {'X-Version': '303', 'Content-Type': 'text/plain'}, 400);
30+
});
31+
});
32+

Diff for: web-api-generic-handlers/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
A simple example demonstrating how to create generic handlers with Claudia API Builder. Since September 2016, API Gateway allows you to set up a wildcard path matcher using `{proxy+}`, and set up a single handler for all supported HTTP methods using the `ANY` method. With Claudia API Builder, since version 2.0, you can use both those features directly.
2+
3+
1. run `npm install` to grab the dependencies
4+
2. run `npm start` to set up the lambda project under the default name on AWS. It will print a URL.
5+
3. Open the `URL + '/echo'` in a browser to see the generic response, post to it, or invoke it with some other HTTP method using curl.
6+
4. Open the `URL + '/dynamic/' + any path` to see how dynamic paths are parsed
7+
8+
Check out [web.js](web.js) to see how the paths are set up. For more information, see the [Claudia Api Builder](https://github.com/claudiajs/claudia-api-builder) documentation.
9+

Diff for: web-api-generic-handlers/package.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "web-api-generic-handlers",
3+
"description": "Demo project for generic handlers",
4+
"private": true,
5+
"files": [
6+
"*.js"
7+
],
8+
"scripts": {
9+
"start": "claudia create --region us-east-1 --api-module web",
10+
"deploy": "claudia update"
11+
},
12+
"devDependencies": {
13+
"claudia": "^2.0.0"
14+
},
15+
"dependencies": {
16+
"claudia-api-builder": "^2.0.0"
17+
}
18+
}

Diff for: web-api-generic-handlers/web.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*global require, module*/
2+
var ApiBuilder = require('claudia-api-builder'),
3+
api = new ApiBuilder();
4+
module.exports = api;
5+
6+
7+
api.any('/echo', function (request) {
8+
'use strict';
9+
return request;
10+
});
11+
12+
api.get('/dynamic/{proxy+}', function (request) {
13+
'use strict';
14+
return 'You called ' + request.pathParams.proxy;
15+
}, { success: { contentType: 'text/plain'}});

0 commit comments

Comments
 (0)