Bekerja dengan kebijakan IAM dengan AWS SDK for PHP Versi 3 - AWS SDK for PHP

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Bekerja dengan kebijakan IAM dengan AWS SDK for PHP Versi 3

Anda memberikan izin kepada pengguna dengan membuat kebijakan. Kebijakan adalah dokumen yang mencantumkan tindakan yang dapat dilakukan pengguna dan sumber daya yang dapat memengaruhi tindakan tersebut. Secara default, tindakan atau sumber daya apa pun yang tidak diizinkan secara eksplisit ditolak. Kebijakan dapat dibuat dan dilampirkan ke pengguna, grup pengguna, peran yang diambil oleh pengguna, dan sumber daya.

Contoh berikut menunjukkan cara:

Semua kode contoh untuk AWS SDK for PHP tersedia di sini GitHub.

Kredensial

Sebelum menjalankan kode contoh, konfigurasikan AWS kredenal Anda, seperti yang dijelaskan dalam. Kredensial Kemudian imporAWS SDK for PHP, seperti yang dijelaskan dalamPenggunaan dasar.

Buat kebijakan

Impor

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

Kode Sampel

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

Lampirkan kebijakan ke peran

Impor

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

Kode Sampel

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

Lampirkan kebijakan ke pengguna

Impor

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

Kode Sampel

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

Lampirkan kebijakan ke grup

Impor

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

Kode Sampel

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

Lepaskan kebijakan pengguna

Impor

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

Kode Sampel

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

Lepaskan kebijakan grup

Impor

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

Kode Sampel

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

Menghapus kebijakan

Impor

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

Kode Sampel

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

Menghapus kebijakan peran

Impor

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

Kode Sampel

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

Menghapus kebijakan pengguna

Impor

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

Kode Sampel

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

Menghapus kebijakan grup

Impor

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

Kode Sampel

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