Code Monkey home page Code Monkey logo

aws-atlas-privatelink's Introduction

Example - AWS and Atlas PrivateLink with Terraform

This project aims to provide a very straight-forward example of setting up PrivateLink connection between AWS and MongoDB Atlas.

Dependencies

  • Terraform v0.12 or v0.13
  • An AWS account - provider.aws: version = "~> 3.3"
  • A MongoDB Atlas account - provider.mongodbatlas: version = "~> 0.6"

Usage

1. Ensure your AWS and MongoDB Atlas credentials are set up.

This can be done using environment variables:

$ export AWS_SECRET_ACCESS_KEY='your secret key'
$ export AWS_ACCESS_KEY_ID='your key id'
export MONGODB_ATLAS_PUBLIC_KEY="xxxx"
export MONGODB_ATLAS_PRIVATE_KEY="xxxx"

... or the ~/.aws/credentials file.

$ cat ~/.aws/credentials
[default]
aws_access_key_id = your key id
aws_secret_access_key = your secret key

... or follow as in the variables.tf file and create terraform.tfvars file with all the variable values and make sure not to commit it.

2. Review the Terraform plan.

Execute the below command and ensure you are happy with the plan.

$ terraform plan

This project currently does the below deployments:

  • MongoDB cluster - M10
  • AWS Custom VPC, Internet Gateway, Route Tables, Subnets with Public and Private access
  • PrivateLink Connection at MongoDB Atlas
  • Create VPC Endpoint in AWS

3. Configure the security group as required.

The security group in this configuration allows All Traffic access in Inbound and Outbound Rules.

4. Execute the Terraform apply.

Now execute the plan to provision the AWS and Atlas resources.

$ terraform apply

5. Destroy the resources.

Once you are finished your testing, ensure you destroy the resources to avoid unnecessary charges.

$ terraform destroy

Important Point

To fetch the connection string follow the below steps:

output "atlasclusterstring" {
    value = mongodbatlas_cluster.cluster-atlas.connection_strings
}

Outputs:

atlasclusterstring = [
  {
    "aws_private_link" = {
      "vpce-0ebb76559e8affc96" = "mongodb://pl-0-us-east-1.za3fb.mongodb.net:1024,pl-0-us-east-1.za3fb.mongodb.net:1025,pl-0-us-east-1.za3fb.mongodb.net:1026/?ssl=true&authSource=admin&replicaSet=atlas-d177ke-shard-0"
    }
    "aws_private_link_srv" = {
      "vpce-0ebb76559e8affc96" = "mongodb+srv://cluster-atlas-pl-0.za3fb.mongodb.net"
    }
    "private" = ""
    "private_srv" = ""
    "standard" = "mongodb://cluster-atlas-shard-00-00.za3fb.mongodb.net:27017,cluster-atlas-shard-00-01.za3fb.mongodb.net:27017,cluster-atlas-shard-00-02.za3fb.mongodb.net:27017/?ssl=true&authSource=admin&replicaSet=atlas-d177ke-shard-0"
    "standard_srv" = "mongodb+srv://cluster-atlas.za3fb.mongodb.net"
  },
]

To fetch a particular connection string, use the lookup() function of terraform as below:

output "plstring" {
    value = lookup(mongodbatlas_cluster.cluster-atlas.connection_strings[0].aws_private_link_srv, aws_vpc_endpoint.ptfe_service.id)
}

Output:

plstring = mongodb+srv://cluster-atlas-pl-0.za3fb.mongodb.net

aws-atlas-privatelink's People

Contributors

nikhil-mongo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.