|
| 1 | +# Example - AWS and Atlas PrivateLink with Terraform |
| 2 | + |
| 3 | +This project aims to provide a very straight-forward example of setting up PrivateLink connection between AWS and MongoDB Atlas. |
| 4 | + |
| 5 | + |
| 6 | +## Dependencies |
| 7 | + |
| 8 | +* Terraform v0.13 |
| 9 | +* An AWS account - provider.aws: version = "~> 3.3" |
| 10 | +* A MongoDB Atlas account - provider.mongodbatlas: version = "~> 0.6" |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +**1\. Ensure your AWS and MongoDB Atlas credentials are set up.** |
| 15 | + |
| 16 | +This can be done using environment variables: |
| 17 | + |
| 18 | +``` bash |
| 19 | +$ export AWS_SECRET_ACCESS_KEY='your secret key' |
| 20 | +$ export AWS_ACCESS_KEY_ID='your key id' |
| 21 | +``` |
| 22 | + |
| 23 | +```bash |
| 24 | +export MONGODB_ATLAS_PUBLIC_KEY="xxxx" |
| 25 | +export MONGODB_ATLAS_PRIVATE_KEY="xxxx" |
| 26 | +``` |
| 27 | + |
| 28 | +... or the `~/.aws/credentials` file. |
| 29 | + |
| 30 | +``` |
| 31 | +$ cat ~/.aws/credentials |
| 32 | +[default] |
| 33 | +aws_access_key_id = your key id |
| 34 | +aws_secret_access_key = your secret key |
| 35 | +
|
| 36 | +``` |
| 37 | +... 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**. |
| 38 | + |
| 39 | +**2\. Review the Terraform plan.** |
| 40 | + |
| 41 | +Execute the below command and ensure you are happy with the plan. |
| 42 | + |
| 43 | +``` bash |
| 44 | +$ terraform plan |
| 45 | +``` |
| 46 | +This project currently does the below deployments: |
| 47 | + |
| 48 | +- MongoDB cluster - M10 |
| 49 | +- AWS Custom VPC, Internet Gateway, Route Tables, Subnets with Public and Private access |
| 50 | +- PrivateLink Connection at MongoDB Atlas |
| 51 | +- Create VPC Endpoint in AWS |
| 52 | + |
| 53 | +**3\. Configure the security group as required.** |
| 54 | + |
| 55 | +The security group in this configuration allows All Traffic access in Inbound and Outbound Rules. |
| 56 | + |
| 57 | +**4\. Execute the Terraform apply.** |
| 58 | + |
| 59 | +Now execute the plan to provision the AWS and Atlas resources. |
| 60 | + |
| 61 | +``` bash |
| 62 | +$ terraform apply |
| 63 | +``` |
| 64 | + |
| 65 | +**5\. Destroy the resources.** |
| 66 | + |
| 67 | +Once you are finished your testing, ensure you destroy the resources to avoid unnecessary charges. |
| 68 | + |
| 69 | +``` bash |
| 70 | +$ terraform destroy |
| 71 | +``` |
| 72 | + |
| 73 | +**Important Point** |
| 74 | + |
| 75 | +To fetch the connection string follow the below steps: |
| 76 | +``` |
| 77 | +output "atlasclusterstring" { |
| 78 | + value = mongodbatlas_cluster.cluster-atlas.connection_strings |
| 79 | +} |
| 80 | +``` |
| 81 | +**Outputs:** |
| 82 | +``` |
| 83 | +atlasclusterstring = [ |
| 84 | + { |
| 85 | + "aws_private_link" = { |
| 86 | + "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" |
| 87 | + } |
| 88 | + "aws_private_link_srv" = { |
| 89 | + "vpce-0ebb76559e8affc96" = "mongodb+srv://cluster-atlas-pl-0.za3fb.mongodb.net" |
| 90 | + } |
| 91 | + "private" = "" |
| 92 | + "private_srv" = "" |
| 93 | + "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" |
| 94 | + "standard_srv" = "mongodb+srv://cluster-atlas.za3fb.mongodb.net" |
| 95 | + }, |
| 96 | +] |
| 97 | +``` |
| 98 | + |
| 99 | +To fetch a particular connection string, use the **lookup()** function of terraform as below: |
| 100 | + |
| 101 | +``` |
| 102 | +output "plstring" { |
| 103 | + value = lookup(mongodbatlas_cluster.cluster-atlas.connection_strings[0].aws_private_link_srv, aws_vpc_endpoint.ptfe_service.id) |
| 104 | +} |
| 105 | +``` |
| 106 | +**Output:** |
| 107 | +``` |
| 108 | +plstring = mongodb+srv://cluster-atlas-pl-0.za3fb.mongodb.net |
| 109 | +``` |
0 commit comments