📑

【AWS Service Catalog】新規アカウント作成を自動化+SSOで設定してみた

に公開

はじめに

サービスなどをSaaS化する際に、お客様ごとにアカウントを作成し、そのアカウントにアプリケーションをデプロイする構成はよくあると思います。
そのアカウント作成をService Catalogを使用して、自動化できないかと思い、検証してみました。
また、それらのアカウントへはIAM Identity Centerを使用して、SSOでログインできるようにしてみました。

前提条件

  • 管理アカウントにログインできること
    • 今回はAdministratorAccessポリシーを付与しています
  • Organizationsが設定してあること
  • IAM Identity Centerを東京リージョンで有効化してあること

実装

Service Catalogで製品を作成

Service Catalogの設定方法はこちらをご覧ください。
https://zenn.dev/t_oishi/articles/26a1871037c8b9

今回は以下のような7つのパラメータを作成しています。

  • 新規アカウントの名前
    • AccountName: "開発環境アカウント"
  • アカウント用のメールアドレス
    • AccountEmail: "aws-dev-team@example.com"
  • アカウントを配置するOU(オプション)
    • OrganizationalUnitId: "ou-abc1-23456789"
  • IAM Identity CenterのインスタンスARN
    • IdentityCenterInstanceArn: "arn:aws:sso:::instance/ssoins-1234567890abcdef"
  • 割り当てる権限セットのARN
    • PermissionSetArn: "arn:aws:sso:::permissionSet/ssoins-1234567890abcdef/ps-abcdef1234567890"
  • アクセスを付与する対象のタイプ
    • PrincipalType: "User/GROUP"
  • ユーザーIDまたはグループID
    • PrincipalId: "xxxxx-e5f6-7890-abcd-ef1234567890"

create-account-parameter.yml
AWSTemplateFormatVersion: '2010-09-09'
Description: 'AWS Organizations Account Creation with IAM Identity Center Integration via Service Catalog'

Parameters:
  AccountName:
    Type: String
    Description: 'Name of the new AWS account'
    MinLength: 1
    MaxLength: 50
    AllowedPattern: '[\w\s\-]+'
    ConstraintDescription: 'Account name must contain only alphanumeric characters, spaces, and hyphens'

  AccountEmail:
    Type: String
    Description: 'Email address for the new account (must be unique)'
    AllowedPattern: '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
    ConstraintDescription: 'Must be a valid email address'

  OrganizationalUnitId:
    Type: String
    Description: 'Organizational Unit ID where the account will be created (e.g., ou-xxxx-xxxxxxxx)'
    Default: ''
    AllowedPattern: '^(ou-[a-z0-9]{4,32}-[a-z0-9]{8,32}|r-[a-z0-9]{4,32})?$'

  IdentityCenterInstanceArn:
    Type: String
    Description: 'IAM Identity Center Instance ARN (e.g., arn:aws:sso:::instance/ssoins-xxxxxxxx)'
    AllowedPattern: '^arn:aws:sso:::instance/ssoins-[a-z0-9]{16}$'

  PermissionSetArn:
    Type: String
    Description: 'Permission Set ARN to assign (e.g., arn:aws:sso:::permissionSet/ssoins-xxxxx/ps-xxxxx)'
    AllowedPattern: '^arn:aws:sso:::permissionSet/ssoins-[a-z0-9]{16}/ps-[a-z0-9]{16}$'

  PrincipalType:
    Type: String
    Description: 'Type of principal to assign access'
    Default: 'GROUP'
    AllowedValues:
      - USER
      - GROUP

  PrincipalId:
    Type: String
    Description: 'Principal ID (User ID or Group ID) from IAM Identity Center'
    AllowedPattern: '^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$'
    ConstraintDescription: 'Must be a valid UUID format'

Conditions:
  HasOrganizationalUnit: !Not [!Equals [!Ref OrganizationalUnitId, '']]

Resources:
  NewAccount:
    Type: AWS::Organizations::Account
    Properties:
      AccountName: !Ref AccountName
      Email: !Ref AccountEmail
      ParentIds:
        - !If
          - HasOrganizationalUnit
          - !Ref OrganizationalUnitId
          - !Ref AWS::NoValue
      Tags:
        - Key: ManagedBy
          Value: ServiceCatalog
        - Key: CreatedDate
          Value: !Sub '${AWS::StackName}'
        - Key: Environment
          Value: 'Production'

  # IAM Identity Center アカウント割り当て
  SSOAccountAssignment:
    Type: AWS::SSO::Assignment
    DependsOn: NewAccount
    Properties:
      InstanceArn: !Ref IdentityCenterInstanceArn
      PermissionSetArn: !Ref PermissionSetArn
      PrincipalId: !Ref PrincipalId
      PrincipalType: !Ref PrincipalType
      TargetId: !GetAtt NewAccount.AccountId
      TargetType: AWS_ACCOUNT

Outputs:
  AccountId:
    Description: 'New AWS Account ID'
    Value: !GetAtt NewAccount.AccountId
    Export:
      Name: !Sub '${AWS::StackName}-AccountId'

  AccountName:
    Description: 'Account Name'
    Value: !Ref AccountName
    Export:
      Name: !Sub '${AWS::StackName}-AccountName'

  AccountEmail:
    Description: 'Account Email'
    Value: !Ref AccountEmail

  AssignmentDetails:
    Description: 'IAM Identity Center Assignment Details'
    Value: !Sub |
      Principal Type: ${PrincipalType}
      Principal ID: ${PrincipalId}
      Permission Set: ${PermissionSetArn}
      Target Account: ${NewAccount.AccountId}

  AccessInstructions:
    Description: 'How to access the new account'
    Value: |
      1. Go to your AWS Access Portal URL
      2. Sign in with your IAM Identity Center credentials
      3. Select the new account from the list
      4. Click 'Management console' to access the account

IAM Identity Centerの設定

まずユーザーを作成します。
このユーザーとはどのAWSアカウントをSSOの対象にし、管理・ログインできるかを設定するものです。
https://techblog.ap-com.co.jp/entry/2023/11/12/033453

設定が完了すると、下記のようなメールが送られてきます。
パスワードやMFA設定をしましょう。

そうするとhttps://d-xxxxxx.awsapps.com/start/にアクセスし、ログインができたらSSO画面に移るはずです。

次に、許可セットの設定をします。
これは、SSOでログインする際にどの権限でアクセスできるようにするか設定するものです。
今回はAdministratorAccessを作成しておきました。

これでIAM Identity Centerの設定は完了です。

グループは任意ですが、ユーザーが複数いる場合に作成することで、複数のアカウントを管理することができます。

製品の起動

では先ほど作成した製品を起動してみましょう。

問題なく作成できれば、Organizetionsへも反映され、SSO画面からもログインができると思います。

GitHubで編集を提案

Discussion