aws / serverless-application-model

The AWS Serverless Application Model (AWS SAM) transform is a AWS CloudFormation macro that transforms SAM templates into CloudFormation templates.
https://aws.amazon.com/serverless/sam
Apache License 2.0
9.36k stars 2.38k forks source link

Can't use ForEach intrinsic function to define resources #3641

Closed smasset-veolia closed 2 months ago

smasset-veolia commented 2 months ago

Description

The SAM CLI fails to parse templates using the ForEach intrinsic function to define resources. The build first fails in aws-sam-translator code.

Comments in a similar issue reported in the sam-cli project (aws/aws-sam-cli#5647) point to a fix recently merged in AWS CLI (aws/aws-cli#8096). Applying similar changes in both aws-sam-translator (see #3636) and aws-sam-cli on a local test environment fixes the issue.

Steps to reproduce

Run sam build with the example provided in the function reference documentation

Observed result

The command fails in aws-sam-translator code with the following traceback:

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

Expected result

The command shouldn't fail and output:

Build Succeeded

Additional environment details

  1. OS: Debian Bookworm
  2. If using the SAM CLI, sam --version: reproduced with both SAM CLI, version 1.121.0 and SAM CLI, version 1.123.0
  3. AWS region: eu-west-1
aaythapa commented 2 months ago

Hi there, thanks for opening this issue and for contributing to SAM. Seems like a valid request to me, I'll try to look through your PR early next week

jfuss commented 2 months ago

We shouldn't be adding support for Fn::ForEach in this repo and instead should be in SAM CLI. We shouldn't fail the way we do here but that doesn't mean we should be re-implementing it in SAM.

Context: Intrinsics are supported through AWS::LanguageExtensions. This is only available through deployments though and how SAM supports this today.

Instrinsics have caused multiple issues over the year for us and why we now recommend customers to use AWS::LanguageExtensions with SAM now.

The issue here is not that SAM doesn't support Fn::ForEach but that SAM CLI does not support AWS::LanguageExtensions. SAM CLI issue regarding this: https://github.com/aws/aws-sam-cli/issues/5647

aaythapa commented 2 months ago

Agree with Jacob on this, didn't realize that SAM CLI doesn't support Language Extensions and thats what was causing the issue. Closing this issue for now, thanks again for bringing this up. Feel free to re-open if you want to discuss further.

github-actions[bot] commented 2 months ago

This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one.

brianhhq commented 2 months ago

hi @jfuss , as a customer, we would expect it to be working and integrate it well with the feature AWS provide. if this is not the right place, would you please raise the request in the right place and put a link here?

In addition, if this is right "Instrinsics have caused multiple issues over the year for us", why hasn't it been resolved or in the backlog to resolve.

when I was notified that cloud formation support for loop feature, I was super happy and write article to celebrate it. but I was disappointed that one of my favourite tool - sam does not work well with it.

if this is not going into the right direction, how would we recommend sam over terraform to others/clients

aaythapa commented 2 months ago

Hi @brianhhq https://github.com/aws/aws-sam-cli/issues/5647 this feature request to support Language Extensions in SAM CLI would be the right request to use as the issue is with the sam build command rather than the actual transform