Adding a Managed Policy to an IAM User - Amazon SDK for Ruby
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Adding a Managed Policy to an IAM User

The following example adds the managed policy AmazonS3FullAccess to the IAM user my_groovy_user in the us-west-2 region.

# 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__