Aws-sdk-ruby: Aws::Errors::MissingCredentialsError when loading from a yaml file

Created on 31 Mar 2016  路  4Comments  路  Source: aws/aws-sdk-ruby

Hi,

I am newbie to ruby aws sdk . I am trying to write a script to start and stop instances based on tags
Unfortunately I am ending up with credential errors . The puts creds gives the correct output for secret and access keys .

i am running this script as a jenkins job .

#!/usr/bin/env ruby

require 'aws-sdk'
require 'yaml'

creds = YAML.load_file("/vol/jenkins/.aws/config.yml")
puts creds
ec2 = Aws::EC2::Resource.new(
    region: 'us-east-1',
    credentials: Aws::Credentials.new(creds[:aws_access_key_id], creds[:aws_secret_access_key]))
hostname = ARGV[0]
instances = ec2.instances(filters: [{ name: 'tag:shutdown', values:['yes'] }])
 if hostname.empty?
   puts "please specify all or DNS name"
   exit 1
 end

 if hostname == "all"
   instances.each do |instance|  # ---error points here
     puts "ID: #{instance.instance_id} State: #{instance.state.name} Hostname:{instance.private_dns_name}"
     instance.start
 end
 else
   instances.each do |instance|
     if instance.private_dns_name == hostname
       instance.start
     end
   end
 end

This is my code above for starting instances
Below is the error shown .

/vol/jenkins/.rvm/gems/ruby-2.2.1@aws/gems/aws-sdk-core-2.2.4/lib/aws-sdk-core/plugins/request_signer.rb:101:in `require_credentials': unable to sign request without credentials set (Aws::Errors::MissingCredentialsError)
    .
        .
        .
        .
    from /vol/jenkins/.rvm/gems/ruby-2.2.1@aws/gems/aws-sdk-resources-2.2.4/lib/aws-sdk-resources/request.rb:24:in `call'
    from /vol/jenkins/.rvm/gems/ruby-2.2.1@aws/gems/aws-sdk-resources-2.2.4/lib/aws-sdk-resources/operations.rb:127:in `all_batches'
    from /vol/jenkins/.rvm/gems/ruby-2.2.1@aws/gems/aws-sdk-resources-2.2.4/lib/aws-sdk-resources/collection.rb:18:in `each'
    from /vol/jenkins/.rvm/gems/ruby-2.2.1@aws/gems/aws-sdk-resources-2.2.4/lib/aws-sdk-resources/collection.rb:18:in `each'
    from /vol/jenkins/scripts/ec2/start_stage_nodes.rb:26:in `<main>'

guidance

Most helpful comment

You're only going to get this error if the credentials you provide are nil. For example:

ec2 = Aws::EC2::Resource.new(region: "us-east-1", credentials: Aws::Credentials.new("key", "secret")) #=> #<Aws::EC2::Resource>
ec2.instances.first # [Aws::EC2::Client 401 0.725211 0 retries] describe_instances() Aws::EC2::Errors::AuthFailure AWS was not able to validate the provided access credentials

ec2 = Aws::EC2::Resource.new(region: "us-east-1", credentials: Aws::Credentials.new(nil, nil)) #=> #<Aws::EC2::Resource>
ec2.instances.first # Aws::Errors::MissingCredentialsError: unable to sign request without credentials set

All 4 comments

Fixed it was my bad reading the yaml file in a wrong way !! creds[:defaults][:aws_access_key_id]
closing this issue ...

You're only going to get this error if the credentials you provide are nil. For example:

ec2 = Aws::EC2::Resource.new(region: "us-east-1", credentials: Aws::Credentials.new("key", "secret")) #=> #<Aws::EC2::Resource>
ec2.instances.first # [Aws::EC2::Client 401 0.725211 0 retries] describe_instances() Aws::EC2::Errors::AuthFailure AWS was not able to validate the provided access credentials

ec2 = Aws::EC2::Resource.new(region: "us-east-1", credentials: Aws::Credentials.new(nil, nil)) #=> #<Aws::EC2::Resource>
ec2.instances.first # Aws::Errors::MissingCredentialsError: unable to sign request without credentials set

Thanks for the update. One thing to consider, if it's feasible in your environment, is to create a shared credentials file at ~/.aws/credentials - if you follow the format in the documentation, it will be loaded correctly for all clients.

In any case, glad you were able to determine the cause of the issue!

I strongly recommend you read the first chapter or two of the Developer Guide, http://docs.aws.amazon.com/sdk-for-ruby/latest/DeveloperGuide/, as it covers a number of areas where you could stumble.

Was this page helpful?
0 / 5 - 0 ratings