Aggiungere una policy gestita a un utente IAM - AWS SDK per Ruby

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Aggiungere una policy gestita a un utente IAM

L'esempio seguente aggiunge la policy gestita AmazonS3FullAccess all'utente IAM my_groovy_user nella us-west-2 regione.

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX - License - Identifier: Apache - 2.0 require 'aws-sdk-iam' # Attaches a policy to a user in AWS Identity and Access Management (IAM). # # Prerequisites: # - The user in IAM. # # @param iam [Aws::IAM::Client] An initialized IAM client. # @param user_name [String] The name of the user. # @param policy_arn [String] The Amazon Resource Name (ARN) of the policy. # @return [Boolean] true if the policy was attached; otherwise, false. # @example # exit 1 unless alias_created?( # Aws::IAM::Client.new, # 'my-user', # 'arn:aws:iam::aws:policy/AmazonS3FullAccess' # ) def policy_attached_to_user?(iam_client, user_name, policy_arn) iam_client.attach_user_policy( user_name: user_name, policy_arn: policy_arn ) return true rescue StandardError => e puts "Error attaching policy to user: #{e.message}" return false end # Full example call: def run_me user_name = 'my-user' arn_prefix = 'arn:aws:iam::aws:policy/' policy_arn = arn_prefix + 'AmazonS3FullAccess' iam_client = Aws::IAM::Client.new puts "Attempting to attach policy with ARN '#{policy_arn}' to " \ "user '#{user_name}'..." if policy_attached_to_user?(iam_client, user_name, policy_arn) puts 'Policy attached.' else puts 'Policy not attached.' end end run_me if $PROGRAM_NAME == __FILE__