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.47k stars 1.16k forks source link

local start-api cannot accept path parameters including - (dash) #3516

Open takanabe opened 3 years ago

takanabe commented 3 years ago

Describe your idea/feature/enhancement

I encountered the following error when I executed sam local start-api

> sam local start-api

Traceback (most recent call last):
  File "/usr/local/bin/sam", line 33, in <module>
    sys.exit(load_entry_point('aws-sam-cli==1.11.0', 'console_scripts', 'sam')())
  File "/usr/local/Cellar/aws-sam-cli/1.11.0/libexec/lib/python3.8/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/Cellar/aws-sam-cli/1.11.0/libexec/lib/python3.8/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/usr/local/Cellar/aws-sam-cli/1.11.0/libexec/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/Cellar/aws-sam-cli/1.11.0/libexec/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/Cellar/aws-sam-cli/1.11.0/libexec/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/Cellar/aws-sam-cli/1.11.0/libexec/lib/python3.8/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/Cellar/aws-sam-cli/1.11.0/libexec/lib/python3.8/site-packages/click/decorators.py", line 73, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "/usr/local/Cellar/aws-sam-cli/1.11.0/libexec/lib/python3.8/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/Cellar/aws-sam-cli/1.11.0/libexec/lib/python3.8/site-packages/samcli/lib/telemetry/metrics.py", line 148, in wrapped
    raise exception  # pylint: disable=raising-bad-type
  File "/usr/local/Cellar/aws-sam-cli/1.11.0/libexec/lib/python3.8/site-packages/samcli/lib/telemetry/metrics.py", line 114, in wrapped
    return_value = func(*args, **kwargs)
  File "/usr/local/Cellar/aws-sam-cli/1.11.0/libexec/lib/python3.8/site-packages/samcli/commands/local/start_api/cli.py", line 72, in cli
    do_cli(
  File "/usr/local/Cellar/aws-sam-cli/1.11.0/libexec/lib/python3.8/site-packages/samcli/commands/local/start_api/cli.py", line 148, in do_cli
    service.start()
  File "/usr/local/Cellar/aws-sam-cli/1.11.0/libexec/lib/python3.8/site-packages/samcli/commands/local/lib/local_api_service.py", line 70, in start
    service.create()
  File "/usr/local/Cellar/aws-sam-cli/1.11.0/libexec/lib/python3.8/site-packages/samcli/local/apigw/local_apigw_service.py", line 159, in create
    self._app.add_url_rule(
  File "/usr/local/Cellar/aws-sam-cli/1.11.0/libexec/lib/python3.8/site-packages/flask/app.py", line 98, in wrapper_func
    return f(self, *args, **kwargs)
  File "/usr/local/Cellar/aws-sam-cli/1.11.0/libexec/lib/python3.8/site-packages/flask/app.py", line 1278, in add_url_rule
    self.url_map.add(rule)
  File "/usr/local/Cellar/aws-sam-cli/1.11.0/libexec/lib/python3.8/site-packages/werkzeug/routing.py", line 1482, in add
    rule.bind(self)
  File "/usr/local/Cellar/aws-sam-cli/1.11.0/libexec/lib/python3.8/site-packages/werkzeug/routing.py", line 767, in bind
    self.compile()
  File "/usr/local/Cellar/aws-sam-cli/1.11.0/libexec/lib/python3.8/site-packages/werkzeug/routing.py", line 836, in compile
    _build_regex(self.rule if self.is_leaf else self.rule.rstrip("/"))
  File "/usr/local/Cellar/aws-sam-cli/1.11.0/libexec/lib/python3.8/site-packages/werkzeug/routing.py", line 803, in _build_regex
    for converter, arguments, variable in parse_rule(rule):
  File "/usr/local/Cellar/aws-sam-cli/1.11.0/libexec/lib/python3.8/site-packages/werkzeug/routing.py", line 228, in parse_rule
    raise ValueError("malformed url rule: %r" % rule)
ValueError: malformed url rule: '/users/<something-name>/resources'

This is because I use /users/{something-name}/resources as an API GW path parameter and Flask/werkzeung cannot accept path parameter names including -.

# portion of SAM template

[snip]
  SomethingManagerFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !FindInMap [ EnvironmentMap, !Ref Environment, SomethingManagerFunctionName]
      CodeUri: .
      Handler: something-manager
      Role: !GetAtt SomethingManagerFunctionRole.Arn
      Runtime: go1.x
      Events:
        ListSomethings:
          Type: Api
          Properties:
            Path: /somethings
            Method: GET
            RestApiId:
              Ref: SomethingApiGateway
        ListUsersInSomething:
          Type: Api
          Properties:
            Path: /users/{something-name}/resources
            Method: GET
            RestApiId:
              Ref: SomethingApiGateway
[snip]

I could not notice that I cannot use path parameters except for /[a-zA-Z_][a-zA-Z0-9_]*/ until I read the code and documents of Flask and werkzeug. So, I want to clarify these implicit conventions for sam local start-api.

Proposal

Fistly, I want to know if the behavior above is a bug or an intentional design for the sam local start-api command.

Then I want to know how we can make the implicit rule clear. There are several options I can think of:

Additional Details

I plan to push PR based on the feedback from the maintainer of this repo but want to know which is the right direction for this project.

niketh90 commented 2 years ago

Any fix for this?

ssenchenko commented 2 years ago

The team is aware of the request. We do not have an update to share at this time. However, we do tag PRs with issues they solve. Once the team prioritizes and solves it, you will be able to see that link in the issue

dschiavu commented 2 years ago

Once more vote +1 from me to solve this one.

MartinKvL commented 1 year ago

I vote +1 to solve this one.

sansay61 commented 3 months ago

hey, any updates on it?