Sub topic 14
Automating with the AWS CLI and CloudFormation
This chapter assumes you know what a permission set and an account assignment are, and that you can run AWS CLI commands against the management account or a delegated administrator account. Here you will script the same setup you built by hand, first with the CLI and then with CloudFormation.
The shape of the automation
Every console action maps to an API call in the sso-admin namespace. Creating a permission set, attaching policies, and creating assignments are separate calls, and the order matters.
Create a permission set with the CLI
Look up the instance ARN once and reuse it. The permission set below has a four-hour session and the AWS managed ReadOnlyAccess policy.
INSTANCE_ARN=$(aws sso-admin list-instances --query 'Instances[0].InstanceArn' --output text)
PS_ARN=$(aws sso-admin create-permission-set \
--instance-arn "$INSTANCE_ARN" \
--name ReadOnlyAuditor \
--description "Read-only access for auditors" \
--session-duration PT4H \
--query 'PermissionSet.PermissionSetArn' --output text)
aws sso-admin attach-managed-policy-to-permission-set \
--instance-arn "$INSTANCE_ARN" \
--permission-set-arn "$PS_ARN" \
--managed-policy-arn arn:aws:iam::aws:policy/ReadOnlyAccessAssign across an OU
An assignment targets one account, not an OU. To cover an OU, loop over its active accounts. This script continues from the previous one and reuses INSTANCE_ARN and PS_ARN.
IDENTITY_STORE_ID=$(aws sso-admin list-instances --query 'Instances[0].IdentityStoreId' --output text)
GROUP_ID=$(aws identitystore get-group-id \
--identity-store-id "$IDENTITY_STORE_ID" \
--alternate-identifier '{"UniqueAttribute":{"AttributePath":"DisplayName","AttributeValue":"Auditors"}}' \
--query 'GroupId' --output text)
for ACCOUNT_ID in $(aws organizations list-accounts-for-parent \
--parent-id ou-xxxx-xxxxxxxx \
--query 'Accounts[?Status==`ACTIVE`].Id' --output text); do
aws sso-admin create-account-assignment \
--instance-arn "$INSTANCE_ARN" \
--target-id "$ACCOUNT_ID" \
--target-type AWS_ACCOUNT \
--permission-set-arn "$PS_ARN" \
--principal-type GROUP \
--principal-id "$GROUP_ID"
doneEach call is asynchronous. It returns a request ID, and you can check progress with describe-account-assignment-creation-status. The loop also does not cover accounts created later, so run it again or trigger it from an Organizations event.
Re-provision after changes
If you change a permission set that is already assigned, the roles in the accounts are not updated until you provision it.
aws sso-admin provision-permission-set \
--instance-arn "$INSTANCE_ARN" \
--permission-set-arn "$PS_ARN" \
--target-type ALL_PROVISIONED_ACCOUNTSThe same setup in CloudFormation
CloudFormation keeps the permission set and its assignments in version control. Deploy this template in the management account or a delegated administrator account.
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
InstanceArn:
Type: String
AuditorsGroupId:
Type: String
TargetAccountId:
Type: String
Resources:
ReadOnlyAuditor:
Type: AWS::SSO::PermissionSet
Properties:
InstanceArn: !Ref InstanceArn
Name: ReadOnlyAuditor
SessionDuration: PT4H
ManagedPolicies:
- arn:aws:iam::aws:policy/ReadOnlyAccess
AuditorsAssignment:
Type: AWS::SSO::Assignment
Properties:
InstanceArn: !Ref InstanceArn
PermissionSetArn: !GetAtt ReadOnlyAuditor.PermissionSetArn
PrincipalId: !Ref AuditorsGroupId
PrincipalType: GROUP
TargetId: !Ref TargetAccountId
TargetType: AWS_ACCOUNTOne AWS::SSO::Assignment resource covers one account, group, and permission set. For many accounts, generate the resources from a list or use a CloudFormation StackSet-style loop in your deployment tooling.
Common errors
AccessDeniedException: you are running in a member account that is not the delegated administrator.- A role in the account still has old permissions: you changed the permission set with the CLI and did not run
provision-permission-set. - A CloudFormation assignment fails: the group ID is wrong. Use the Identity Store group ID, not the display name.
Exercise
You attach a new managed policy to an assigned permission set using the CLI, but users in the target accounts do not see the new permissions. What step is missing, and which command does it?
Show answer
The permission set must be provisioned to the accounts where it is assigned. Run aws sso-admin provision-permission-set with --target-type ALL_PROVISIONED_ACCOUNTS.