aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

[Feature] Local support for integration type Mock

Open islasjuanp opened this issue 2 years ago • 6 comments

I'm trying to create a mock method integration and test it in the local environment. I was able to build and start the following example but I'm getting the following error when trying to hit the endpoint.

Do you know if there is anything else that I need to configure in order to have it working in my local environment?

Sam version

$ sam --version
SAM CLI, version 1.78.0

Error

Exception on / [GET]
Traceback (most recent call last):
  File "/usr/local/Cellar/aws-sam-cli/1.78.0/libexec/lib/python3.11/site-packages/flask/app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/aws-sam-cli/1.78.0/libexec/lib/python3.11/site-packages/flask/app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/aws-sam-cli/1.78.0/libexec/lib/python3.11/site-packages/flask/app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/aws-sam-cli/1.78.0/libexec/lib/python3.11/site-packages/flask/app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/aws-sam-cli/1.78.0/libexec/lib/python3.11/site-packages/samcli/local/apigw/local_apigw_service.py", line 364, in _request_handler
    self.lambda_runner.invoke(route.function_name, event, stdout=stdout_stream_writer, stderr=self.stderr)
  File "/usr/local/Cellar/aws-sam-cli/1.78.0/libexec/lib/python3.11/site-packages/samcli/commands/local/lib/local_lambda.py", line 113, in invoke
    function = self.provider.get(function_identifier)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/aws-sam-cli/1.78.0/libexec/lib/python3.11/site-packages/samcli/lib/providers/sam_function_provider.py", line 118, in get
    raise ValueError("Function name is required")
ValueError: Function name is required
2023-04-05 14:42:40 127.0.0.1 - - [05/Apr/2023 14:42:40] "GET / HTTP/1.1" 502 -

template.yamñ

  RootMethodGet:
    Type: AWS::ApiGateway::Method
    Properties:
      RestApiId: !Ref AppApi
      ResourceId: !GetAtt AppApi.RootResourceId
      HttpMethod: GET
      AuthorizationType: NONE
      Integration:
        Type: MOCK
        RequestTemplates:
          application/json: '{"statusCode": 200}'
        IntegrationResponses:
          - StatusCode: 200
        PassthroughBehavior: WHEN_NO_MATCH
      MethodResponses:
        - StatusCode: 200
          ResponseModels:
            application/json: Empty

islasjuanp avatar Apr 05 '23 17:04 islasjuanp

Hi @islasjuanp thanks for reaching out! Based on your use case I would suggest on using a AWS::Serverless::Api resource with a simple AWS::Serverless::Function with aws_proxy integration like this template:

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31

Globals:
  Function:
    Timeout: 10

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: function/
      Handler: app.lambda_handler
      Runtime: python3.7
  HelloWorldApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      DefinitionBody:
        swagger: '2.0'
        info:
          title: Example
          version: '1'
        paths:
          /test:
            get:
              x-amazon-apigateway-integration:
                httpMethod: GET
                type: aws_proxy
                uri: !Sub "${HelloWorldFunction.Arn}"

Or you can more simply write a AWS::Serverless::Function with a simple API event: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-eventsource.html. As far as I know local start-api is designated for invoking lambdas through API locally, and does not yet have ApiGateway Integration support of mock type.

qingchm avatar Apr 06 '23 12:04 qingchm

Hi, @qingchm. Thanks for your response.

I was able to create a function with a simple API Event, but I was interested in testing a mock resource in my local environment before deploying it to AWS. It would be nice to have this functionality in the future.

islasjuanp avatar Apr 06 '23 14:04 islasjuanp

Got it, sounds good! Then I'll leave this issue as a feature request for the team to consider then! Thanks for the suggestion here

qingchm avatar Apr 06 '23 16:04 qingchm

@islasjuanp Would you mind if I modify the title of this issue so it's easier to be found?

qingchm avatar Apr 06 '23 16:04 qingchm

Sure, @qingchm. It would be helpful for the community.

islasjuanp avatar Apr 06 '23 19:04 islasjuanp

any update on this? I also have a Mock Integration I would like to test locally and it seems this is a current limitation in the SAM CLI local command

victorekpo avatar Aug 02 '24 00:08 victorekpo