[アップデート] AWS SAM CLI 1.160.0 で CloudFormation Language Extensions(Fn::ForEach)がサポートされたので試してみた

[アップデート] AWS SAM CLI 1.160.0 で CloudFormation Language Extensions(Fn::ForEach)がサポートされたので試してみた

2026.05.15

いわさです。

CloudFormation には AWS::LanguageExtensions トランスフォームという機能があり、Fn::ForEach などの拡張組み込み関数を使うことでテンプレートの記述を大幅に簡略化できます。
例えば Fn::ForEach を使えば、同じ構成のリソースを複数環境分(Dev/Stg/Prod など)ループで定義できます。
Fn::ForEach 自体は 2023 年に CloudFormation でリリースされた機能で、以下の記事で紹介しました。

https://dev.classmethod.jp/articles/cloudformation-foreach/

しかし、SAM CLI はこれまでこの Fn::ForEach の構文を認識できず、sam buildsam validatesam local invoke を実行するとテンプレート解析の段階でエラーになっていました。
Issue としても 2022 年から要望が上がっていた機能です。

https://github.com/aws/aws-sam-cli/issues/4172

これが今回、SAM CLI 1.160.0 で CloudFormation Language Extensions(Fn::ForEach)がついにサポートされました。

https://github.com/aws/aws-sam-cli/releases/tag/v1.160.0

今回こちらを確認してみたので紹介します。

実際に確認してみる

Fn::ForEach を使った SAM テンプレートを用意し、SAM CLI 1.159.1(アップデート前)と 1.160.0(アップデート後)でそれぞれ動作を確認してみました。

検証用テンプレート

以下のテンプレートを使います。
Fn::ForEach で Dev / Stg / Prod の 3 つの Lambda 関数を動的に生成するシンプルな構成です。

template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform:
  - AWS::LanguageExtensions
  - AWS::Serverless-2016-10-31

Description: SAM template using Fn::ForEach to test CloudFormation Language Extensions support

Globals:
  Function:
    Timeout: 10
    Runtime: python3.12
    Handler: app.lambda_handler
    MemorySize: 128

Resources:
  Fn::ForEach::Functions:
    - Env
    - - Dev
      - Stg
      - Prod
    - Hello${Env}Function:
        Type: AWS::Serverless::Function
        Properties:
          CodeUri: src/
          Environment:
            Variables:
              ENV_NAME: ${Env}

SAM CLI 1.159.1(アップデート前)

ちょうど私の環境がひとつ前の v1.159.1 だったので
まず、アップデート前のバージョンで各コマンドを実行してみます。

% sam --version
SAM CLI, version 1.159.1

sam validate

% sam validate
Error: 'list' object has no attribute 'get'

sam build

% sam build
2026-05-14 23:57:23 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/sdk/resource.py", line 26, in __init__
AttributeError: 'list' object has no attribute 'get'

Error: 'list' object has no attribute 'get'

sam local invoke

% sam local invoke HelloDevFunction
2026-05-14 23:57:31 Plugin 'ServerlessAppPlugin' raised an exception: 'list' object has no attribute 'get'

Error: 'list' object has no attribute 'get'

すべてのコマンドで 'list' object has no attribute 'get' エラーが発生しました。
SAM CLI が Resources セクションの各エントリを辞書として処理しようとするのに対し、Fn::ForEach の値はリスト形式 [LoopVar, Collection, Body] であるため、テンプレート解析の段階で即座に失敗しています。

SAM CLI 1.160.0(アップデート後)

SAM CLI を 1.160.0 にアップデートして同じコマンドを実行してみます。

% sam --version
SAM CLI, version 1.160.0

sam validate

% sam validate
/Users/iwasa/work/sam-foreach-test/template.yaml is a valid SAM Template. This is according to basic SAM Validation, for additional validation, please run with "--lint" option

成功しました。

sam build

% sam build
Building codeuri: /Users/iwasa/work/sam-foreach-test/src runtime: python3.12 architecture: x86_64 functions: HelloDevFunction, HelloStgFunction, HelloProdFunction
requirements.txt file not found. Continuing the build without dependencies.
 Running PythonPipBuilder:CopySource

Build Succeeded

Built Artifacts  : .aws-sam/build
Built Template   : .aws-sam/build/template.yaml

Commands you can use next
=========================
[*] Validate SAM template: sam validate
[*] Invoke Function: sam local invoke
[*] Test Function in the Cloud: sam sync --stack-name {{stack-name}} --watch
[*] Deploy: sam deploy --guided

ビルドも成功しました。
Fn::ForEach が展開されて HelloDevFunction、HelloStgFunction、HelloProdFunction の 3 つの関数としてビルドされていることがわかります。

ビルド成果物のディレクトリ構成を確認してみます。

% ls .aws-sam/build/
HelloDevFunction    HelloProdFunction   HelloStgFunction    template.yaml

3 つの関数ディレクトリが生成されていますね。良いですね!

sam local invoke

% sam local invoke HelloDevFunction --no-event
No current session found, using default AWS::AccountId
Invoking app.lambda_handler (python3.12)
Mounting /Users/iwasa/work/sam-foreach-test/.aws-sam/build/HelloDevFunction as /var/task:ro,delegated, inside runtime container

テンプレートの解析が成功し、HelloDevFunction を認識してコンテナの起動まで到達しています。
素晴らしいぞ。

さいごに

本日は AWS SAM CLI 1.160.0 で CloudFormation Language Extensions(Fn::ForEach)がサポートされたので確認してみました。

2022 年から Issue で要望されていた機能がようやく対応されました。このままもうこないと思ってた。
地味に嬉しいです!

この記事をシェアする

AWSのお困り事はクラスメソッドへ

関連記事