|
| 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 Serverless. |
| 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 = data.mongodbatlas_serverless_instance.cluster_atlas.connection_strings_standard_srv |
| 79 | +} |
| 80 | +``` |
| 81 | +**Outputs:** |
| 82 | +``` |
| 83 | +atlasclusterstring = "mongodb+srv://cluster-atlas.za3fb.mongodb.net" |
| 84 | + |
| 85 | +``` |
| 86 | + |
| 87 | +To fetch a private connection string, use the output of terraform as below after second apply: |
| 88 | + |
| 89 | +``` |
| 90 | +output "plstring" { |
| 91 | + value = mongodbatlas_serverless_instance.cluster_atlas.connection_strings_private_endpoint_srv[0] |
| 92 | +} |
| 93 | +``` |
| 94 | +**Output:** |
| 95 | +``` |
| 96 | +plstring = mongodb+srv://cluster-atlas-pe-0.za3fb.mongodb.net |
| 97 | +``` |
0 commit comments