CodeWithYou

Create HTTP Proxy APIGateway in AWS CDK

Published on
Authors
HTTP Proxy APIGateway

Photo by dakmonk

In this article, I will demo how to create an API gateway with HTTP proxy integration.

REST API with HTTP proxy integration

HTTP proxy integration is a simple, powerful, and versatile mechanism to build an API that allows a web application to access multiple resources or features of the integrated HTTP endpoint

Create an API with HTTP proxy integration

Firstly, let's start by creating an Http AP

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

Create proxy resource

const namespace = this.api.root.addResource(id)
const proxyResource = new apiGateway.ProxyResource(this, `ProxyResource${method}${id}`, {
  parent: namespace,
  anyMethod: false,
})

Config method

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,
    },
  }
)

The code for this article is available on GitHub

That all, now you can deploy the stack to aws and try access to api endpoint. Like this: https://[api-id].execute-api.[regeion].amazonaws.com/prod/blog/[proxy+]

Clean up

Don't forget clean all resource avoid unexpected cost.

npx cdk destroy
Advertisement