複数Lambdaから参照するLambdaレイヤーをExportすると「Export レイヤー名 cannot be updated as it is in use by スタック名」エラーが出た

複数Lambdaから参照するLambdaレイヤーをExportすると「Export レイヤー名 cannot be updated as it is in use by スタック名”」エラーが出た

LambdaレイヤーとLambdaを別々のスタックにしたかったのでLambdaレイヤーをExportに設定してみたら「Export レイヤー名 cannot be updated as it is in use by スタック名」エラー出ました。

AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Resources:
  LambdaLayer:
    Type: "AWS::Serverless::LayerVersion"
    Properties:
      LayerName: layername
      ContentUri: layer/
      CompatibleRuntimes:
        - nodejs18.x
      RetentionPolicy: Delete
Outputs:
  LayerArn:
    Value: !Ref LambdaLayer
    Export:
      Name: layer

Lambdaレイヤーをpackage deployします。

aws cloudformation package --template-file layer.yml --s3-bucket バケット名 --output-template-file layer_out.yml
aws cloudformation deploy --template-file layer_out.yml --stack-name スタック名 --capabilities CAPABILITY_NAMED_IAM

複数Lambdaから参照するLambdaレイヤーをExportすると「Export レイヤー名 cannot be updated as it is in use by スタック名"」エラーが出た

Lambdaからは!ImportValue layerとして参照、デプロイします。この状態で、layerの変更をしてlayerのみデプロイしようとしたら「Export レイヤー名 cannot be updated as it is in use by スタック名」エラーが出てしまいました。

他のスタックが参照しているからLambdaレイヤーの更新でエラーになるみたいです。

↓こちらのスライドシェアが同じことされていました。

結論

仕方ないから1ファイルで書きました。

AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Resources:
  LambdaLayer:
    Type: "AWS::Serverless::LayerVersion"
    Properties:
      LayerName: layername
      ContentUri: layer/
      CompatibleRuntimes:
        - nodejs18.x
      RetentionPolicy: Delete
  TestFunctionA:
    Type: "AWS::Serverless::Function"
    Properties:
      FunctionName: functionname1
      Handler: index.handler
      Runtime: nodejs18.x
      CodeUri: src-a/
      Layers:
        - !Ref LambdaLayer
  TestFunctionB:
    Type: "AWS::Serverless::Function"
    Properties:
      FunctionName: functionname2
      Handler: index.handler
      Runtime: nodejs18.x
      CodeUri: src-b/
      Layers:
        - !Ref LambdaLayer

Globalsセクションを使用したらもう少し短いymlファイルで済むと思います。

AWS SAM テンプレートの Globals セクション - AWS Serverless Application Model
AWS SAM テンプレートの Globals セクションについて学びます。

コメント

タイトルとURLをコピーしました