API Gateway HTTP Proxy integration with aws-sam (NOT Lambda Proxy)
Asked Answered
B

2

7

I am trying to use aws-sam to develop / simulate my API Gateway locally. My API gateway makes liberal use of the HTTP proxy integrations. The production Resource looks like this:

Screenshot of an HTTP Proxy integration on an API Gateway Resource Method

All of the aws-sam examples which I've found, as well as related documentation and Q&A, use the Lambda integrations / have a hard dependency on a Lambda function being the proxied resource, versus an HTTP Proxy integration.

Is there a way to define an HTTP Proxy resource for an aws-sam application? (As opposed to a Lambda Proxy resource?)

Related:

Bacardi answered 12/3, 2019 at 19:15 Comment(0)
B
5

Yes, SAM is just a Cloud Formation transform. So you can still create traditional CloudFormation objects as usual. You might be able to also attach it to your Serverless::API but I am less confident about that.

Resource:
  Api:
    Type: 'AWS::ApiGateway::RestApi'
    Properties:
      Description: A test API
      Name: MyRestAPI

    Type: 'AWS::ApiGateway::Resource'
    Properties:
      ParentId: !GetAtt Api.RootResourceId
      RestApiId: !Ref Api
      PathPart: '{proxy+}'

or possibly (untested):

Resource:
  Api:
    Type: 'AWS::Serverless::Api'
    Properties:
      Description: A test API
      Name: MyRestAPI

    Type: 'AWS::ApiGateway::Resource'
    Properties:
      ParentId: !GetAtt Api.RootResourceId
      RestApiId: !Ref Api
      PathPart: '{proxy+}'
Booted answered 9/9, 2019 at 4:37 Comment(1)
For SAM : #54386121Fellows
C
2

I tested J.A second suggestion and it works! Thank you.

  MyGalleryApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: prod

  GetImagesResource:
    Type: AWS::ApiGateway::Resource
    Properties:
      RestApiId: !Ref MyGalleryApi
      ParentId: !GetAtt MyGalleryApi.RootResourceId
      PathPart: getImages

  GetImagesMethod:
    Type: AWS::ApiGateway::Method
    Properties:
      RestApiId: !Ref MyGalleryApi
      ResourceId: !Ref GetImagesResource
      HttpMethod: GET
      AuthorizationType: AWS_IAM
      Integration:
        Type: HTTP_PROXY
        IntegrationHttpMethod: GET
        Uri: https://proxy.com/path/getImages

You can combine both HTTP_PROXY and LAMBA_PROXY together in one template by using RestApiId in your function definition:

  NonceFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/handlers/nonce.handler
      Role: !GetAtt MyGalleryLambdaExecutionRole.Arn
      Architectures:
        - x86_64
      MemorySize: 256
      Description: Nonce function to start authentication
      Policies:
        - DynamoDBCrudPolicy:
            TableName: !Ref UsersTable
      Environment:
        Variables:
          USERTABLE_NAME: !Ref UsersTable
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Path: /nonce
            Method: GET
            RestApiId: !Ref MyGalleryApi
            RequestParameters:
              - method.request.path.address:
                  Required: True
Chinatown answered 10/10, 2022 at 21:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.