Skip to content

Commit

Permalink
feat: add a http proxy with api gateway typescript example (#297)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 26, 2020
1 parent 505d05d commit b34c0e8
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ $ cdk destroy
| [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 |
| [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 |
| [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 |
| [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 |

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

Expand Down
39 changes: 39 additions & 0 deletions typescript/http-proxy-apigateway/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Proxy-APIGateway
<!--BEGIN STABILITY BANNER-->
---

![Stability: Stable](https://img.shields.io/badge/stability-Stable-success.svg?style=for-the-badge)

> **This is a stable example. It should successfully build out of the box**
>
> This examples does is built on Construct Libraries marked "Stable" and does not have any infrastructure prerequisites to build.
---
<!--END STABILITY BANNER-->

This example creates Http proxy using API gateway of cdk.
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.

> 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).
## Build

To build this app, you need to be in this example's root folder. Then run the following:

```bash
npm install -g aws-cdk
npm install
npm run build
```

This will install the necessary CDK, then this example's dependencies, and then build your TypeScript files and your CloudFormation template.

## Deploy

Run `cdk deploy`. This will deploy / redeploy your Stack to your AWS Account.

After the deployment you will see the API's URL, which represents the url you can then use.

## Synthesize Cloudformation Template

To see the Cloudformation template generated by the CDK, run `cdk synth`, then check the output file in the "cdk.out" directory.
3 changes: 3 additions & 0 deletions typescript/http-proxy-apigateway/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"app": "node index"
}
21 changes: 21 additions & 0 deletions typescript/http-proxy-apigateway/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as cdk from "@aws-cdk/core";

import { EndpointType } from "@aws-cdk/aws-apigateway";

import { Proxy } from "./proxy";

export class ProxyStack extends cdk.Stack {
constructor(app: cdk.App, id: string, props?: cdk.StackProps) {
super(app, id, props);

const proxy = new Proxy(this, "Proxy", {
apiName: "HttpProxy", endpointType: EndpointType.EDGE
});

proxy.addProxy("aws", "https://aws.amazon.com/ko");
}
}

const app = new cdk.App();
new ProxyStack(app, "HttpProxy");
app.synth();
25 changes: 25 additions & 0 deletions typescript/http-proxy-apigateway/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "http-proxy-apigateway",
"version": "1.0.0",
"description": "Generate http proxy using apigateway of cdk",
"private": true,
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"cdk": "cdk"
},
"author": {
"name": "Amazon Web Services",
"url": "https://aws.amazon.com",
"organization": true
},
"license": "Apache-2.0",
"devDependencies": {
"@types/node": "^14.0.6",
"typescript": "3.9.3"
},
"dependencies": {
"@aws-cdk/aws-apigateway": "*",
"@aws-cdk/core": "*"
}
}
47 changes: 47 additions & 0 deletions typescript/http-proxy-apigateway/proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Construct, CfnOutput } from "@aws-cdk/core";

import * as apiGateway from "@aws-cdk/aws-apigateway";

export interface ProxyProps {
readonly apiName: string;
readonly endpointType: apiGateway.EndpointType;
}

export class Proxy extends Construct {
public readonly api: apiGateway.RestApi;

constructor(scope: Construct, id: string, props: ProxyProps) {
super(scope, id);

this.api = new apiGateway.RestApi(this, "API", {
restApiName: props.apiName,
endpointConfiguration: {
types: [props.endpointType]
},
});
}

public addProxy(id: string, baseUrl: string, method: string = "GET") {
const namespace = this.api.root.addResource(id);
const proxyResource = new apiGateway.ProxyResource(this, `ProxyResource${method}${id}`, {
parent: namespace,
anyMethod: false,
});

proxyResource.addMethod(method, new apiGateway.HttpIntegration(`${baseUrl}/{proxy}`, {
proxy: true,
httpMethod: method,
options: {
requestParameters: {
"integration.request.path.proxy": "method.request.path.proxy"
}
}
}), {
requestParameters: {
"method.request.path.proxy": true
}
});

new CfnOutput(this, `EndPoint${method}${id}`, { value: this.api.urlForPath(proxyResource.path) });
}
}
20 changes: 20 additions & 0 deletions typescript/http-proxy-apigateway/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target":"ES2018",
"module": "commonjs",
"lib": ["es2016", "es2017.object", "es2017.string"],
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": false,
"inlineSourceMap": true,
"inlineSources": true,
"experimentalDecorators": true,
"strictPropertyInitialization":false
}
}

0 comments on commit b34c0e8

Please sign in to comment.