Function with 'Fn::Transform' as first events causes 'sam local start-api' to crash
See original GitHub issueDescription:
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:
- 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"
- 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:
- Created 4 years ago
- Reactions:3
- Comments:9 (1 by maintainers)
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:
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 theintrinsic_property_resolver.py
. This is the Cloudformation part: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.
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.