Review the IAM role association for the EC2 instance

Attach a least-privilege role through an instance profile when an EC2 workload needs AWS API access.

Description

When an EC2 application needs to call AWS APIs, attach a role through IamInstanceProfile so it can use temporary credentials. Grant API access only where the workload needs it.

Potential impact

API calls fail without suitable credentials. A long-lived key stored on the server can still be used if it is exposed.

Remediation

Give a role that trusts EC2 only the actions and resources the application needs. Verify that its API calls use the role’s temporary credentials, then revoke and remove obsolete long-lived keys.

Examples

Supply an approved AMI in the target Region and define AppRole.

Before

yaml
Resources:
  AppServer:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t3.micro
      ImageId: ami-1234567890abcdef0

After

yaml
Resources:
  AppServer:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t3.micro
      ImageId: ami-1234567890abcdef0
      IamInstanceProfile: !Ref AppInstanceProfile

  AppInstanceProfile:
    Type: AWS::IAM::InstanceProfile
    Properties:
      Roles:
        - !Ref AppRole

The second example puts AppRole in AppInstanceProfile and attaches that profile to AppServer.

References