What works
We have several EC2 instances that pull things out of an S3 bucket on boot (and at other times). To allow this, we have an IAM policy granting read-only access...
"Effect": "Allow",
"Action": [
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::BUCKET"
]
"Effect": "Allow",
"Action": [
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::BUCKET/prefix-for-folder",
"arn:aws:s3:::BUCKET/prefix-for-folder/*",
]
# other similar Get/List/Read permissions for S3, EFS, etc, all
# specifically to support automated read-only instance actions
...and yes, those should probably be combined into single Statement
blocks; some of them are generated and some are managed by hand. Anyhow, we also have an IAM role with the above policy attached. EC2 instances for this account then list that role as their "instance role" and magically get read permissions at boot.
Works like a charm.
The goal
Trying to create some EC2 instances in a different AWS account. Ideally we'd like them to be able to list the "instance role" from the first account, and get the same read-only access to the buckets-and-whatnot in the first account.
What I've tried
Trying to edit the IAM role mentioned above. Following the various "give access to resources across accounts" tutorials, it's clear that the permissions in the policy/role aren't the crucial bit here, but rather the trust relationships in the role. In the "instance role", the working trust relationships are
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
and in the examples given for sharing across accounts, the principal is instead the AWS account ID of the account being shared to:
# they're careful to point out that 'root' means any authenticated
# user in that account, not the special root user for the account
"Principal": {
"AWS": "arn:aws:iam::other-account-12-digit-ID:root",
},
I'm hoping to avoid duplicating the role and the entire complex policy for just the sake of a different principal, and according to the AWS IAM user guide there can be more than once principal listed, so I tried changing the existing role's trust relationship to
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::other-account-12-digit-ID:root",
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
The IAM Console was okay with the Principal syntax, and I've verified that EC2 instances in this same account continue to boot up and take on the specified instance role/policy.
However, creating an EC2 instance in other-account-12-digit-ID doesn't display this role as an option for the instance role. (In the EC2's "new launch experience" wizard, if that's relevant.)
My vague theory is that the arn:aws:iam::other-account-12-digit-ID:root
is the wrong ARN to specify EC2 instances in the other account -- guessing that maybe this syntax is for people, not instance profiles -- but I don't know what I should be trying instead? I'm also assuming that any of this is even possible in the first place -- all the examples of sharing across accounts that I can find refer to S3 buckets directly, rather than an EC2 instance role. Dunno how much that matters?
There are some other SO/SE/ServerFault questions involving IAM roles like this one, but the problem and answers all involve additional pieces like Ansible or Elastic Beanstalk. I'm hoping to figure out what I need to do to IAM (or the EC2 launcher) itself by hand, and will eventually apply it to CloudFormation and AWS CDK and so forth.