-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
505d05d
commit b34c0e8
Showing
7 changed files
with
156 additions
and
0 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
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. |
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,3 @@ | ||
{ | ||
"app": "node index" | ||
} |
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,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(); |
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,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": "*" | ||
} | ||
} |
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,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) }); | ||
} | ||
} |
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,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 | ||
} | ||
} |