CDKを使ってグループとユーザーを作成する方法

サンプル

参考

例 lib/user-stack.ts

import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { Group, ManagedPolicy, User } from 'aws-cdk-lib/aws-iam';

const listUser = [ 'toyota', 'honda' ];

export class UserStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
    this.addUsers();
  }

  private addUsers() {
    const groupName = 'abc-admin';
    const group = new Group(this, groupName, {
      groupName,
      managedPolicies: [
        ManagedPolicy.fromAwsManagedPolicyName('AdministratorAccess')
      ],
    });
    listUser.forEach((uname) => {
      new User(this, uname, {
        userName: uname,
        groups: [group]
      });
    })
  }
}