LoginSignup
2
0

More than 3 years have passed since last update.

Serverlessフレームワークで${aws:username}を使う

Posted at

Serverlessフレームワークで${aws:username}のようなIAM ポリシーエレメント(IAM Policy Elements)を使用するとエラーとなります。


Invalid variable reference syntax for variable aws:username. You can only reference env vars, options, & files. You can check our docs for more info.

ServerlessのValuablesと記法が被っているからですね。

対処法

以下のようにprovidervariableSyntaxを設定して、ServerlessのValuablesの記法を変更する必要があります。

provider:
  name: aws
  runtime: python3.8
  region: ap-northeast-1
  variableSyntax: "\\${{([ ~:a-zA-Z0-9._\\'\",\\-\\/\\(\\)]+?)}}"

これにより、ServerlessのValuablesは${{xxx}}という記法に変わります。これでIAMの ポリシーエレメントが ${...}の記法で使えるようになりました。
が、これだけでは今度はCloudFormationでエラーとなります。


Error: The CloudFormation template is invalid: Template format error: Unresolved resource dependencies [aws:username] in the Resources block of the template

この場合は↓のようにエスケープする必要があります。

  • ${aws:username} -> ${!aws:username}

以上で無事、IAM ポリシーエレメントが使用可能となりました。
2重に対応しないといけないのは厄介ですね。Serverless側でどうにかできないかというissueも立っていますね

環境

  • Serverless: 1.76.1
2
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
2
0