mongodb-auto-discovery


License

License

Categories

Categories

MongoDB Data Databases Auto Application Layer Libs Code Generators
GroupId

GroupId

com.gu
ArtifactId

ArtifactId

mongodb-auto-discovery_2.11
Last Version

Last Version

1.6
Release Date

Release Date

Type

Type

jar
Description

Description

mongodb-auto-discovery
mongodb-auto-discovery
Project URL

Project URL

https://github.com/guardian/mongodb-auto-discovery
Project Organization

Project Organization

com.gu
Source Code Management

Source Code Management

https://github.com/guardian/mongodb-auto-discovery

Download mongodb-auto-discovery_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/com.gu/mongodb-auto-discovery_2.11/ -->
<dependency>
    <groupId>com.gu</groupId>
    <artifactId>mongodb-auto-discovery_2.11</artifactId>
    <version>1.6</version>
</dependency>
// https://jarcasting.com/artifacts/com.gu/mongodb-auto-discovery_2.11/
implementation 'com.gu:mongodb-auto-discovery_2.11:1.6'
// https://jarcasting.com/artifacts/com.gu/mongodb-auto-discovery_2.11/
implementation ("com.gu:mongodb-auto-discovery_2.11:1.6")
'com.gu:mongodb-auto-discovery_2.11:jar:1.6'
<dependency org="com.gu" name="mongodb-auto-discovery_2.11" rev="1.6">
  <artifact name="mongodb-auto-discovery_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.gu', module='mongodb-auto-discovery_2.11', version='1.6')
)
libraryDependencies += "com.gu" % "mongodb-auto-discovery_2.11" % "1.6"
[com.gu/mongodb-auto-discovery_2.11 "1.6"]

Dependencies

compile (2)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.7
com.amazonaws : aws-java-sdk jar 1.9.12

test (2)

Group / Artifact Type Version
org.scalatest : scalatest_2.11 jar 2.2.6
org.scalacheck : scalacheck_2.11 jar 1.12.5

Project Modules

There are no modules declared in this project.

# mongodb-auto-discovery

Build Status

Releases at Maven Central

## Motivation

Auto-discover your MongoDB instances in AWS and generate connection configuration during run-time.

Reserving private IP addresses in an AWS VPC is tricky and relying on a privately hosted Route 53 is a bit of a pain. By using instance tags we can instead auto-discover which hosts that are designated to run MongoDB and generate the configuration using that.

Using the library

### Add it as a dependency in build.sbt

libraryDependencies += "com.gu" %% "mongodb-auto-discovery" % "1.6"

Make sure your instances have the right permissions

Your EC2 instances need to be able to read EC2 meta-data in order to find the MongoDB servers. The easiest way to set this up is to grant the following permissions to the instances in your stack(s) as part of your CloudFormation:

{
  "Effect": "Allow",
  "Action": "ec2:Describe*",
  "Resource": "*"
}

### Example using the Play framework

import com.gu.aws.{EC2, EC2DiscoveryService}
import com.gu.mongodb.AutoDiscovery
import play.api.Play
import play.api.Play.current

object MongoConfig {

  val mongoAutoDiscovery = new AutoDiscovery(EC2DiscoveryService, EC2)

  def uri: String = {
    Play.configuration.getString("mongodb.uri") match {
      case Some(uriFromConfig) => uriFromConfig
      case _ => {
        val uri = for {
          username <- Play.configuration.getString("mongodb.username")
          password <- Play.configuration.getString("mongodb.password")
          database <- Play.configuration.getString("mongodb.database")
        } yield mongoAutoDiscovery.mongoUri(username, password, database, Config.stage, Config.stack)
        uri.getOrElse(throw new RuntimeException("Could not construct Mongo URI. Missing mongodb.username, mongodb.password or mongodb.database.")).toString
      }
    }
  }

}
com.gu

The Guardian

The source code of the world's leading liberal voice

Versions

Version
1.6
1.5