Wednesday, October 27, 2021

[SOLVED] How to create an IAM role of specific type using boto3?

Issue

I'm trying to lock down a user to a specific VPC in AWS and following How to Help Lock Down a User’s Amazon EC2 Capabilities to a Single VPC | AWS Security Blog.

It is mentioned that we need to create an IAM role with name VPCLockDown of type AWS Service

IAM role type

and add the services for which the role needs access to. like ec2, lambda etc.

I was trying to create this role programatically using boto3.

I checked the create_role documentation for creating a role using boto3.

However, they haven't mentioned anything to specify the type of role and the services that I can specify that the role should have access to.

Is there any way to specify these items while creation of the IAM role using boto3

Edit1:

I tried creating a service_linked_role as per Sudarshan Rampuria's answer like

response = iam.create_service_linked_role(
            AWSServiceName='ec2.amazonaws.com',
        )

But getting the following error:

An error occurred (AccessDenied) when calling the CreateServiceLinkedRole operation: Cannot find Service Linked Role template for ec2.amazonaws.com


Solution

You can use create_service_linked_role() function boto3 to link a role to a service. https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.create_service_linked_role



Answered By - Sudarshan Rampuria