'default', 'region' => 'us-west-2', 'version' => '2010-05-08' ]); $roleName = 'ROLE_NAME'; $policyName = 'AmazonDynamoDBFullAccess'; $policyArn = 'arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess'; try { $attachedRolePolicies = $client->getIterator('ListAttachedRolePolicies', ([ 'RoleName' => $roleName, ])); if (count($attachedRolePolicies) > 0) { foreach ($attachedRolePolicies as $attachedRolePolicy) { if ($attachedRolePolicy['PolicyName'] == $policyName) { echo $policyName . " is already attached to this role. \n"; exit(); } } } $result = $client->attachRolePolicy(array( // RoleName is required 'RoleName' => $roleName, // PolicyArn is required 'PolicyArn' => $policyArn )); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); } // snippet-end:[iam.php.attach_role_policy.main] // snippet-end:[iam.php.attach_role_policy.complete] // snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.] // snippet-sourcedescription:[AttachRolePolicy.php demonstrates how to attach an IAM policy to the specified User role.] // snippet-keyword:[PHP] // snippet-sourcesyntax:[php] // snippet-keyword:[AWS SDK for PHP v3] // snippet-keyword:[Code Sample] // snippet-keyword:[AWS Identity and Access Management (IAM)] // snippet-service:[iam] // snippet-sourcetype:[full-example] // snippet-sourcedate:[2018-09-20] // snippet-sourceauthor:[jschwarzwalder (AWS)]