diff --git a/README.md b/README.md index 733746175..871369792 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/typescript/http-proxy-apigateway/README.md b/typescript/http-proxy-apigateway/README.md new file mode 100644 index 000000000..f7e10e519 --- /dev/null +++ b/typescript/http-proxy-apigateway/README.md @@ -0,0 +1,39 @@ +# Proxy-APIGateway + +--- + +![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. + +--- + + +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. diff --git a/typescript/http-proxy-apigateway/cdk.json b/typescript/http-proxy-apigateway/cdk.json new file mode 100644 index 000000000..2f0e44c6f --- /dev/null +++ b/typescript/http-proxy-apigateway/cdk.json @@ -0,0 +1,3 @@ +{ + "app": "node index" +} diff --git a/typescript/http-proxy-apigateway/index.ts b/typescript/http-proxy-apigateway/index.ts new file mode 100644 index 000000000..d3ef7327d --- /dev/null +++ b/typescript/http-proxy-apigateway/index.ts @@ -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(); diff --git a/typescript/http-proxy-apigateway/package.json b/typescript/http-proxy-apigateway/package.json new file mode 100644 index 000000000..625cf03b6 --- /dev/null +++ b/typescript/http-proxy-apigateway/package.json @@ -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": "*" + } +} diff --git a/typescript/http-proxy-apigateway/proxy.ts b/typescript/http-proxy-apigateway/proxy.ts new file mode 100644 index 000000000..0c0e4a8a8 --- /dev/null +++ b/typescript/http-proxy-apigateway/proxy.ts @@ -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) }); + } +} \ No newline at end of file diff --git a/typescript/http-proxy-apigateway/tsconfig.json b/typescript/http-proxy-apigateway/tsconfig.json new file mode 100644 index 000000000..327de69de --- /dev/null +++ b/typescript/http-proxy-apigateway/tsconfig.json @@ -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 + } +}