question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Function with 'Fn::Transform' as first events causes 'sam local start-api' to crash

See original GitHub issue

Description:

If a template contains a Function, of which the top-most event is a Fn::Transform entry. Then sam local start-api fails to start with something like:

...
File "/usr/local/Cellar/aws-sam-cli/0.21.0/libexec/lib/python3.7/site-packages/samcli/commands/local/lib/sam_api_provider.py", line 191, in extract_routes_from_events
    for _, event in serverless_function_events.items():
AttributeError: 'str' object has no attribute 'items'

This template does not cause any issues with other commands, including sam local invoke, sam package and sam deploy.

Steps to reproduce the issue:

  1. Given the template:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Resources:
  Func1:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri:                      "./"
      Handler:                      "main"
      Runtime:                      "go1.x"
      Events:
        Api1:
          Type: Api
          Properties:
            Path:                   "/func1"
            Method:                 "GET"

  Func2:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri:                      "./"
      Handler:                      "main"
      Runtime:                      "go1.x"
      Events:
        Fn::Transform:
          Name: AWS::Include
          Parameters:
            Location:               "events.yaml"
  1. Run sam local start-api

Observed result: sam local start-api fails to start:

Traceback (most recent call last):
  File "/usr/local/bin/sam", line 11, in <module>
    load_entry_point('aws-sam-cli==0.21.0', 'console_scripts', 'sam')()
  File "/usr/local/Cellar/aws-sam-cli/0.21.0/libexec/lib/python3.7/site-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/Cellar/aws-sam-cli/0.21.0/libexec/lib/python3.7/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/usr/local/Cellar/aws-sam-cli/0.21.0/libexec/lib/python3.7/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/Cellar/aws-sam-cli/0.21.0/libexec/lib/python3.7/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/Cellar/aws-sam-cli/0.21.0/libexec/lib/python3.7/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/Cellar/aws-sam-cli/0.21.0/libexec/lib/python3.7/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/Cellar/aws-sam-cli/0.21.0/libexec/lib/python3.7/site-packages/click/decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "/usr/local/Cellar/aws-sam-cli/0.21.0/libexec/lib/python3.7/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/Cellar/aws-sam-cli/0.21.0/libexec/lib/python3.7/site-packages/samcli/lib/telemetry/metrics.py", line 94, in wrapped
    raise exception  # pylint: disable=raising-bad-type
  File "/usr/local/Cellar/aws-sam-cli/0.21.0/libexec/lib/python3.7/site-packages/samcli/lib/telemetry/metrics.py", line 65, in wrapped
    return_value = func(*args, **kwargs)
  File "/usr/local/Cellar/aws-sam-cli/0.21.0/libexec/lib/python3.7/site-packages/samcli/commands/local/start_api/cli.py", line 60, in cli
    parameter_overrides)  # pragma: no cover
  File "/usr/local/Cellar/aws-sam-cli/0.21.0/libexec/lib/python3.7/site-packages/samcli/commands/local/start_api/cli.py", line 95, in do_cli
    static_dir=static_dir)
  File "/usr/local/Cellar/aws-sam-cli/0.21.0/libexec/lib/python3.7/site-packages/samcli/commands/local/lib/local_api_service.py", line 43, in __init__
    cwd=self.cwd)
  File "/usr/local/Cellar/aws-sam-cli/0.21.0/libexec/lib/python3.7/site-packages/samcli/commands/local/lib/api_provider.py", line 42, in __init__
    self.api = self._extract_api(self.resources)
  File "/usr/local/Cellar/aws-sam-cli/0.21.0/libexec/lib/python3.7/site-packages/samcli/commands/local/lib/api_provider.py", line 71, in _extract_api
    provider.extract_resources(resources, collector, cwd=self.cwd)
  File "/usr/local/Cellar/aws-sam-cli/0.21.0/libexec/lib/python3.7/site-packages/samcli/commands/local/lib/sam_api_provider.py", line 52, in extract_resources
    self._extract_routes_from_function(logical_id, resource, collector)
  File "/usr/local/Cellar/aws-sam-cli/0.21.0/libexec/lib/python3.7/site-packages/samcli/commands/local/lib/sam_api_provider.py", line 172, in _extract_routes_from_function
    self.extract_routes_from_events(logical_id, serverless_function_events, collector)
  File "/usr/local/Cellar/aws-sam-cli/0.21.0/libexec/lib/python3.7/site-packages/samcli/commands/local/lib/sam_api_provider.py", line 191, in extract_routes_from_events
    for _, event in serverless_function_events.items():
AttributeError: 'str' object has no attribute 'items'

Expected result: sam local start-api should process the Fn::Transform entry and run properly.

Or (also acceptable for my use case) sam local start-api should ignore the Fn::Transform entry completely and not crash.

Workarounds: If you add any other event above the Fn::Transform entry, sam local start-api will work. But the included events (events.yaml in this case) do not get picked up by sam local start-api.

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

Resources:

  Func1:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri:                      "./"
      Handler:                      "main"
      Runtime:                      "go1.x"
      Events:
        Api1:
          Type: Api
          Properties:
            Path:                   "/func1"
            Method:                 "GET"

  Func2:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri:                      "./"
      Handler:                      "main"
      Runtime:                      "go1.x"
      Events:
        Schedule2:
          Type: Schedule
          Properties:
            Schedule:               "rate(5 minutes)"
        Fn::Transform:
          Name: AWS::Include
          Parameters:
            Location:               "events.yaml"

Versions:

SAM CLI, version 0.21.0
aws-cli/1.16.210 Python/3.7.4 Darwin/18.7.0 botocore/1.12.200

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
sgatescommented, Sep 25, 2019

I’m having a similar issue with transform used to process a swagger template for an Api Gateway declaration. This works fine in v0.19.0 but breaks after. Little bit of debugging - it seems the parsing of the swagger file doesn’t happen, and it tries to get the resources out of a dict, but it’s a string not a dict so I get a similar error as OP: AttributeError: ‘str’ object has no attribute ‘get’. I can provide more details if needed, but it looks very similar and is also related to a change in the transform.

Edit: For anyone else having this issue that wants to revert and you’re using home-brew try this:

$ brew uninstall was-sam-cli
$ brew install https://raw.githubusercontent.com/aws/homebrew-tap/2160475c2a6d86cb9d5ceea25d4ad791ee908908/Formula/aws-sam-cli.rb
3reactions
joskarkcommented, Sep 25, 2019

I am also having the same issue as @sgates with AWS::Serverless::Api. I tried to debug and I found that the error is somewhere in the intrinsic_property_resolver.py. This is the Cloudformation part:

Api:
    Type: AWS::Serverless::Api
    Properties:
      StageName: !Ref Variable
      Cors: "'*'"
      DefinitionBody:
        Fn::Transform:
          Name: AWS::Include
          Parameters:
            Location: !Ref Swagger

Then i am getting the sanitized version that gives: 'DefinitionBody': 'Swagger' 'StageName': 'Variable'

I found that the change happens here: https://github.com/awslabs/aws-sam-cli/blob/704c1ea94a72d7db9438a61d64da64156d4cbe53/samcli/lib/intrinsic_resolver/intrinsic_property_resolver.py#L217

Below is the output from before and after the sanitation.

original: 'StageName' -> '{'Ref': 'Variable'}'
sanitized: 'StageName' -> 'Variable'

original: 'DefinitionBody' -> 'OrderedDict([('Fn::Transform', OrderedDict([('Name', 'AWS::Include'), ('Parameters', OrderedDict([('Location', {'Ref': 'Swagger'})]))]))])'
sanitized: 'DefinitionBody' -> 'Swagger'

sanitized: 'Properties' -> '{'MethodSettings': [{'LoggingLevel': 'INFO', 'MetricsEnabled': True, 'DataTraceEnabled': True, 'ResourcePath': '/*', 'HttpMethod': '*'}], 'StageName': 'Variable', 'Cors': "'*'", 'DefinitionBody': 'Swagger'}'

SAM version: 0.22.0 Python: 3.7.4

Hope that helps and I will try to continue debugging when I will have some time in case it’s not solved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Function with 'Fn::Transform' as first events causes 'sam local ...
Description: If a template contains a Function, of which the top-most event is a Fn::Transform entry. Then sam local start-api fails to ...
Read more >
sam local start-api - AWS Serverless Application Model
The AWS SAM CLI first tries to locate a template file built using the sam build command, located in the .aws-sam subfolder, and...
Read more >
Develop Lambdas And Debug Them Locally Using SAM
I used to build my functions in a way that I could easily run them locally, and simply wrap that function with the...
Read more >
Using AWS SAM-CLI requires rebuild every time I update the ...
A great solution to this problem is to use the skip-pull-image flag so that Docker will reuse the Lambda runtime. Essentially run: sam...
Read more >
How to setup a Serverless application with AWS SAM and ...
local development that emulates AWS Lambda, and API Gateway via Docker; takes care of blue/green deployments via AWS CodeDeploy; infrastructure ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found