Introduction:
Attaching IAM (Identity and Access Management) roles to an EC2 (Elastic Compute Cloud) instance in AWS allows you to grant specific permissions and access to resources. This step-by-step guide will walk you through the process of creating a new IAM role, launching an EC2 instance, and attaching the IAM role to the instance. By the end of this guide, you will have successfully attached an IAM role to an EC2 instance and verified its functionality.
Step 1: Create a New IAM Role and Attach IAM ReadOnly Policy:
- - Go to the AWS Management Console and navigate to the IAM service.
- - Select "Roles" from the left-hand side menu.
- - Click on "Create Role" and choose the service that will use the role (in this case, select EC2).
- - In the "Permissions" section, search for and select the "ReadOnlyAccess" policy.
- - Proceed to configure any optional tags or review the role's settings.
- - Give your role a meaningful name, such as "EC2ReadOnlyRole," and provide a description.
- - Click on "Create Role" to create the IAM role with the attached IAM ReadOnly policy.
Step 2: Launch an EC2 Instance:
- - Go to the EC2 Dashboard in the AWS Management Console.
- - Click on "Launch Instances" and follow the wizard to configure your instance, selecting the desired instance type, AMI, and other settings.
- - In the "Configure Instance Details" section, scroll down to the "IAM role" field.
- - Choose the "EC2ReadOnlyRole" (or the name you provided) from the drop-down menu.
- - Continue with the remaining steps of the instance launch wizard, configuring security groups, storage, and other settings as needed.
- - Launch the EC2 instance.
Step 3: Attach the IAM Role to the EC2 Instance:
- - Once the instance is launched and running, go to the EC2 Dashboard.
- - Select the newly created EC2 instance from the list.
- - In the "Actions" drop-down menu, choose "Security," and then "Modify IAM Role."
- - Select the "EC2ReadOnlyRole" (or the name you provided) from the "IAM role" drop-down menu.
- - Click on "Save" to attach the IAM role to the EC2 instance.
Step 4: Connect to the EC2 Instance:
- - Obtain the necessary connection details for your EC2 instance, including the public IP address or public DNS name.
- - Open a terminal or SSH client and use the appropriate command to connect to the instance. Step 5: Verify IAM Role Functionality:
Once connected to the EC2 instance, execute the following command:
aws iam list-users
- This command lists all the IAM users in the AWS account.
- If the command successfully returns a list of IAM users, it indicates that the IAM role attached to the EC2 instance has the required IAM ReadOnly permissions. ** Conclusion:** By following the step-by-step guide above, you have successfully created a new IAM role, launched an EC2 instance, attached the IAM role to the instance, and verified the functionality by listing IAM users from the EC2 instance. Attaching IAM roles to EC2 instances provides granular access control and allows instances to interact securely with other AWS services using temporary credentials.
Top comments (0)