2018-10-11 15:00:23 -07:00
< ? php
/**
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* This file is licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License. A copy of
* the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* ABOUT THIS PHP SAMPLE: This sample is part of the SDK for PHP Developer Guide topic at
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/iam-examples-working-with-policies.html
*
*/
require 'vendor/autoload.php' ;
use Aws\Iam\IamClient ;
use Aws\Exception\AwsException ;
/**
* Attaches policy to the specified role
*
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
$client = new IamClient ([
'profile' => '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 ());
}
2018-10-11 14:25:58 -07:00
2018-10-23 12:17:39 -07:00
//snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
2018-10-18 16:55:47 -07:00
//snippet-sourcedescription:[AttachRolePolicy.php demonstrates how to attach an IAM policy to the specified User role.]
2018-10-11 14:25:58 -07:00
//snippet-keyword:[PHP]
2018-10-16 20:34:57 -07:00
//snippet-keyword:[AWS SDK for PHP v3]
2018-10-11 14:25:58 -07:00
//snippet-keyword:[Code Sample]
2018-10-16 10:26:09 -07:00
//snippet-keyword:[AWS Identity and Access Management (IAM)]
//snippet-service:[iam]
2018-10-11 14:25:58 -07:00
//snippet-sourcetype:[full-example]
2018-10-16 15:00:48 -07:00
//snippet-sourcedate:[2018-09-20]
2018-10-23 12:17:39 -07:00
//snippet-sourceauthor:[jschwarzwalder (AWS)]
2018-10-11 14:25:58 -07:00