Class: AWS::IAM::AccessKeyCollection

Inherits:
Object
  • Object
show all
Includes:
Collection
Defined in:
lib/aws/iam/access_key_collection.rb

Overview

Both AWS accounts and IAM users can have access keys (maximum of 2). You can create new keys so that you can rotate out your old keys. You can create, delete, activate and deactivate access keys.

Create New Access Keys

# for the aws account
access_keys = iam.access_keys.create

# for an iam user
user_access_keys = iam.users['johndoe'].access_keys.create

Secret

Make sure after creating an access to retrieve the secret access key and save it somewhere safe.

access_keys = iam.access_keys.create   
secret = access_keys.secret

If you try to access the secret on an access key that was not newly created an error will be raised. AWS will only give the secret for a newly created access key:

access_keys = iam.access_keys.first
access_keys.secret
#=> oops, raises a runtime error

Instance Attribute Summary collapse

Attributes included from Core::Model

#config

Instance Method Summary collapse

Methods included from Core::Collection::Limitable

#each_batch

Methods included from Core::Collection

#each_batch, #enum, #first, #in_groups_of, #page

Methods included from Core::Model

#client, #config_prefix, #inspect

Constructor Details

#initialize(options = {}) ⇒ AccessKeyCollection

Returns a new instance of AccessKeyCollection.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :user (User)

    If present, this collection will only represent the access keys for the given user.


52
53
54
55
# File 'lib/aws/iam/access_key_collection.rb', line 52

def initialize options = {}
  @user = options[:user]
  @user ? super(@user, options) : super(options)
end

Instance Attribute Details

#userUser? (readonly)

Returns the user these accesss keys belong to. If this returns nil then these access keys belong to the AWS account.

Returns:

  • (User, nil)

    Returns the user these accesss keys belong to. If this returns nil then these access keys belong to the AWS account.


60
61
62
# File 'lib/aws/iam/access_key_collection.rb', line 60

def user
  @user
end

Instance Method Details

#[](access_key_id) ⇒ AccessKey

Returns a reference to the access key with the given access_key_id.

Parameters:

  • access_key_id (String)

    The ID of the access key.

Returns:

  • (AccessKey)

    Returns a reference to the access key with the given access_key_id.


77
78
79
# File 'lib/aws/iam/access_key_collection.rb', line 77

def [] access_key_id
  AccessKey.new(access_key_id, new_options)
end

#clearnil

Deletes all of the access keys from this collection.

iam.users['someuser'].access_keys.clear

Returns:

  • (nil)

86
87
88
89
# File 'lib/aws/iam/access_key_collection.rb', line 86

def clear
  each{|access_key| access_key.delete }
  nil
end

#createObject


62
63
64
65
66
67
68
69
70
71
72
# File 'lib/aws/iam/access_key_collection.rb', line 62

def create

  options = {}
  options[:user_name] = user.name if user

  resp = client.create_access_key(options)

  AccessKey.new_from(:create_access_key, resp.access_key,
    resp.access_key.access_key_id, new_options)
  
end

#each(options = {}) {|access_key| ... } ⇒ nil

Yields once for each access key. You can limit the number of access keys yielded using :limit.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :limit (Integer)

    The maximum number of access keys to yield.

  • :batch_size (Integer)

    The maximum number of access keys received each service reqeust.

Yield Parameters:

Returns:

  • (nil)

101
102
103
104
105
# File 'lib/aws/iam/access_key_collection.rb', line 101

def each options = {}, &block
  each_options = options.dup
  each_options[:user_name] = user.name if user
  super(each_options, &block)
end