Use AddRoleToInstanceProfile with a CLI
The following code examples show how to use AddRoleToInstanceProfile.
- CLI
-
- AWS CLI
-
To add a role to an instance profile
The following
add-role-to-instance-profilecommand adds the role namedS3Accessto the instance profile namedWebserver.aws iam add-role-to-instance-profile \ --role-nameS3Access\ --instance-profile-nameWebserverThis command produces no output.
To create an instance profile, use the
create-instance-profilecommand.For more information, see Using an IAM role to grant permissions to applications running on Amazon EC2 instances in the AWS IAM User Guide.
-
For API details, see AddRoleToInstanceProfile
in AWS CLI Command Reference.
-
- PowerShell
-
- Tools for PowerShell V4
-
Example 1: This command adds the role named
S3Accessto an existing instance profile namedwebserver. To create the instance profile, use theNew-IAMInstanceProfilecommand. After you create the instance profile and associate it with a role using this command, you can attach it to an EC2 instance. To do that, use theNew-EC2Instancecmdlet with either theInstanceProfile_Arnor theInstanceProfile-Nameparameter to launch the new instance.Add-IAMRoleToInstanceProfile -RoleName "S3Access" -InstanceProfileName "webserver"-
For API details, see AddRoleToInstanceProfile in AWS Tools for PowerShell Cmdlet Reference (V4).
-
- Tools for PowerShell V5
-
Example 1: This command adds the role named
S3Accessto an existing instance profile namedwebserver. To create the instance profile, use theNew-IAMInstanceProfilecommand. After you create the instance profile and associate it with a role using this command, you can attach it to an EC2 instance. To do that, use theNew-EC2Instancecmdlet with either theInstanceProfile_Arnor theInstanceProfile-Nameparameter to launch the new instance.Add-IAMRoleToInstanceProfile -RoleName "S3Access" -InstanceProfileName "webserver"-
For API details, see AddRoleToInstanceProfile in AWS Tools for PowerShell Cmdlet Reference (V5).
-
For a complete list of AWS SDK developer guides and code examples, see Using this service with an AWS SDK. This topic also includes information about getting started and details about previous SDK versions.