php-awsHow do I use the AWS PHP SDK to set up an IAM role?
- First, install the AWS PHP SDK using Composer:
composer require aws/aws-sdk-php
- Next, create an IAM role using the AWS PHP SDK. You can do this by creating an
Aws\Iam\IamClient
object and calling thecreateRole
method on it.
$client = new Aws\Iam\IamClient([
'region' => 'us-east-1',
'version' => '2010-05-08',
]);
$result = $client->createRole([
'AssumeRolePolicyDocument' => '{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}',
'RoleName' => 'my-iam-role',
]);
- The output of the above code will be an
Aws\Result
object containing the details of the created IAM role.
Aws\Result Object
(
[data:Aws\Result:private] => Array
(
[Role] => Array
(
[Path] => /
[RoleName] => my-iam-role
[RoleId] => AROAJ6I4X3KXGJTJX5NCK
[Arn] => arn:aws:iam::123456789012:role/my-iam-role
[CreateDate] => 2020-08-05T20:30:27.000Z
[AssumeRolePolicyDocument] => {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
)
)
)
- You can also add permissions to the IAM role by calling the
putRolePolicy
method on theAws\Iam\IamClient
object.
$client->putRolePolicy([
'PolicyDocument' => '{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::my-bucket"
}
]
}',
'RoleName' => 'my-iam-role',
'PolicyName' => 'MyBucketPolicy',
]);
- You can also delete the IAM role by calling the
deleteRole
method on theAws\Iam\IamClient
object.
$client->deleteRole([
'RoleName' => 'my-iam-role',
]);
-
For more information about using the AWS PHP SDK to manage IAM roles, refer to the AWS PHP SDK Documentation.
-
You can also refer to the AWS IAM Documentation for more information about IAM roles.
More of Php Aws
- How do I use PHP to create a ZIP file on AWS?
- How can I use AWS and Zksync together with PHP?
- How can I use Yum to install PHP on an Amazon EC2 instance?
- How can I use an AWS SQS Worker with PHP?
- How can I use AWS WAF to secure my PHP application?
- How can I use AWS PHP SDK without credentials?
- How do I generate an AWS Signature Version 4 with PHP?
- How do I determine the version of PHP I am running on AWS?
- How can I use the AWS S3 S3Client library with PHP?
- How can I connect to an AWS MySQL database using PHP?
See more codes...