0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

CloudFormationで複数のIamユーザーをまとめて作成

Posted at

背景

AWSのIaCのCloudFormationでループ処理ができるようになっていたらしい(2023/07くらい)

AWS Docs

CloudFormationでIamユーザーを複数作る際に似たような処理を何回も記載するのは面倒だったけど、これで一気に作成できる

CloudFormation yml

ymlで記載

AWSTemplateFormatVersion: 2010-09-09
Transform: 'AWS::LanguageExtensions'
Description: Template for creating Initial IAM

Parameters:
  UserNames:
    Type: List<String>
    Default: user01, user02, user03

Resources:
  IAMGrouopInitialTest:
    Type: 'AWS::IAM::Group'
    DeletionPolicy: Retain
    Properties:
      GroupName: "test-user-group-01"
      ManagedPolicyArns:
        - 'arn:aws:iam::aws:policy/ReadOnlyAccess'
  'Fn::ForEach::CreateUserLoopName':
    - username
    - !Ref UserNames
    - 'IAMUserInitial${username}':
        Type: 'AWS::IAM::User'
        DeletionPolicy: Retain
        Properties:
          UserName: !Ref username
          Groups: 
            - "test-user-group-01"

Outputs:
  'Fn::ForEach::OutputUserLoopName':
    - username
    - !Ref UserNames
    - 'UserNames${username}':
        Description: Information about User name
        Value: !Sub "IAMUserInitial${username}"

      'PasswordSettingURL${username}':
        Description: Please create a password at the URL
        Value: !Join 
          - ''
          - - 'https://'
            - console.aws.amazon.com/iam/home?region=
            - !Ref 'AWS::Region'
            - '#/users/'
            - !Ref username
            - '?section=security_credentials'

  LoginURL:
    Description: Information about console login URL
    Value: !Join 
      - ''
      - - 'https://'
        - !Ref 'AWS::AccountId'
        - .signin.aws.amazon.com/console

ポイントは2つ. 1つ目はParametersでstringのlistを作成. ここでユーザー名の一覧を記載.

2つ目はFn::ForEach::CreateUserLoopNameでループを定義.

  'Fn::ForEach::CreateUserLoopName':
    - username
    - !Ref UserNames
    - 'IAMUserInitial${username}':

usernameの部分でループ変数を定義している.

    - username
    - !Ref UserNames

あとはこれを普通cloudformation通りに実行してstackを作成すれば良い.

実行すると、ymlで指定したユーザーグループ、test-user-group-01にユーザーが複数作成されていた.

スクリーンショット 2023-11-06 2.42.14.png

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?