# HTTP Proxy via CloudFormation

**URL:** <https://forum.serverless.com/t/http-proxy-via-cloudformation/1366>\
**Category:** Serverless Framework\
**Tags:** aws\
**Created:** [February 21, 2017, 11:15am UTC](https://forum.serverless.com/t/http-proxy-via-cloudformation/1366 "2017-02-21T11:15:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![timwis](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/timwis/32/443_2.png) [@timwis](https://forum.serverless.com/u/timwis)\
**Post date:** [February 21, 2017, 11:15am UTC](https://forum.serverless.com/t/http-proxy-via-cloudformation/1366/1 "2017-02-21T11:15:53Z")

</div>

Hello folks, I’m using the [example](https://serverless.com/framework/docs/providers/aws/events/apigateway/#setting-an-http-proxy-on-api-gateway) provided in the docs to deploy a pass-through HTTP proxy alongside my lambda function, and I’m afraid it’s missing a few important settings that are easy to enable in the API Gateway UI. Specifically, the **Use HTTP Proxy Integration** setting on the Integration Request page adds settings like the URL Path Parameter `proxy` mapping to `method.request.path.proxy`. I can’t tell how it allows querystrings to pass through though.

Does anyone know how to replicate this functionality via CloudFormation template, so I can deploy it alongside my serverless lambda function?

Goal: `myserverlesssite.com/carto/api/v2/sql?q=select ...` is proxied to `phl.carto.com/api/v2/sql?q=select ...`

Here’s how I’ve been approaching it:

```auto
resources:
  Resources:
    ProxyResource:
      Type: AWS::ApiGateway::Resource
      Properties:
        ParentId:
          Fn::GetAtt:
            - ApiGatewayRestApi # our default Rest API logical ID
            - RootResourceId
        PathPart: '{proxy+}' # the endpoint in your API that is set as proxy
        RestApiId:
          Ref: ApiGatewayRestApi
    ProxyMethod:
      Type: AWS::ApiGateway::Method
      Properties:
        ResourceId:
          Ref: ProxyResource
        RestApiId:
          Ref: ApiGatewayRestApi
        AuthorizationType: NONE
        HttpMethod: GET # the method of your proxy. Is it GET or POST or ... ?
        MethodResponses:
          - StatusCode: 200
        Integration:
          IntegrationHttpMethod: GET
          Type: HTTP
          Uri: https://phl.carto.com/{proxy} # the URL you want to set a proxy to
          IntegrationResponses:
            - StatusCode: 200
        RequestParameters:
          integration.request.path.proxy: proxy

```

---

<div class="post-metadata">

**Author:** ![timwis](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/timwis/32/443_2.png) [@timwis](https://forum.serverless.com/u/timwis)\
**Post date:** [February 28, 2017, 11:36am UTC](https://forum.serverless.com/t/http-proxy-via-cloudformation/1366/2 "2017-02-28T11:36:06Z")

</div>

I came across [this blog post](https://blog.jayway.com/2016/09/18/introduction-swagger-cloudformation-api-gateway/) which pointed out that you can include swagger in CloudFormation. But it appears only for ApiGateway cloudformation. So I gave it a shot, exporting the swagger of the working api I configured manually, then pulled out just the http proxy part, and gave it a go in `serverless.yml`.

It deploys properly, but ends up overwriting the other serverless-created resources :-/

```auto
resources:
  Resources:
    ApiGatewayRestApi: # default serverless logical id
      Type: AWS::ApiGateway::RestApi
      Properties:
        Name: dev-soda-carto
        Body:
          swagger: '2.0'
          schemes:
            - https
          paths:
            /carto/{proxy+}:
              x-amazon-apigateway-any-method:
                produces:
                - "application/json"
                parameters:
                - name: "proxy"
                  in: "path"
                  required: true
                  type: "string"
                responses: {}
                x-amazon-apigateway-integration:
                  responses:
                    default:
                      statusCode: "200"
                  uri: "https://phl.carto.com/{proxy}"
                  requestParameters:
                    integration.request.header.Accept-Encoding: "'identity'"
                    integration.request.path.proxy: "method.request.path.proxy"
                  passthroughBehavior: "when_no_match"
                  httpMethod: "ANY"
                  cacheNamespace: "jf083p"
                  cacheKeyParameters:
                  - "method.request.path.proxy"
                  type: "http_proxy"

```

---

<div class="post-metadata">

**Author:** ![timwis](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/timwis/32/443_2.png) [@timwis](https://forum.serverless.com/u/timwis)\
**Post date:** [February 28, 2017, 12:48pm UTC](https://forum.serverless.com/t/http-proxy-via-cloudformation/1366/3 "2017-02-28T12:48:33Z")

</div>

Update: I got it working! I noticed that the exported swagger is _basically_ cloudformation, so that told me what pieces were missing from the original cloudformation.

```auto
resources:
  Resources:
    ProxyParentResource:
      Type: AWS::ApiGateway::Resource
      Properties:
        ParentId:
          Fn::GetAtt:
            - ApiGatewayRestApi # serverless default Rest API logical ID
            - RootResourceId
        PathPart: 'carto'
        RestApiId:
          Ref: ApiGatewayRestApi
    ProxyResource:
      Type: AWS::ApiGateway::Resource
      Properties:
        ParentId:
          Ref: ProxyParentResource
        PathPart: '{proxy+}' # the endpoint in your API that is set as proxy
        RestApiId:
          Ref: ApiGatewayRestApi
    ProxyMethod:
      Type: AWS::ApiGateway::Method
      Properties:
        ResourceId:
          Ref: ProxyResource
        RestApiId:
          Ref: ApiGatewayRestApi
        AuthorizationType: NONE
        HttpMethod: GET # the method of your proxy. Is it GET or POST or ... ?
        MethodResponses:
          - StatusCode: 200
        RequestParameters:
          method.request.path.proxy: true
        Integration:
          IntegrationHttpMethod: GET
          Type: HTTP_PROXY
          Uri: https://phl.carto.com/{proxy} # the URL you want to set a proxy to
          IntegrationResponses:
            - StatusCode: 200
          RequestParameters:
            integration.request.path.proxy: method.request.path.proxy
            integration.request.header.Accept-Encoding: "'identity'"
          PassthroughBehavior: WHEN_NO_MATCH

```

---

<div class="post-metadata">

**Author:** ![northerncodemky](https://avatars.discourse-cdn.com/v4/letter/n/e36b37/32.png) [@northerncodemky](https://forum.serverless.com/u/northerncodemky)\
**Post date:** [May 3, 2017, 9:14am UTC](https://forum.serverless.com/t/http-proxy-via-cloudformation/1366/4 "2017-05-03T09:14:05Z")

</div>

The AWS documentation is poor in many places but I’ve found it almost unusable when it comes to API Gateway, so thanks for the above!  
I’d got almost all the way to that by inspecting the integration PATCH requests when manually setting them up in the management console, but in the process removed the RequestParameters from the method.
