LoginSignup
1
0

AWS SAMのテンプレートでFunctionのCode Uriに!Refや!Subを使えない時の解決法

Last updated at Posted at 2024-03-12

はじめに

AWS SAMのテンプレートでFunctionのCode Uriに!Subパラメーターを指定してデプロイしようとしたところ、下記のようなエラーが発生してデプロイに失敗しました。

Error
Error: Failed to create changeset for the stack: my-stack, ex: Waiter 
ChangeSetCreateComplete failed: Waiter encountered a terminal failure state: 
For expression "Status" we matched expected path: "FAILED" Status: FAILED.
Reason: Transform AWS::Serverless-2016-10-31 failed with: Invalid Serverless
Application Specification document. Number of errors found: 1. Resource with
id [MyFunction] is invalid. 'CodeUri' requires Bucket and Key properties to
be specified.

原因を調査したところSAM固有の問題で、AWS::Serverless::FunctionのPathパラメーターに!Ref!Subが使えないようでした。
なんとか解決できないか試行錯誤した結果、やり方がわかったので備忘録として残します。

方法

解決策としては、

  • CodeUriに直接S3のURLを!Ref!Subで指定せずBucketKeyを設定する
    です。

簡単な例を出します。

まず、ダメな例。

Function:
    Type: AWS::Serverless::Function
    Properties:
        FunctionName: example-fucntion
        Handler: bootstrap
        Runtime: provided.al2
        CodeUri: !Sub s3://${bucket}/function.zip

良い例。

Function:
    Type: AWS::Serverless::Function
    Properties:
        FunctionName: example-fucntion
        Handler: bootstrap
        Runtime: provided.al2
        CodeUri: 
            Bucket: !Ref bucket
            Key: function.zip

これで、エラーの問題が解決できます。
以上です。

もちろん、Bucketブロックで!Sub!ImportValueの設定も可能です。

CodeUri: 
    Bucket: !ImportValue example-bucket
    Key: function.zip

さいごに

誰かの参考になれば嬉しいです。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0