Skip to content

Commit ffae700

Browse files
syeutyuNiranjan Jayakarmergify[bot]
authored
feat: add a http proxy with api gateway typescript example (#297)
* feat: add a http proxy with api gateway typescript example * fix: fix dependencies version * docs: Add more information in README.md * docs: change naming * docs: Fix naming * docs: Fix broken github url Co-authored-by: Niranjan Jayakar <nija@amazon.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 561229a commit ffae700

File tree

7 files changed

+156
-0
lines changed

7 files changed

+156
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ $ cdk destroy
4949
| [ecs-service-with-logging](https://github.com/aws-samples/aws-cdk-examples/tree/master/typescript/ecs/ecs-service-with-logging/) | Starting a container fronted by a load balancer on ECS |
5050
| [fargate-service-with-logging](https://github.com/aws-samples/aws-cdk-examples/tree/master/typescript/ecs/fargate-service-with-logging/) | Starting a container fronted by a load balancer on Fargate |
5151
| [custom-logical-names](https://github.com/aws-samples/aws-cdk-examples/tree/master/typescript/custom-logical-names/) | Example of how to override logical name allocation |
52+
| [http-proxy-apigateway](https://github.com/aws-samples/aws-cdk-examples/tree/master/typescript/http-proxy-apigateway/) | Use ApiGateway to set up a http proxy |
5253

5354
## Java examples <a name="Java"></a>
5455

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Proxy-APIGateway
2+
<!--BEGIN STABILITY BANNER-->
3+
---
4+
5+
![Stability: Stable](https://img.shields.io/badge/stability-Stable-success.svg?style=for-the-badge)
6+
7+
> **This is a stable example. It should successfully build out of the box**
8+
>
9+
> This examples does is built on Construct Libraries marked "Stable" and does not have any infrastructure prerequisites to build.
10+
11+
---
12+
<!--END STABILITY BANNER-->
13+
14+
This example creates Http proxy using API gateway of cdk.
15+
If you want to parse another region site(your origin is us-east-1 but scrap site origin is ap-north-east-2) then you can use this proxy.
16+
17+
> For more information on using Http proxy with apigateway clik [here](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-http.html).
18+
19+
## Build
20+
21+
To build this app, you need to be in this example's root folder. Then run the following:
22+
23+
```bash
24+
npm install -g aws-cdk
25+
npm install
26+
npm run build
27+
```
28+
29+
This will install the necessary CDK, then this example's dependencies, and then build your TypeScript files and your CloudFormation template.
30+
31+
## Deploy
32+
33+
Run `cdk deploy`. This will deploy / redeploy your Stack to your AWS Account.
34+
35+
After the deployment you will see the API's URL, which represents the url you can then use.
36+
37+
## Synthesize Cloudformation Template
38+
39+
To see the Cloudformation template generated by the CDK, run `cdk synth`, then check the output file in the "cdk.out" directory.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"app": "node index"
3+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as cdk from "@aws-cdk/core";
2+
3+
import { EndpointType } from "@aws-cdk/aws-apigateway";
4+
5+
import { Proxy } from "./proxy";
6+
7+
export class ProxyStack extends cdk.Stack {
8+
constructor(app: cdk.App, id: string, props?: cdk.StackProps) {
9+
super(app, id, props);
10+
11+
const proxy = new Proxy(this, "Proxy", {
12+
apiName: "HttpProxy", endpointType: EndpointType.EDGE
13+
});
14+
15+
proxy.addProxy("aws", "https://aws.amazon.com/ko");
16+
}
17+
}
18+
19+
const app = new cdk.App();
20+
new ProxyStack(app, "HttpProxy");
21+
app.synth();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "http-proxy-apigateway",
3+
"version": "1.0.0",
4+
"description": "Generate http proxy using apigateway of cdk",
5+
"private": true,
6+
"scripts": {
7+
"build": "tsc",
8+
"watch": "tsc -w",
9+
"cdk": "cdk"
10+
},
11+
"author": {
12+
"name": "Amazon Web Services",
13+
"url": "https://aws.amazon.com",
14+
"organization": true
15+
},
16+
"license": "Apache-2.0",
17+
"devDependencies": {
18+
"@types/node": "^14.0.6",
19+
"typescript": "3.9.3"
20+
},
21+
"dependencies": {
22+
"@aws-cdk/aws-apigateway": "*",
23+
"@aws-cdk/core": "*"
24+
}
25+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Construct, CfnOutput } from "@aws-cdk/core";
2+
3+
import * as apiGateway from "@aws-cdk/aws-apigateway";
4+
5+
export interface ProxyProps {
6+
readonly apiName: string;
7+
readonly endpointType: apiGateway.EndpointType;
8+
}
9+
10+
export class Proxy extends Construct {
11+
public readonly api: apiGateway.RestApi;
12+
13+
constructor(scope: Construct, id: string, props: ProxyProps) {
14+
super(scope, id);
15+
16+
this.api = new apiGateway.RestApi(this, "API", {
17+
restApiName: props.apiName,
18+
endpointConfiguration: {
19+
types: [props.endpointType]
20+
},
21+
});
22+
}
23+
24+
public addProxy(id: string, baseUrl: string, method: string = "GET") {
25+
const namespace = this.api.root.addResource(id);
26+
const proxyResource = new apiGateway.ProxyResource(this, `ProxyResource${method}${id}`, {
27+
parent: namespace,
28+
anyMethod: false,
29+
});
30+
31+
proxyResource.addMethod(method, new apiGateway.HttpIntegration(`${baseUrl}/{proxy}`, {
32+
proxy: true,
33+
httpMethod: method,
34+
options: {
35+
requestParameters: {
36+
"integration.request.path.proxy": "method.request.path.proxy"
37+
}
38+
}
39+
}), {
40+
requestParameters: {
41+
"method.request.path.proxy": true
42+
}
43+
});
44+
45+
new CfnOutput(this, `EndPoint${method}${id}`, { value: this.api.urlForPath(proxyResource.path) });
46+
}
47+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"target":"ES2018",
4+
"module": "commonjs",
5+
"lib": ["es2016", "es2017.object", "es2017.string"],
6+
"strict": true,
7+
"noImplicitAny": true,
8+
"strictNullChecks": true,
9+
"noImplicitThis": true,
10+
"alwaysStrict": true,
11+
"noUnusedLocals": true,
12+
"noUnusedParameters": true,
13+
"noImplicitReturns": true,
14+
"noFallthroughCasesInSwitch": false,
15+
"inlineSourceMap": true,
16+
"inlineSources": true,
17+
"experimentalDecorators": true,
18+
"strictPropertyInitialization":false
19+
}
20+
}

0 commit comments

Comments
 (0)