Skip to content

Conversation

@smasset-veolia
Copy link

@smasset-veolia smasset-veolia commented Aug 14, 2024

Inspired by aws/aws-cli#8096.

Issue #, if available #3641

Description of changes

The Fn::ForEach intrinsic function requires to add array entries in the Resources section of the SAM template. Parts of the code only expect to find object entries keyed with a string and fail with errors like 'list' object has no attribute 'get'.

This PR brings similar changes to those merged in the aws-cli to the aws-sam-translator code used by aws-sam-cli (replacing the array with the resource fragment provided with the Fn::ForEach loop definition).

Description of how you validated changes

On top of adding more local tests I've run sam build and sam validate commands on a local environment with edited versions of aws-sam-translator and aws-sam-cli pip packages.

I've replaced calls to resource_dict.get("Type") or resource_dict.get("Metadata") with the proposed code changes in each location where the commands would fail until they passed.

Checklist

Examples?

Please reach out in the comments if you want to add an example. Examples will be
added to sam init through aws/aws-sam-cli-app-templates.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@smasset-veolia smasset-veolia requested a review from a team as a code owner August 14, 2024 16:17
@smasset-veolia
Copy link
Author

Rebased on latest develop branch

@jfuss
Copy link
Contributor

jfuss commented Aug 14, 2024

@smasset-veolia If the goal is to bring this to SAM CLI, this is the wrong repo for that. You can find SAM CLI's repo: https://github.com/aws/aws-sam-cli

This repo is specifically for SAM's conversion from SAM's Spec to raw CloudFormation Resources. SAM Supports Fn::ForEach and other intrinsics though the LanguageExtension CloudFormation Provides.

Please make sure to submit an Issue first before submitting PRs so we can discuss changes before time is put into editting the code.

Closing this for now, if you believe there is something deeper the Spec needs to support, please create an Issue first.

@jfuss jfuss closed this Aug 14, 2024
@smasset-veolia
Copy link
Author

smasset-veolia commented Aug 17, 2024

@jfuss as you know the aws-sam-cli Pypi package also depends on aws-sam-translator.

If you look at the logs of the examples shared in the linked issue aws/aws-sam-cli#5647, you'll see the first place the code fails is actually in samtranslator code which is updated in this PR. So I need a released version of aws-sam-translator before opening another PR on aws-sam-cli itself.

Here's an abbreviated version of the traceback log from the last comment in aws/aws-sam-cli#5647.

Starting Build use cache                                                                                                                   
2024-07-05 08:13:52 Plugin 'ServerlessAppPlugin' raised an exception: 'list' object has no attribute 'get'
Traceback (most recent call last):
  File "samtranslator/plugins/sam_plugins.py", line 130, in act
  File "samtranslator/metrics/method_decorator.py", line 116, in wrapper_cw_timer
  File "samtranslator/plugins/application/serverless_app_plugin.py", line 124, in on_before_transform_template
  File "samtranslator/sdk/template.py", line 34, in iterate
  File "samtranslator/sdk/resource.py", line 26, in __init__
AttributeError: 'list' object has no attribute 'get'

Error: 'list' object has no attribute 'get'
Traceback:
  File "click/core.py", line 1055, in main
  File "click/core.py", line 1657, in invoke
[...]
  File "samcli/lib/samlib/wrapper.py", line 73, in run_plugins
  File "samcli/lib/samlib/wrapper.py", line 130, in parse
  File "samtranslator/plugins/sam_plugins.py", line 136, in act
  File "samtranslator/plugins/sam_plugins.py", line 130, in act
  File "samtranslator/metrics/method_decorator.py", line 116, in wrapper_cw_timer
  File "samtranslator/plugins/application/serverless_app_plugin.py", line 124, in on_before_transform_template
  File "samtranslator/sdk/template.py", line 34, in iterate
  File "samtranslator/sdk/resource.py", line 26, in __init__

An unexpected error was encountered while executing "sam build".
Search for an existing issue:
https://github.com/aws/aws-sam-cli/issues?q=is%3Aissue+is%3Aopen+Bug%3A%20sam%20build%20-%20AttributeError
Or create a bug report:
https://github.com/aws/aws-sam-cli/issues/new?template=Bug_report.md&title=Bug%3A%20sam%20build%20-%20AttributeError

If needed, I can open a specific issue in this repo too.

Please reopen and review the PR.

@smasset-veolia
Copy link
Author

smasset-veolia commented Aug 17, 2024

Rebased against latest develop branch after new release.

@GavinZZ
Copy link
Member

GavinZZ commented Aug 19, 2024

@smasset-veolia can you please first create an issue first and link this PR in the issue for discussion?

@smasset-veolia
Copy link
Author

@smasset-veolia can you please first create an issue first and link this PR in the issue for discussion?

Done. See #3641

@smasset-veolia
Copy link
Author

smasset-veolia commented Aug 23, 2024

@GavinZZ @jfuss, anything missing from #3641 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants