aws-sdkaws-sdk-rubylocalstack

Localstack: awscli works, aws-sdk raise errors


I want to use localstack with ruby aws-sdk. It seems the aws sdk miss some configuration or has a bug, it raises error:

After a

Aws::S3::Resource.new.bucket('mybucket').exists?

it raises a:

/usr/local/lib/ruby/2.2.0/net/http.rb:879:in `initialize': unable to connect to 
`mybucket.localstack`; SocketError: getaddrinfo: Name or service not known
(Seahorse::Client::NetworkingError)

On the same container, if I use awscli no problem at all:

root@35afc611394b:/app/user# aws --endpoint-url=http://localstack:4572 s3 mb s3://test1
  make_bucket: test1
root@35afc611394b:/app/user# aws --endpoint-url=http://localstack:4572 s3 ls
  2006-02-03 16:45:09 test1

I created a docker-compose.yml to help following the problem:

https://github.com/ook/localstack-s3-problem

I noticed in the readme what I tried for now.

Please advice :)


Solution

  • Thanks for providing the detailed repo to reproduce your issue.
    I was able to resolve this by forcing Aws configuration to force_path_style (based off of this).
    TLDR:

    If you enable path style access on your client, it will not append the bucket name to your domain name

    So the config ended up looking like this:

    Aws.config.update(endpoint: localstack, credentials: Aws::Credentials.new('sofake', 'solie'), region: 'eu-west-1', force_path_style: true)
    

    The next problem was due to the fact that the bucket didn't exist yet (at least on my machine). So, I had to run this command once Aws::S3::Resource.new.create_bucket(bucket: 'mybucket')

    After that your script worked as expected:

       Setting endpoint to http://localstack:4572/
       Aws.config={:endpoint=>"http://localstack:4572/", :credentials=>#
       <Aws::Credentials access_key_id="sofake">, :region=>"eu-west-1", 
    :force_path_style=>true}
       sleeping 1s
       setting aws endpoint
       Aws::S3::Resource.new.bucket('mybucket').exists?
       #<Aws::S3::Bucket:0x00559716b95a20 @name="mybucket", @data=nil, @client=#<Aws::S3::Client>>
       true