使用 Amazon 开发工具包创建 IAM 服务相关角色 - Amazon Identity and Access Management
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

使用 Amazon 开发工具包创建 IAM 服务相关角色

以下代码示例显示如何创建 IAM 服务相关角色。

Go
SDK for Go V2
注意

在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

// CreateServiceLinkedRole fmt.Println("➡️ Create SLR for " + ExampleSLRService) createSlrResult, err := service.CreateServiceLinkedRole(context.Background(), &iam.CreateServiceLinkedRoleInput{ AWSServiceName: aws.String(ExampleSLRService), Description: aws.String(ExampleSLRDescription), }) // NOTE: We don't consider this an error as running this example multiple times will cause an error. if err != nil { fmt.Printf("Couldn't create service-linked role: %v\n", err.Error()) } else { fmt.Printf("Created service-linked role with ARN: %s\n", *createSlrResult.Role.Arn) }
JavaScript
SDK for JavaScript V3
注意

GitHub 上还有更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

创建客户端。

import { IAMClient } from "@aws-sdk/client-iam"; // Set the AWS Region. const REGION = "REGION"; // For example, "us-east-1". // Create an IAM service client object. const iamClient = new IAMClient({ region: REGION }); export { iamClient };

创建服务相关角色。

// Import required AWS SDK clients and commands for Node.js. import { iamClient } from "./libs/iamClient.js"; import { CreateServiceLinkedRoleCommand } from "@aws-sdk/client-iam"; // Set the parameters. const params = { AWSServiceName: "AWS_SERVICE_NAME" /* required */, }; const run = async () => { try { const data = await iamClient.send( new CreateServiceLinkedRoleCommand(params) ); console.log("Success", data); } catch (err) { console.log("Error", err); } }; run();
PHP
SDK for PHP
注意

在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

$uuid = uniqid(); $service = new IamService(); public function createServiceLinkedRole($awsServiceName, $customSuffix = "", $description = "") { $createServiceLinkedRoleArguments = ['AWSServiceName' => $awsServiceName]; if ($customSuffix) { $createServiceLinkedRoleArguments['CustomSuffix'] = $customSuffix; } if ($description) { $createServiceLinkedRoleArguments['Description'] = $description; } return $this->iamClient->createServiceLinkedRole($createServiceLinkedRoleArguments); }
Python
适用于 Python (Boto3) 的 SDK
注意

GitHub 上还有更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

def create_service_linked_role(service_name, description): """ Creates a service-linked role. :param service_name: The name of the service that owns the role. :param description: A description to give the role. :return: The newly created role. """ try: response = iam.meta.client.create_service_linked_role( AWSServiceName=service_name, Description=description) role = iam.Role(response['Role']['RoleName']) logger.info("Created service-linked role %s.", role.name) except ClientError: logger.exception("Couldn't create service-linked role for %s.", service_name) raise else: return role
  • 有关 API 详细信息,请参阅《Amazon SDK for Python(Boto3)API 参考》中的 CreateServiceLinkedRole

Ruby
SDK for Ruby
注意

在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

# Creates a service-linked role. # # @param service_name [String] The name of the service that owns the role. # @param description [String] A description to give the role. # @return [Aws::IAM::Role] The newly created role. def create_service_linked_role(service_name, description) response = @iam_resource.client.create_service_linked_role( aws_service_name: service_name, description: description) role = @iam_resource.role(response.role.role_name) puts("Created service-linked role #{role.name}.") rescue Aws::Errors::ServiceError => e puts("Couldn't create service-linked role for #{service_name}. Here's why:") puts("\t#{e.code}: #{e.message}") raise else role end
Rust
SDK for Rust
注意

本文档适用于预览版中的软件开发工具包。软件开发工具包可能随时发生变化,不应在生产环境中使用。

注意

在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

pub async fn create_service_linked_role( client: &iamClient, aws_service_name: String, custom_suffix: Option<String>, description: Option<String>, ) -> Result<CreateServiceLinkedRoleOutput, SdkError<CreateServiceLinkedRoleError>> { let response = client .create_service_linked_role() .aws_service_name(aws_service_name) .set_custom_suffix(custom_suffix) .set_description(description) .send() .await?; Ok(response) }

有关 Amazon 软件开发工具包开发人员指南和代码示例的完整列表,请参阅 将 IAM 与 Amazon 开发工具包配合使用。本主题还包括有关入门的信息以及有关先前的软件开发工具包版本的详细信息。