In AWS architecture, we often require one AWS service to manage or access other AWS services (for example you want your EC2 instance to read data from S3 bucket) on your behalf. To do so, we have to give permission to that service just like we give permissions to IAM users in our account. These permissions are granted by attaching IAM policies to IAM Roles. Then this IAM role is assigned to the AWS service. This blog describes how we can create IAM roles on AWS using AWS management console and AWS command line interface.

Types of AWS Roles

There are four types of roles we can create in AWS which are as follows:

AWS Service Role

AWS service roles are most commonly used roles when you want one AWS service to have permissions to access another AWS service on your behalf. The AWS service role can be attached to an EC2 instance, Lambda functions or any other AWS service.

Another AWS Account Role

This is simply used to allow access from one AWS account to another AWS account.

Web Identity Role

This is a way to allow users which are not in your AWS account (not IAM Users) to access AWS services in your AWS account. So, using web identity roles these users can be permitted to use AWS services from your account.

SAML 2.0 Federation Role

This role is used to provide access to specific users to manage and access your AWS account if they are federated with SAML 2.0. SAML 2.0 is a protocol which can provide authentication and authorization between security domains.

Creating IAM Roles

In this section we are going to look at how you can create IAM roles using the following methods.

  • Using AWS Management Console
  • Using AWS Command Line Interface (CLI)

Creating IAM Role Using Management Console

Sign in to your AWS account and in the top search bar, type IAM.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/1-11.jpg" data-lazy- height="220" src="data:image/svg xml,” width=”884″>

Select the IAM option down the search menu. This will take you to your IAM dashboard. Click on Roles in the left side panel to manage IAM Roles in your account.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/2-11.jpg" data-lazy- height="384" src="data:image/svg xml,” width=”1064″>

Click on Create role button to create a new role in your account.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/3-10.jpg" data-lazy- height="319" src="data:image/svg xml,” width=”927″>

In the create role section, first you need to select the type of role you want to create. In this article, we are going to discuss only AWS service roles as they are the most commonly and frequently used type of role.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/4-9.jpg" data-lazy- height="491" src="data:image/svg xml,” width=”969″>

Now, you need to select the AWS service for which you want to create the role. There is a long list of services available here and we are going to stick with EC2.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/5-9.jpg" data-lazy- height="457" src="data:image/svg xml,” width=”874″>

To give a role the desired permission you want, you need to attach an IAM policy to the role just like an IAM policy is attached to IAM users to grant them permissions. These policies are JSON documents with single or multiple statements. You can either use AWS managed policies or create your own custom policies. For this demo, we will attach an AWS managed policy that gives read only permission to S3.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/6-9.jpg" data-lazy- height="473" src="data:image/svg xml,” width=”974″>

Next, you need to add tags if you want and this is totally optional step.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/7-7.jpg" data-lazy- height="281" src="data:image/svg xml,” width=”976″>

Lastly, review the details about the role you are creating and add the name for your role. Then click on the Create role button in the bottom right corner of the console.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/8-5.jpg" data-lazy- height="489" src="data:image/svg xml,” width=”982″>

So, you have successfully created a role in AWS and this role can be found in the roles section of IAM console.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/9-4.jpg" data-lazy- height="363" src="data:image/svg xml,” width=”945″>

Attach Role to Service

So far, we have created an IAM role, now we will see how we can attach this role to an AWS service to grant permissions. As we have created an EC2 role so it can only be attached to an EC2 instance.

In order to attach an IAM role to an EC2 instance, first create an EC2 instance in your AWS account. After creating an EC2 instance, go to the EC2 console.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/10-4.jpg" data-lazy- height="205" src="data:image/svg xml,” width=”1020″>

Click on the actions tab, choose Security from the list and click on the Modify IAM role.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/11-5.jpg" data-lazy- height="399" src="data:image/svg xml,” width=”1027″>

In the Modify IAM role section, select the role from the list you want to assign and simply click on Save button.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/12-2.jpg" data-lazy- height="350" src="data:image/svg xml,” width=”818″>

After this, if you want to verify that the role is actually attached to your instance, you can just look for it in the summary section.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/13-2.jpg" data-lazy- height="551" src="data:image/svg xml,” width=”982″>

Creating IAM Role Using Command Line Interface

IAM roles can be created using the command line interface, and this is the most common method from developers’ point of view who prefer using CLI over management console. For AWS, you can set up CLI either on Windows, Mac, Linux or simply you can use AWS cloudshell. First, login to AWS user account using your credentials and to create a new role, just go along the following procedure.

Create a test or trust relationship policy file using the following command in the terminal.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/14-2.jpg" data-lazy- height="102" src="data:image/svg xml,” width=”631″>

In the editor, paste the IAM policy you want to attach to the IAM role.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/15-2.jpg" data-lazy- height="256" src="data:image/svg xml,” width=”721″>

[


    “Version”: “2012-10-17”,

    “Statement”: [

        {

            “Effect”: “Allow”,

            “Principal”: {

                “Service”: “ec2.amazonaws.com”

            },

            “Action”: “sts:AssumeRole”

        }

    ]

]

After copying the IAM policy, save and exit the editor. In order to read the policy from the file, use the cat command.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/16-2.jpg" data-lazy- height="237" src="data:image/svg xml,” width=”628″>

Now, finally you can create your IAM role using the following command.

$ aws iam create-role –role-name  –assume-role-policy-document file://<name.json>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/17-2.jpg" data-lazy- height="346" src="data:image/svg xml,” width=”716″>

This command will create the IAM role and attach the IAM policy defined in the JSON document to the role.

The IAM policy attached to the IAM role can be changed by using the following command in the terminal.

$ aws iam attach-role-policy –role-name <name>–policy-arn<arn>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/18-1.jpg" data-lazy- height="44" src="data:image/svg xml,” width=”902″>

In order to list policy attached to the IAM role, use the following command in the terminal.

$ aws iam list-attached-role-policies –role-name<name>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/19-1.jpg" data-lazy- height="190" src="data:image/svg xml,” width=”1017″>

Attach Role to Service

After creating the IAM role, attach the newly created IAM role to AWS service. Here, we are going to attach the role to an EC2 instance.

To attach a role to an EC2 instance, we first need to create an instance profile using the following CLI command.

$ aws iam create-instance profile –instance-profile-name<name>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/20-1.jpg" data-lazy- height="234" src="data:image/svg xml,” width=”1105″>

Now, attach role to instance profile

$ aws iam add-role-to-instance-profile –instance-profile-name>name<–role-name>name<

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/21-1.jpg" data-lazy- height="66" src="data:image/svg xml,” width=”786″>

Finally, now we are going to attach this instance profile to our EC2 instance. For this we need the following command:

$ aws ec2 associate-iam-instance-profile –instance-id <id>–iam-instance-profile Name=<name>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/22-1.jpg" data-lazy- height="275" src="data:image/svg xml,” width=”830″>

In order to list IAM instance profile associations, use the following command in the terminal.

$ aws ec2 describe-iam-instance-profile-associations

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/23.jpg" data-lazy- height="296" src="data:image/svg xml,” width=”920″>

Conclusion

Managing IAM roles is one of the basic concepts in AWS cloud. IAM roles can be used to authorize AWS service to access another AWS service on your behalf. They are also important to keep your AWS resources secure by assigning specific permissions to AWS services they need. These roles can also be used to allow IAM users from other AWS accounts to use AWS resources on your AWS account. IAM roles use IAM policies to assign permissions to the AWS services they are attached with. This blog describes step by step procedure to create IAM roles using AWS management console and AWS command line interface.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/Zain-150×150.jpg" height="112" src="data:image/svg xml,” width=”112″>

Zain Abideen

A DevOps Engineer with expertise in provisioning and managing servers on AWS and Software delivery lifecycle (SDLC) automation. I’m from Gujranwala, Pakistan and currently working as a DevOps engineer.