使用 Amazon 开发工具包将 IAM policy 附加到用户 - Amazon Identity and Access Management
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

使用 Amazon 开发工具包将 IAM policy 附加到用户

以下代码示例显示如何将 IAM policy 附加到用户。

Go
SDK for Go V2
注意

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

package main import ( "context" "flag" "fmt" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/iam" ) // IAMAttachRolePolicyAPI defines the interface for the AttachRolePolicy function. // We use this interface to test the function using a mocked service. type IAMAttachRolePolicyAPI interface { AttachRolePolicy(ctx context.Context, params *iam.AttachRolePolicyInput, optFns ...func(*iam.Options)) (*iam.AttachRolePolicyOutput, error) } // AttachDynamoFullPolicy attaches an Amazon DynamoDB full-access policy to an AWS Identity and Access Management (IAM) role. // Inputs: // c is the context of the method call, which includes the AWS Region. // api is the interface that defines the method call. // input defines the input arguments to the service call. // Output: // If successful, an AttachRolePolicyOutput object containing the result of the service call and nil. // Otherwise, nil and an error from the call to AttachRolePolicy. func AttachDynamoFullPolicy(c context.Context, api IAMAttachRolePolicyAPI, input *iam.AttachRolePolicyInput) (*iam.AttachRolePolicyOutput, error) { return api.AttachRolePolicy(c, input) } func main() { roleName := flag.String("r", "", "The name of the IAM role") policyName := flag.String("p", "", "The name of the policy to attach to the role") flag.Parse() if *roleName == "" || *policyName == "" { fmt.Println("You must supply a role and policy name (-r ROLE -p POLICY)") return } cfg, err := config.LoadDefaultConfig(context.TODO()) if err != nil { panic("configuration error, " + err.Error()) } client := iam.NewFromConfig(cfg) policyArn := "arn:aws:iam::aws:policy/" + *policyName input := &iam.AttachRolePolicyInput{ PolicyArn: &policyArn, RoleName: roleName, } _, err = AttachDynamoFullPolicy(context.TODO(), client, input) if err != nil { fmt.Println("Unable to attach policy " + *policyName + " to role " + *roleName) return } fmt.Println("Policy " + *policyName + " attached to role " + *roleName) }
  • 有关 API 详细信息,请参阅《Amazon SDK for Go API 参考》中的 AttachUserPolicy

Python
适用于 Python (Boto3) 的 SDK
注意

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

def attach_policy(user_name, policy_arn): """ Attaches a policy to a user. :param user_name: The name of the user. :param policy_arn: The Amazon Resource Name (ARN) of the policy. """ try: iam.User(user_name).attach_policy(PolicyArn=policy_arn) logger.info("Attached policy %s to user %s.", policy_arn, user_name) except ClientError: logger.exception("Couldn't attach policy %s to user %s.", policy_arn, user_name) raise
  • 有关 API 详细信息,请参阅《Amazon SDK for Python(Boto3)API 参考》中的 AttachUserPolicy

Rust
SDK for Rust
注意

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

注意

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

pub async fn attach_user_policy( client: &iamClient, user_name: &str, policy_arn: &str, ) -> Result<(), iamError> { client .attach_user_policy() .user_name(user_name) .policy_arn(policy_arn) .send() .await?; Ok(()) }
  • 有关 API 详细信息,请参阅《Amazon SDK for Rust API 参考》中的 AttachUserPolicy

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