LoginSignup
10
4

More than 5 years have passed since last update.

CloudFormationで変数の埋め込みはしたいが、IAMポリシ-変数を使いたい場合

Last updated at Posted at 2017-10-05

例えばこんな感じにユーザ自身しかパスワード変更させないようにする場合を考えてみます。

Type: "AWS::IAM::ManagedPolicy"
Properties:
  ManagedPolicyName: IAMChangePassword
  Description: "パスワード変更"
  PolicyDocument:
    Version: "2012-10-17"
    Statement:
      -
        Effect: Allow
        Action:
          - iam:ChangePassword
        Resource: !Sub arn:aws:iam::${AWS::AccountId}:user/${aws:username}

このままCloudFormationで実行しようとすると、テンプレートの検証エラーが出てしまいます。

Template format error: Unresolved resource dependencies [aws:username] in the Resources block of the template

どうすればよいのかはStackOverFlowに書いてありました。

${!}を使って、以下のようにやるとうまくいきました。

Type: "AWS::IAM::ManagedPolicy"
Properties:
  ManagedPolicyName: IAMChangePassword
  Description: "パスワード変更"
  PolicyDocument:
    Version: "2012-10-17"
    Statement:
      -
        Effect: Allow
        Action:
          - iam:ChangePassword
        Resource: !Sub arn:aws:iam::${AWS::AccountId}:user/${!aws:username}
10
4
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
10
4