aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.52k stars 1.17k forks source link

sam local start-api does not work with x-amazon-apigateway-integrations for openapi HttpApis #3039

Open spli opened 3 years ago

spli commented 3 years ago

Description:

When using an open api definition for a AWS::Serverless::HttpApi, the documentation says you can declare x-amazon-apigateway-integrations components, which you can then refer to in paths. This works when deployed using Cloudformation or if just declaring the x-amazon-apigateway-integration without using the reference but doing so inline, but when run using sam local start-api there is an error of Error: Template does not have any APIs connected to Lambda functions

Steps to reproduce:

Based on the standard HelloWorld template.

Given template.yaml:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  test-app

  Sample SAM Template for test-app

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3

Resources:
  HelloWorldApi:
    Type: AWS::Serverless::HttpApi
    Properties:
      FailOnWarnings: True
      DefinitionBody:
        Fn::Transform:
          Name: AWS::Include
          Parameters:
            Location: api.yaml

  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.8

Outputs:
  # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
  # Find out more about other implicit resources you can reference within SAM
  # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
  HelloWorldApi:
    Description: "API Gateway endpoint URL for Prod stage for Hello World function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
  HelloWorldFunction:
    Description: "Hello World Lambda Function ARN"
    Value: !GetAtt HelloWorldFunction.Arn
  HelloWorldFunctionIamRole:
    Description: "Implicit IAM Role created for Hello World function"
    Value: !GetAtt HelloWorldFunctionRole.Arn

and the included api.yaml

openapi: 3.0.1
info:
  title: test  
  version: 1.0.0
paths:
  /hello:
    get:      
      x-amazon-apigateway-integration:
        $ref: '#/components/x-amazon-apigateway-integrations/test'

components:
  x-amazon-apigateway-integrations:
    test:
      type: aws_proxy
      httpMethod: POST
      uri:
        Fn::Sub: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${EvServiceFunction.Arn}/invocations"
      passthroughBehavior: when_no_templates
      payloadFormatVersion: 2.0

Observed result:

2021-07-13 11:52:45,747 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics 2021-07-13 11:52:45,747 | Using config file: samconfig.toml, config environment: default 2021-07-13 11:52:45,747 | Expand command line arguments to: 2021-07-13 11:52:45,747 | --template_file=/Users/simonfrost/Projects/EM/test-app/.aws-sam/build/template.yaml --host=127.0.0.1 --port=3000 --static_dir=public --layer_cache_basedir=/Users/simonfrost/.aws-sam/layers-pkg --container_host=localhost --container_host_interface=127.0.0.1 2021-07-13 11:52:45,827 | local start-api command is called 2021-07-13 11:52:45,829 | No Parameters detected in the template 2021-07-13 11:52:45,844 | Unable to resolve property DefinitionBody: OrderedDict([('Fn::Transform', OrderedDict([('Name', 'AWS::Include'), ('Parameters', OrderedDict([('Location', '../../api.yaml')]))]))]). Leaving as is. 2021-07-13 11:52:45,844 | 2 stacks found in the template 2021-07-13 11:52:45,844 | No Parameters detected in the template 2021-07-13 11:52:45,855 | Unable to resolve property DefinitionBody: OrderedDict([('Fn::Transform', OrderedDict([('Name', 'AWS::Include'), ('Parameters', OrderedDict([('Location', '../../api.yaml')]))]))]). Leaving as is. 2021-07-13 11:52:45,855 | 2 resources found in the stack 2021-07-13 11:52:45,855 | No Parameters detected in the template 2021-07-13 11:52:45,866 | Unable to resolve property DefinitionBody: OrderedDict([('Fn::Transform', OrderedDict([('Name', 'AWS::Include'), ('Parameters', OrderedDict([('Location', '../../api.yaml')]))]))]). Leaving as is. 2021-07-13 11:52:45,866 | Found Serverless function with name='HelloWorldFunction' and CodeUri='HelloWorldFunction' 2021-07-13 11:52:45,866 | --base-dir is not presented, adjusting uri HelloWorldFunction relative to /Users/simonfrost/Projects/EM/test-app/.aws-sam/build/template.yaml 2021-07-13 11:52:45,929 | No Parameters detected in the template 2021-07-13 11:52:45,943 | Unable to resolve property DefinitionBody: OrderedDict([('Fn::Transform', OrderedDict([('Name', 'AWS::Include'), ('Parameters', OrderedDict([('Location', '../../api.yaml')]))]))]). Leaving as is. 2021-07-13 11:52:45,943 | No Parameters detected in the template 2021-07-13 11:52:45,955 | Unable to resolve property DefinitionBody: OrderedDict([('Fn::Transform', OrderedDict([('Name', 'AWS::Include'), ('Parameters', OrderedDict([('Location', '../../api.yaml')]))]))]). Leaving as is. 2021-07-13 11:52:45,955 | Successfully parsed location from AWS::Include transform: ../../api.yaml 2021-07-13 11:52:45,955 | Trying to download Swagger from ../../api.yaml 2021-07-13 11:52:45,955 | Reading Swagger document from local file at /Users/simonfrost/Projects/EM/test-app/.aws-sam/build/../../api.yaml 2021-07-13 11:52:45,957 | Lambda function integration not found in Swagger document at path='/hello' method='get' 2021-07-13 11:52:45,957 | Found '0' APIs in resource 'HelloWorldApi' 2021-07-13 11:52:45,957 | Found '0' API Events in Serverless function with name 'HelloWorldFunction' 2021-07-13 11:52:45,957 | Removed duplicates from '0' Explicit APIs and '0' Implicit APIs to produce '0' APIs 2021-07-13 11:52:45,957 | 0 APIs found in the template 2021-07-13 11:52:45,971 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': '0ee696b7-bbd2-42ed-bdba-646467ec9e61', 'installationId': '3b4bad08-61b9-4fd0-bb9c-b0be2c98369b', 'sessionId': '36744068-94a1-4ee8-9187-d4f02ef9b6b7', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.8.11', 'samcliVersion': '1.26.0', 'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam local start-api', 'duration': 224, 'exitReason': 'NoApisDefined', 'exitCode': 1}}]} 2021-07-13 11:52:46,585 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1) Error: Template does not have any APIs connected to Lambda functions

Expected result:

If you declare it inline it works as expected.

e.g. for inline

openapi: 3.0.1
info:
  title: test  
  version: 1.0.0
paths:
  /hello:
    get:      
      x-amazon-apigateway-integration:
        type: aws_proxy
        httpMethod: POST
        uri:
          Fn::Sub: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${EvServiceFunction.Arn}/invocations"
        passthroughBehavior: when_no_templates
        payloadFormatVersion: 2.0

produces

2021-07-13 11:54:15,030 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2021-07-13 11:54:15,030 | Using config file: samconfig.toml, config environment: default
2021-07-13 11:54:15,030 | Expand command line arguments to:
2021-07-13 11:54:15,030 | --template_file=/Users/simonfrost/Projects/EM/test-app/.aws-sam/build/template.yaml --host=127.0.0.1 --port=3000 --static_dir=public --layer_cache_basedir=/Users/simonfrost/.aws-sam/layers-pkg --container_host=localhost --container_host_interface=127.0.0.1 
2021-07-13 11:54:15,098 | local start-api command is called
2021-07-13 11:54:15,101 | No Parameters detected in the template
2021-07-13 11:54:15,115 | Unable to resolve property DefinitionBody: OrderedDict([('Fn::Transform', OrderedDict([('Name', 'AWS::Include'), ('Parameters', OrderedDict([('Location', '../../api.yaml')]))]))]). Leaving as is.
2021-07-13 11:54:15,115 | 2 stacks found in the template
2021-07-13 11:54:15,116 | No Parameters detected in the template
2021-07-13 11:54:15,131 | Unable to resolve property DefinitionBody: OrderedDict([('Fn::Transform', OrderedDict([('Name', 'AWS::Include'), ('Parameters', OrderedDict([('Location', '../../api.yaml')]))]))]). Leaving as is.
2021-07-13 11:54:15,131 | 2 resources found in the stack 
2021-07-13 11:54:15,131 | No Parameters detected in the template
2021-07-13 11:54:15,144 | Unable to resolve property DefinitionBody: OrderedDict([('Fn::Transform', OrderedDict([('Name', 'AWS::Include'), ('Parameters', OrderedDict([('Location', '../../api.yaml')]))]))]). Leaving as is.
2021-07-13 11:54:15,144 | Found Serverless function with name='HelloWorldFunction' and CodeUri='HelloWorldFunction'
2021-07-13 11:54:15,145 | --base-dir is not presented, adjusting uri HelloWorldFunction relative to /Users/simonfrost/Projects/EM/test-app/.aws-sam/build/template.yaml
2021-07-13 11:54:15,163 | No Parameters detected in the template
2021-07-13 11:54:15,174 | Unable to resolve property DefinitionBody: OrderedDict([('Fn::Transform', OrderedDict([('Name', 'AWS::Include'), ('Parameters', OrderedDict([('Location', '../../api.yaml')]))]))]). Leaving as is.
2021-07-13 11:54:15,174 | No Parameters detected in the template
2021-07-13 11:54:15,183 | Unable to resolve property DefinitionBody: OrderedDict([('Fn::Transform', OrderedDict([('Name', 'AWS::Include'), ('Parameters', OrderedDict([('Location', '../../api.yaml')]))]))]). Leaving as is.
2021-07-13 11:54:15,184 | Successfully parsed location from AWS::Include transform: ../../api.yaml
2021-07-13 11:54:15,184 | Trying to download Swagger from ../../api.yaml
2021-07-13 11:54:15,184 | Reading Swagger document from local file at /Users/simonfrost/Projects/EM/test-app/.aws-sam/build/../../api.yaml
2021-07-13 11:54:15,186 | Resolved Sub intrinsic function: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:123456789012:function:EvServiceFunction/invocations
2021-07-13 11:54:15,186 | Extracted Function ARN: arn:aws:lambda:${AWS::Region}:123456789012:function:EvServiceFunction
2021-07-13 11:54:15,186 | Found '1' APIs in resource 'HelloWorldApi'
2021-07-13 11:54:15,186 | Found '0' API Events in Serverless function with name 'HelloWorldFunction'
2021-07-13 11:54:15,186 | Removed duplicates from '1' Explicit APIs and '0' Implicit APIs to produce '1' APIs
2021-07-13 11:54:15,186 | 1 APIs found in the template
2021-07-13 11:54:15,199 | Mounting EvServiceFunction at http://127.0.0.1:3000/hello [GET]
2021-07-13 11:54:15,199 | You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template
2021-07-13 11:54:15,200 | Localhost server is starting up. Multi-threading = True
2021-07-13 11:54:15  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)2021-07-13 11:54:15,030 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2021-07-13 11:54:15,030 | Using config file: samconfig.toml, config environment: default
2021-07-13 11:54:15,030 | Expand command line arguments to:
2021-07-13 11:54:15,030 | --template_file=/Users/simonfrost/Projects/EM/test-app/.aws-sam/build/template.yaml --host=127.0.0.1 --port=3000 --static_dir=public --layer_cache_basedir=/Users/simonfrost/.aws-sam/layers-pkg --container_host=localhost --container_host_interface=127.0.0.1 
2021-07-13 11:54:15,098 | local start-api command is called
2021-07-13 11:54:15,101 | No Parameters detected in the template
2021-07-13 11:54:15,115 | Unable to resolve property DefinitionBody: OrderedDict([('Fn::Transform', OrderedDict([('Name', 'AWS::Include'), ('Parameters', OrderedDict([('Location', '../../api.yaml')]))]))]). Leaving as is.
2021-07-13 11:54:15,115 | 2 stacks found in the template
2021-07-13 11:54:15,116 | No Parameters detected in the template
2021-07-13 11:54:15,131 | Unable to resolve property DefinitionBody: OrderedDict([('Fn::Transform', OrderedDict([('Name', 'AWS::Include'), ('Parameters', OrderedDict([('Location', '../../api.yaml')]))]))]). Leaving as is.
2021-07-13 11:54:15,131 | 2 resources found in the stack 
2021-07-13 11:54:15,131 | No Parameters detected in the template
2021-07-13 11:54:15,144 | Unable to resolve property DefinitionBody: OrderedDict([('Fn::Transform', OrderedDict([('Name', 'AWS::Include'), ('Parameters', OrderedDict([('Location', '../../api.yaml')]))]))]). Leaving as is.
2021-07-13 11:54:15,144 | Found Serverless function with name='HelloWorldFunction' and CodeUri='HelloWorldFunction'
2021-07-13 11:54:15,145 | --base-dir is not presented, adjusting uri HelloWorldFunction relative to /Users/simonfrost/Projects/EM/test-app/.aws-sam/build/template.yaml
2021-07-13 11:54:15,163 | No Parameters detected in the template
2021-07-13 11:54:15,174 | Unable to resolve property DefinitionBody: OrderedDict([('Fn::Transform', OrderedDict([('Name', 'AWS::Include'), ('Parameters', OrderedDict([('Location', '../../api.yaml')]))]))]). Leaving as is.
2021-07-13 11:54:15,174 | No Parameters detected in the template
2021-07-13 11:54:15,183 | Unable to resolve property DefinitionBody: OrderedDict([('Fn::Transform', OrderedDict([('Name', 'AWS::Include'), ('Parameters', OrderedDict([('Location', '../../api.yaml')]))]))]). Leaving as is.
2021-07-13 11:54:15,184 | Successfully parsed location from AWS::Include transform: ../../api.yaml
2021-07-13 11:54:15,184 | Trying to download Swagger from ../../api.yaml
2021-07-13 11:54:15,184 | Reading Swagger document from local file at /Users/simonfrost/Projects/EM/test-app/.aws-sam/build/../../api.yaml
2021-07-13 11:54:15,186 | Resolved Sub intrinsic function: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:123456789012:function:EvServiceFunction/invocations
2021-07-13 11:54:15,186 | Extracted Function ARN: arn:aws:lambda:${AWS::Region}:123456789012:function:EvServiceFunction
2021-07-13 11:54:15,186 | Found '1' APIs in resource 'HelloWorldApi'
2021-07-13 11:54:15,186 | Found '0' API Events in Serverless function with name 'HelloWorldFunction'
2021-07-13 11:54:15,186 | Removed duplicates from '1' Explicit APIs and '0' Implicit APIs to produce '1' APIs
2021-07-13 11:54:15,186 | 1 APIs found in the template
2021-07-13 11:54:15,199 | Mounting EvServiceFunction at http://127.0.0.1:3000/hello [GET]
2021-07-13 11:54:15,199 | You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template
2021-07-13 11:54:15,200 | Localhost server is starting up. Multi-threading = True
2021-07-13 11:54:15  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: macOS 11.3
  2. If using SAM CLI, sam --version: SAM CLI, version 1.26.0
  3. AWS region: eu-west-1

Add --debug flag to any SAM CLI commands you are running

moelasmar commented 3 years ago

thanks @spli .. I can reproduce this issue.