EC2 instances should get AWS access through IAM instance roles
This check looks at whether your EC2 instances have an IAM role attached through an instance profile. The safe setting is that instances which call AWS use a role, instead of access keys stored on the machine.
Why it matters
An application on an EC2 instance has to sign its AWS API calls. If a person stores an access key on the instance, that key is long-lived, has to be copied around and rotated by hand, and is exposed to anyone who can read the machine.
The AWS IAM User Guide says you can and should use an IAM role for applications on EC2. The role supplies temporary credentials that refresh automatically, so no long-term key sits on the instance. See the sources below.
How to check it yourself
In the EC2 console select an instance and look at Actions → Security → Modify IAM role or the IAM Role field on the details tab. It should name a role.
The AWS CLI lists instances that have no instance profile. This is read-only, and it covers one Region at a time:
aws ec2 describe-instances --region eu-west-1 \
--query 'Reservations[].Instances[?!IamInstanceProfile].InstanceId[]' \
--output text
aws ec2 describe-iam-instance-profile-associations --region eu-west-1Repeat for each Region you use. An instance with no profile is not necessarily wrong, but if it calls AWS it needs one.
How to fix it
In the IAM console create a role with AWS service → EC2 as the trusted entity and attach only the permissions the application needs. In the EC2 console choose Actions → Security → Modify IAM role and pick it. With the CLI you create the profile and attach it as separate steps. Names and the instance ID below are placeholders:
aws iam create-role --role-name app-ec2-role \
--assume-role-policy-document file://ec2-trust.json
aws iam create-instance-profile --instance-profile-name app-ec2-profile
aws iam add-role-to-instance-profile \
--instance-profile-name app-ec2-profile --role-name app-ec2-role
aws ec2 associate-iam-instance-profile \
--instance-id i-0123456789abcdef0 \
--iam-instance-profile Name=app-ec2-profileThe trust policy file must allow ec2.amazonaws.com to assume the role. You run this yourself. ConfigCobra reads your account through a read-only role and never attaches roles or changes instances.
What changes when you fix it
The instance gets the role's permissions on top of whatever it had. Any application on it can use them, because AWS documents that one role is shared by everything on the instance. Keep the policy narrow, and decide who may pass the role by limiting iam:PassRole, or those people can hand the instance more rights than their own.
Once the application uses the role's temporary credentials through the SDK, delete the old access key from the machine and from IAM. Test first on one instance: a leftover key in a config file can still take priority over the role in some tools.
Included in
| Benchmark | CIS ID | Profile |
|---|---|---|
| CIS Amazon Web Services Foundations v7.0.0 | 2.16 | Level 2 |
Check this on your AWS
ConfigCobra runs this check with every other control of the CIS Amazon Web Services Benchmarks, read-only.