Skip to content

Support AWS API Gateway integration mapping of request headers, querystring and path variables#7897

Merged
medikoo merged 5 commits into
serverless:masterfrom
benarena:master
Jul 6, 2020
Merged

Support AWS API Gateway integration mapping of request headers, querystring and path variables#7897
medikoo merged 5 commits into
serverless:masterfrom
benarena:master

Conversation

@benarena

@benarena benarena commented Jul 2, 2020

Copy link
Copy Markdown
Contributor

Closes: #6911

This PR implements the proposed changes in #6911 and preserves existing behavior for backward compatibility.

Existing configuration (still supported) marks integration request header "PrincipalId" as required and maps it to method.request.header.PrincipalId:

  events:
    - http:
        path: /path/{id}
        method: ANY
        integration: http-proxy
        request:
          uri: https://my.url/path/{id}
          parameters:
            paths:
              id: true
            headers:
              PrincipalId: true

cloudformation-template-update-stack.json:

{
      "Type": "AWS::ApiGateway::Method",
      "Properties": {
        "HttpMethod": "ANY",
        "RequestParameters": {
          "method.request.path.id": true,
          "method.request.header.PrincipalId": true
        },
        "Integration": {
          "IntegrationHttpMethod": "ANY",
          "Type": "HTTP_PROXY",
          "Uri": "https://my.url/path/{id}",
          "RequestParameters": {
            "integration.request.path.id": "method.request.path.id",
            "integration.request.header.PrincipalId": "method.request.header.PrincipalId"
          },
          ...
       }
}

New configuration marks integration request header "PrincipalId" as required and maps it to context.authorizer.principalId from the lambda authorizer:

  events:
    - http:
        path: /path/{id}
        method: ANY
        integration: http-proxy
        authorizer: ...
        request:
          uri: https://my.url/path/{id}
          parameters:
            paths:
              id: true
            headers:
              PrincipalId:
                required: true
                mappedValue: context.authorizer.principalId

Resulting cloudformation-template-update-stack.json:

{
      "Type": "AWS::ApiGateway::Method",
      "Properties": {
        "HttpMethod": "ANY",
        "RequestParameters": {
          "method.request.path.id": true,
          "method.request.header.PrincipalId": true
        },
        "Integration": {
          "IntegrationHttpMethod": "ANY",
          "Type": "HTTP_PROXY",
          "Uri": "https://my.url/path/{id}",
          "RequestParameters": {
            "integration.request.path.id": "method.request.path.id",
            "integration.request.header.PrincipalId": "context.authorizer.principalId"
          },
          ...
       }
}

… query variables and headers for AWS API Gateway
@codecov-commenter

codecov-commenter commented Jul 2, 2020

Copy link
Copy Markdown

Codecov Report

Merging #7897 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #7897   +/-   ##
=======================================
  Coverage   88.44%   88.44%           
=======================================
  Files         244      244           
  Lines        9059     9062    +3     
=======================================
+ Hits         8012     8015    +3     
  Misses       1047     1047           
Impacted Files Coverage Δ
...kage/compile/events/apiGateway/lib/method/index.js 100.00% <100.00%> (ø)
...ompile/events/apiGateway/lib/method/integration.js 98.24% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 94ff3b2...fac84ca. Read the comment docs.

@medikoo medikoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you @benarena ! In general it looks very good.

I've just added two suggestions, so our current conventions are maintained

Comment thread docs/providers/aws/events/apigateway.md

@medikoo medikoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@benarena Thank you, it looks excellent!

See just one suggestion from me, about test file naming, and we should be good

@medikoo medikoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you @benarena !

@medikoo
medikoo merged commit 56b335f into serverless:master Jul 6, 2020
@medikoo medikoo mentioned this pull request Oct 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow Specifying Parameter (Header, Querystring, Path) Mapping Values for API Gateway Integrations

3 participants