Granting application permissions with an AWS EC2 instance profile
An EC2 instance profile is the link that passes an IAM role to an EC2 instance. The role defines permissions; the profile carries that role to the instance so applications can obtain temporary credentials. This avoids storing long-term access keys on a server, but every application on the same instance can use the same role, so least privilege and workload isolation matter. ([AWS IAM: Use instance profiles](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2_instance-profiles.html), [IAM roles for applications on EC2](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2.html))
Granting application permissions with an AWS EC2 instance profile
An EC2 instance profile is the link that passes an IAM role to an EC2 instance. The role defines permissions; the profile carries that role to the instance so applications can obtain temporary credentials. This avoids storing long-term access keys on a server, but every application on the same instance can use the same role, so least privilege and workload isolation matter. (AWS IAM: Use instance profiles, IAM roles for applications on EC2)
This guide is for operators and developers granting AWS API access to applications on EC2. It explains the difference between a role and a profile, the credential flow, attaching a role to an existing instance, and verifying or troubleshooting the setup. CLI/API automation needs separate profile creation and permissions, so it is noted but not treated as a full automation recipe.
How is an IAM role different from an instance profile?
An IAM role defines what actions are allowed and which principals may assume it. Its permissions policy limits actions and resources; its trust policy decides whether a principal such as ec2.amazonaws.com may assume the role. An instance profile is the container EC2 uses to pass the role to an instance. An instance can have one role at a time, while the same role can be used by many instances. A profile can contain only one role. (AWS: Use instance profiles, AWS: How EC2 roles work)
When you create an EC2 role in the AWS console, the console normally creates a same-named instance profile automatically. With the CLI, API, or SDK, you create the role and profile as separate resources and add the role to the profile; their names may differ. As a result, a console field labeled “IAM role” may show profile names. (AWS: Managing profiles, AWS EC2: Attach a role)
How does an application receive permissions?
Suppose an image-processing program on EC2 needs to read objects from one S3 bucket. An administrator creates a role allowing only the required s3:GetObject access to the relevant bucket paths and a trust policy that lets EC2 assume the role. After the profile is associated with the instance, the Instance Metadata Service (IMDS) makes temporary role credentials available. An application using an AWS SDK's default credential provider can obtain and refresh them before expiration. There is no need to embed or copy long-term access keys. (AWS IAM: EC2 application roles, AWS: Instance Metadata Service)
There is an important boundary: all processes and applications on the instance can use the same role permissions. Co-locating workloads with different trust levels or granting a broad role weakens least privilege. For containers that need separate permissions, consider a role at a more specific execution level, such as an ECS task role. (AWS IAM: EC2 application roles)
How do I attach a role to a running EC2 instance?
You need permission to manage the IAM role and instance association, and the principal making the change may need permission to pass that role. For an existing instance, the console path is:
Prepare a role whose trust policy allows EC2 to assume it and whose permissions policy grants only the application's needs. For S3, scope access to the required bucket and paths rather than all buckets.
In the EC2 console, open **Instances** and select the target instance.
Choose **Actions → Security → Modify IAM role**.
Select the desired IAM instance profile and choose **Update IAM role**. If a role is already attached, this replaces it. ([AWS EC2: Attach an IAM role](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/attach-iam-role.html))
Ensure the application uses the AWS SDK default credential chain. Environment variables or a local credentials file can be selected ahead of the instance role.
The person or automation making the change may need iam:PassRole, in addition to EC2 permissions. Restrict the role ARNs that can be passed: anyone able to attach a powerful role may indirectly use its permissions. (AWS IAM: EC2 roles and PassRole, AWS EC2: Permission to pass roles)
How can I verify that the association works?
First confirm in the EC2 instance details that a role/profile is associated. With CLI/API-managed resources, check both the profile name and the role it contains because they may differ. Inside the instance, verify that the AWS SDK or CLI is using role credentials, without printing or logging the credential values. A safe identity check is aws sts get-caller-identity, which reports the caller's account and role ARN.
Then test the actual required operation. For an S3 read role, the intended object read should work while an unnecessary write should be denied. This also helps reveal overly broad permissions. Cross-account access can require a resource policy and trust conditions in addition to the role's identity policy.
For security, consider requiring the newer Instance Metadata Service Version 2 (IMDSv2). Before enforcing it, verify that the application and SDK support IMDSv2; container networking can also be affected by the metadata hop limit. Blocking or misconfiguring metadata access prevents the application from receiving role credentials, so validate compatibility as well as the security setting. (AWS: Use IMDS, AWS: Configure metadata options)
What if the role is missing or access is denied?
**The role does not appear in the EC2 list:** Check whether the operator has `iam:ListInstanceProfiles`. If the role was created with the CLI/API, confirm that a profile exists and contains the role; the console may list the profile name rather than the role name. ([AWS IAM troubleshooting](https://docs.aws.amazon.com/IAM/latest/UserGuide/troubleshoot_iam-ec2.html))
**`AccessDenied` when attaching:** Check the EC2 permissions, `iam:PassRole` for the role, and permission to inspect the profile. Confirm that the profile exists and contains a role. The role's trust policy and its permissions policy solve different problems. ([AWS IAM troubleshooting](https://docs.aws.amazon.com/IAM/latest/UserGuide/troubleshoot_iam-ec2.html))
**The app cannot obtain credentials:** Confirm the profile is associated, IMDS is not blocked by host firewall settings, and the SDK supports IMDSv2. After changing a role, credential updates may take time to propagate. ([AWS IAM troubleshooting](https://docs.aws.amazon.com/IAM/latest/UserGuide/troubleshoot_iam-ec2.html), [IMDS documentation](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html))
**The app has unexpected permissions:** Check whether environment variables or a shared credentials file are taking precedence, and remember all applications on the instance share the role. Remove unneeded permissions and verify with a constrained test.
An instance profile is a way to connect a role to an instance, not a way to distribute a secret key. Design the role with least privilege, restrict who can pass it, and verify both metadata access and application behavior.
Official documentation
[AWS IAM: Use instance profiles](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2_instance-profiles.html)
[AWS IAM: Use a role for applications on EC2](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2.html)
[AWS EC2: Attach an IAM role to an instance](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/attach-iam-role.html)
[AWS IAM: Troubleshoot IAM and EC2](https://docs.aws.amazon.com/IAM/latest/UserGuide/troubleshoot_iam-ec2.html)
[AWS EC2: Use the Instance Metadata Service](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html)
[AWS EC2: Configure metadata options for new instances](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-IMDS-new-instances.html)
[AWS EC2: Permission to pass IAM roles](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/permission-to-pass-iam-roles.html)