aws iam – Automatic EC2 Role Assignment in AWS

amazon-iamamazon-web-servicesauthenticationauthorization

Trying to understand AWS IAM resources/concepts a little better. I know there is a way to configure an EC2 (either possibly via its underlying AMI or a launch template) so that when it launches for the first time it is automatically assigned the right role/permissions for things that instance should have access to. For instance if I know that the software server running on that instance will need S3 read/write access, I should be able to configure things so that I don't need to SSH onto that instance and configure an ~/.aws/credentials file.

My understanding is that Users and Roles are IAM resources that make up authenticatable identities. Meaning you can gives a User/Role a set of credentials and those credentials can then authenticate against AWS and identify themselves. I believe it is the recommended practices to give human beings a User resource and to give software a Role resource. So I would have my own AWS IAM User, and that User would have credentials that I would punch in to the web console or CLI to gain access to AWS. And my server (which would be deployed to AWS) would be given an AWS IAM Role such as (myapp-server-dev) and that Role would have credentials that the server could then present to the AWS API for authentication.

My understanding is that Groups are just collections of Users and/or Roles, used for the purpose of assigning those Users/Roles to specific permission sets.

My understanding is that IAM Policies are those permission sets, and that you attach/bind Policies to Users, Roles and/or Groups so as to give them permission to various AWS services/resources.

My understanding is that IAM Instance Profiles somehow bind an EC2 instance to a Role, but herein lies my main confusion.

Finally, it is my understanding that in order to make it so that:

  • I create an EC2 instances; and
  • That EC2 instance automagically has access to the correct AWS services/resources without having to manually install credentials files on the instance via SSH…

…in order to make that happen, I need to:

  1. Create a Role for the EC2 instance and any other instances that will be running homogenous server software on them (that have the same authorization needs)
  2. Create an Instance Profile for the EC2 instance that is bound to this Role
  3. Attach a Policy to that Role (or any Group where the Role is member of) that gives it access to the appropriate services/resources

So first, if anything I've stated above is inaccurate, please begin my providing some course correction/clarification for me! Assuming my understandings are correct, then my question here is: how and where do I associate a given EC2 instance with an IAM Instance Profile, and how/where do I associate that Profile with a Role?

Best Answer

An "IAM Instance Profile" is a Role if you're using the console.

You associate one when you create the instance, in the "Advanced details" section.

Screenshot of AWS console, showing "advanced details" screen.

Behind the scenes, AWS is creating an instance profile for you. If you're using the command line or API, you can create them separately if you like; see https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2_instance-profiles.html for details.