MongoDB Atlas VPC Peering CONTAINER_ALREADY_EXISTS

How to reuse “mongodbatlas_network_container” if it exists for a region specified? Currently, it gives an error if container exists crashing the workflow. Please suggest changes so the mongodbatlas_network_container can be reused if it exists else it will be created.

Hi Sumit,

As noted in the issue filed in the MongoDB Atlas Terraform Provider repro the ideal path here to get more help is to file a support ticket with the Atlas support team: Error: error creating MongoDB Network container, A container already exists for group · Issue #684 · mongodb/terraform-provider-mongodbatlas · GitHub. The Atlas support team provides support for the MongoDB Atlas Terraform Provider. We also have multiple examples for setting up peering at terraform-provider-mongodbatlas/examples at master · mongodb/terraform-provider-mongodbatlas · GitHub.

As stated in the issue you are likely getting this error because a container already exists for the region you are specifying. A container is just a VPC. If you already created a cluster or other resource that requires a VPC a container was likely created for you. In that case you will need to find out the container id directly with the Atlas Admin API. I’d recommend grabbing all the AWS containers for the project:

curl --user "{PUBLIC-KEY}:{PRIVATE-KEY}" --digest \
     --header "Accept: application/json" \
     --request GET "https://cloud.mongodb.com/api/atlas/v1.0/groups/{PROJECT-ID}/containers?providerName=AWS&pretty=true"

Take the id of the container for the region you want to create a peer with and import it into Terraform w/ the project id so you can use it - Terraform Registry

Once you’ve imported in the existing region use that in your Terraform configs. Then if you need to add any more AWS regions you can, if a container doesn’t already exist, create it with the container resource in the Terraform config.

2 Likes