Web application developer
PHP, Yii, MySQL, Node.js - JavaScript, HTML, CSS & SASS - Linux, AWS & serverless
Using multiple AWS accounts within an organisation is an effective way to isolate your AWS resources, and by setting up cross-account IAM roles you can provide convenient and secure access for users of other AWS accounts.
When creating new cloud-based applications on AWS, I like to use sub-accounts to isolate the account and all the resources within it. As an AWS developer this is very useful to isolate one client from another, even to separate development and production environments and for ease of billing and account management.
Using separate AWS accounts and cross-account roles also means you only need a single IAM user, that of your own IAM user on your primary account - and can use this for accessing all AWS accounts to which you have permission, rather than creating new IAM users within each AWS account. You can also force use of MFA tokens for additional security.
This tutorial assumes you're setting up a new sub-account within your own Organisation, but if you skip to Phase 2 you can allow cross-account Role access into existing AWS accounts too.
If using an Organisation:
AccountAccessRole
If creating a standalone AWS account:
If using an existing AWS account as your sub-account:
Start from Phase 2 if your sub account already exists
AccountAccessRole
you created earlier. Alternatively, if you're in a new standalone account or existing AWS account, create a new Role called AccountAccessRole
or {Source Account Name}AccessRole
, something that might help identify the purpose of the Role{SOURCE ACCOUNT ID}
to the 12-digit account ID of the account in which your IAM user is registered. This tells the sub account that it will only trust IAM users from this source account to assume this role. You can enforce MFA whenever an IAM user attempts to assume this role.{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::{SOURCE ACCOUNT ID}:root" }, "Action": "sts:AssumeRole", "Condition": { "Bool": { "aws:MultiFactorAuthPresent": "true" } } } ] }
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "sts:AssumeRole" ], "Resource": "arn:aws:iam::{SUB ACCOUNT ID}:role/{SUB ACCOUNT ACCESS ROLE NAME}" } ] }
Now you can use the Switch Role URL that the AWS console provides to assume the new role in the child account. This URL looks something like:
https://signin.aws.amazon.com/switchrole?roleName={SUB ACCOUNT ROLE NAME}&account={SUB ACCOUNT ID}&displayName={ROLE DISPLAY NAME}
This process also applies if you have a separate AWS account that isn't part of your organisation. You can still create a role with a cross-account Trust Relationship, and allow your primary account IAM users to assume the role in the account.
To use these cross-account roles with the AWS CLI you'll need to add a different style of entry into your .aws/credentials
file:
[default]
aws_access_key_id=AKIA123456
aws_secret_access_key=123SECRET567
[profile-label]
role_arn = arn:aws:iam::{SUB ACCOUNT ID}:role/{SUB ACCOUNT ROLE NAME}
source_profile = {.aws/credentials PROFILE LABEL OF PARENT IAM USER CREDS}
mfa_serial = arn:aws:iam::{PARENT ACCOUNT ID}:mfa/{PARENT IAM USERNAME}
As an example:
[sub-account]
role_arn = arn:aws:iam::111122223333:role/AccountAccessRole
source_profile = default
mfa_serial = arn:aws:iam::444455556666:mfa/iam-username
And then to use this sub-account profile in the AWS CLI:
$ aws iam list-users --profile=sub-account