AWS SDK for PHP バージョン 3 での IAM ポリシーの使用 - AWS SDK for PHP

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

AWS SDK for PHP バージョン 3 での IAM ポリシーの使用

ポリシーを作成することによって、ユーザーにアクセス許可を付与します。ポリシーとは、ユーザーが実行できるアクションと、そのアクションが影響を与えるリソースの一覧が記載されているドキュメントです。デフォルトでは、明示的に許可されていないアクションやリソースはすべて拒否されます。ポリシーを作成して、ユーザー、ユーザーのグループ、ユーザーが引き受けるロール、およびリソースにアタッチできます。

以下の例では、次の方法を示しています。

のすべてのサンプルコードAWS SDK for PHPは、 にあります GitHub

認証情報

サンプルコードを実行する前に、AWS の認証情報を設定します (認証情報 を参照)。AWS SDK for PHP からのインポート (基本的な使用法 を参照)。

ポリシーの作成

インポート

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Iam\IamClient;

サンプルコード

$client = new IamClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2010-05-08' ]); $myManagedPolicy = '{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "logs:CreateLogGroup", "Resource": "RESOURCE_ARN" }, { "Effect": "Allow", "Action": [ "dynamodb:DeleteItem", "dynamodb:GetItem", "dynamodb:PutItem", "dynamodb:Scan", "dynamodb:UpdateItem" ], "Resource": "RESOURCE_ARN" } ] }'; try { $result = $client->createPolicy(array( // PolicyName is required 'PolicyName' => 'myDynamoDBPolicy', // PolicyDocument is required 'PolicyDocument' => $myManagedPolicy )); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

ポリシーをロールにアタッチする

インポート

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Iam\IamClient;

サンプルコード

$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()); }

ポリシーをユーザーにアタッチします

インポート

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Iam\IamClient;

サンプルコード

$client = new IamClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2010-05-08' ]); $userName = 'USER_NAME'; $policyName = 'AmazonDynamoDBFullAccess'; $policyArn = 'arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess'; try { $attachedUserPolicies = $client->getIterator('ListAttachedUserPolicies', ([ 'UserName' => $userName, ])); if (count($attachedUserPolicies) > 0) { foreach ($attachedUserPolicies as $attachedUserPolicy) { if ($attachedUserPolicy['PolicyName'] == $policyName) { echo $policyName . " is already attached to this role. \n"; exit(); } } } $result = $client->attachUserPolicy(array( // UserName is required 'UserName' => $userName, // PolicyArn is required 'PolicyArn' => $policyArn, )); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

グループにポリシーをアタッチする

インポート

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Iam\IamClient;

サンプルコード

$client = new IamClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2010-05-08' ]); try { $result = $client->attachGroupPolicy(array( // GroupName is required 'GroupName' => 'string', // PolicyArn is required 'PolicyArn' => 'string', )); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

ユーザーポリシーをデタッチする

インポート

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Iam\IamClient;

サンプルコード

$client = new IamClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2010-05-08' ]); try { $result = $client->detachUserPolicy([ // UserName is required 'UserName' => 'string', // PolicyArn is required 'PolicyArn' => 'string', ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

グループポリシーをデタッチする

インポート

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Iam\IamClient;

サンプルコード

$client = new IamClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2010-05-08' ]); try { $result = $client->detachGroupPolicy([ // GroupName is required 'GroupName' => 'string', // PolicyArn is required 'PolicyArn' => 'string', ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

ポリシーの削除

インポート

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Iam\IamClient;

サンプルコード

$client = new IamClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2010-05-08' ]); try { $result = $client->deletePolicy(array( // PolicyArn is required 'PolicyArn' => 'string' )); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

ロールポリシーを削除

インポート

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Iam\IamClient;

サンプルコード

$client = new IamClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2010-05-08' ]); try { $result = $client->deleteRolePolicy([ // RoleName is required 'RoleName' => 'string', // PolicyName is required 'PolicyName' => 'string' ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

ユーザーポリシーを削除する

インポート

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Iam\IamClient;

サンプルコード

$client = new IamClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2010-05-08' ]); try { $result = $client->deleteUserPolicy([ // UserName is required 'UserName' => 'string', // PolicyName is required 'PolicyName' => 'string', ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

グループポリシーを削除する

インポート

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Iam\IamClient;

サンプルコード

$client = new IamClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2010-05-08' ]); try { $result = $client->deleteGroupPolicy(array( // GroupName is required 'GroupName' => 'string', // PolicyName is required 'PolicyName' => 'string', )); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }