Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use local DynamoDB with AWS Go SDK?

I was looking at the DynamoDB Go SDK docs, but I cannot find how to connect to the local version of DynamoDB. Did I miss something?

like image 357
Jiew Meng Avatar asked Mar 13 '17 04:03

Jiew Meng


People also ask

Can I use AWS SDK for Java with DynamoDB?

You now can use the AWS SDK for Java 2.x with DynamoDB local, the downloadable version of Amazon DynamoDB. With DynamoDB local, you can develop and test applications by using a version of DynamoDB running in your local development environment without incurring any additional costs.

Does DynamoDB local require an internet connection?

DynamoDB local does not require an internet connection, and it works with your existing DynamoDB API calls. To learn more about the AWS SDK for Java 2.x, see the official AWS SDK for Java - Version 2 on GitHub.

What is Amazon DynamoDB for go?

Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. The AWS SDK for Go examples can integrate Amazon DynamoDB into your Go applications.

How to run DynamoDB on localhost 8000?

You'll have DynamoDB up and running locally at localhost:8000. Code your application to use config files and set your default/local dev config files to use localhost:8000 for dynamodb and run your tests. Done. I do this for all of my apps AWS and GCE dependencies, such as AWS S3, Redis/ElasticCache, ElasticSearch, etc etc.


3 Answers

You want to use the Endpoint property of Config: https://docs.aws.amazon.com/sdk-for-go/api/aws/#Config

For example:

svc := dynamodb.New(mySession, aws.NewConfig().WithEndpoint("http://localhost:8000"))
like image 73
Cmag Avatar answered Oct 11 '22 03:10

Cmag


I had a similar challenge and was able to get it working doing something like this after setting AWS environment variables. I also used the "github.com/aws/aws-sdk-go/aws/credentials" library.

os.Setenv("AWS_ACCESS_KEY_ID", "dummy1")
os.Setenv("AWS_SECRET_ACCESS_KEY", "dummy2")
os.Setenv("AWS_SESSION_TOKEN", "dummy3")

I then add these statements to get the aws credentials and start my session

creds := credentials.NewEnvCredentials()
creds.Get()
sess, err := session.NewSession(&aws.Config{
    Region: aws.String("us-east-1"),
    Endpoint: aws.String("http://localhost:8000"),
    Credentials: creds,
})
if err != nil {
    return errors.Wrap(err, "error creating dynamodb session")
}
svc := dynamodb.New(sess)
like image 33
Fadyboy Avatar answered Oct 11 '22 03:10

Fadyboy


You could download and run AWS DynamoDB locally, if you really wanted to burden your local machine with it:

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html

But there is no need to.

Instead I've always used Docker to host local dependencies when developing against AWS. I spend > 4 hours each day during my commutes with no internet access. So, offline development is essential.

Never used Docker? It's really easy.

  1. Download Docker for your OS and install it.
  2. Open a terminal (Windows: CMD or Powershell, OSX: normal terminal).

Then just run this:

docker run -d -p 8000:8000 -v /tmp/data:/data/ dwmkerr/dynamodb -dbPath /data/

You'll have DynamoDB up and running locally at localhost:8000. Code your application to use config files and set your default/local dev config files to use localhost:8000 for dynamodb and run your tests. Done.

I do this for all of my apps AWS and GCE dependencies, such as AWS S3, Redis/ElasticCache, ElasticSearch, etc etc.

There are several "mock" Docker containers available that mimick these online services. The objective is to find a container that suites your needs, run it locally and use it. For example, while most AWS S3 docker containers available support most of the S3 API, none of them support S3 Turbo - which if you think about it is really out of context of a local container. None the less, be aware of certain restrictions certain mock containers may have.

For more complex dependency chains, read up on Docker-Compose which is a way to orchestrate a whole bunch of dependencies with a single command: docker-compose up and docker-compose down and that's it.

Note: Docker filesystems are normally transient, meaning they are not persisted. In the DynamoDB Docker container example I have earlier, it specified a way to keep persisted data during container restarts.

If you don't care about persisting data (I dont, that's what Integration Tests are for - who wants to keep a few million test accounts?), then you can just run:

docker run -d -p 8000:8000 dwmkerr/dynamodb

When the container is shut down, all changes will be lost: perfect for local development and a few 100 integration tests!

like image 2
eduncan911 Avatar answered Oct 11 '22 04:10

eduncan911