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:

Daniel B.
  • 1,650
  • 1
  • 19
  • 40

2 Answers2

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+}'
J.A. Simmons V
  • 176
  • 1
  • 8
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
koxon
  • 818
  • 1
  • 10
  • 12