Example usage for com.amazonaws.services.identitymanagement AmazonIdentityManagementClient createRole

List of usage examples for com.amazonaws.services.identitymanagement AmazonIdentityManagementClient createRole

Introduction

In this page you can find the example usage for com.amazonaws.services.identitymanagement AmazonIdentityManagementClient createRole.

Prototype

@Override
public CreateRoleResult createRole(CreateRoleRequest request) 

Source Link

Document

Creates a new role for your AWS account.

Usage

From source file:awslabs.lab41.SolutionCode.java

License:Open Source License

@Override
public String prepMode_CreateRole(AmazonIdentityManagementClient iamClient, String roleName, String policyText,
        String trustRelationshipText) {
    String roleArn = null;//from   w w w  .ja v a2  s  .c om

    //  Construct a CreateRoleRequest object using the specified name and "assume role" policy. The policy is the trustRelationshipText parameter.
    CreateRoleRequest createRoleRequest = new CreateRoleRequest()
            .withAssumeRolePolicyDocument(trustRelationshipText).withRoleName(roleName);
    //  Submit the request using the createRole method of the iamClient object.
    //  Retrieve and store the role ARN from the request response.
    roleArn = iamClient.createRole(createRoleRequest).getRole().getArn();

    //  Construct a PutRolePolicyRequest object using the provided policy for the new role. Use whatever policy name you like.
    PutRolePolicyRequest putRolePolicyRequest = new PutRolePolicyRequest().withPolicyDocument(policyText)
            .withPolicyName(roleName + "_policy").withRoleName(roleName);
    //  Submit the request using the putRolePolicy method of the iamClient object.
    iamClient.putRolePolicy(putRolePolicyRequest);

    //  Return the ARN for the new role.
    return roleArn;
}