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

feat: add a http proxy with api gateway typescript example #297

Merged
merged 8 commits into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
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/proxy-apigateway/) | Use ApiGateway to set up lambda proxy |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, this isn't setting up a lambda proxy, but a http proxy

Suggested change
| [http-proxy-apigateway](https://github.com/aws-samples/aws-cdk-examples/tree/master/typescript/proxy-apigateway/) | Use ApiGateway to set up lambda proxy |
| [http-proxy-apigateway](https://github.com/aws-samples/aws-cdk-examples/tree/master/typescript/proxy-apigateway/) | Use ApiGateway to set up a http proxy |

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you, that is my typo


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

Expand Down
39 changes: 39 additions & 0 deletions typescript/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/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/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/proxy-apigateway/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "proxy-api-gateway",
"version": "1.0.0",
"description": "Generate proxy using api gateway 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/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/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
}
}