terraform-aws-mongodb-ecs
terraform-aws-mongodb-ecs copied to clipboard
Terraform module to provision MongoDB using AWS-ECS
This module is part of a project to simplify the provisioning of MongoDB on AWS cloud using Terraform. You may also wish to consider one of the other approaches.
Terraform module to provision MongoDB using AWS-ECS
Terraform module that provisions Mongo container in ECS-EC2 container instance.
This module creates the following resources required for mongo container to be up and running in ECS cluster.
- ECS cluster
- ECS task definition and service for provided version of mongo
- Roles required for EC2 to execute the task
- EBS volume for persistent storage of mongo container
Approach:
This approach creates an ECS task for mongo and runs/manages that task on EC2 instance of provided instance type. This mongo task
uses a docker plugin called rexray/ebs to provision and use EBS volume for persistent storage of mongo container.

Pros:
- Simplified mongo infra management with just Terraform
- Mongo task gets restarted automatically in case of failure
- Easy version upgrades with minimal downtime
- Auto provision of EBS volume
- Built-in ECS monitoring provided in ECS dashboard
- Data volume will not be lost unless deleted manually.
Cons:
- Rolling deployments are not supported yet due to mongo single instance limitation.
- EBS volume size should be chosen carefully because it is not possible to expand it later as it is managed by docker plugin
Terraform Versions
Terraform - 0.12.24
Terraform AWS provider version - 2.60
Prerequisites
- Configure AWS credentials. Refer this for help.
- Make sure that your AWS user has permissions required to create all resources in the diagram
- Install Terraform (version mentioned above) from here
Usage
Note:
Change the inputs to match your requirement
module "mongo_ecs_ec2_cluster" {
source = "path-to-the-module"
security_group_id = "security-group-id"
subnet_id = "subnet-id"
ebs_volume_size = 5
ebs_volume_type = "gp2"
instance_type = "t3.medium"
name = "mongo"
region = "ap-southeast-1"
stage = "Development"
mongo_container_cpu = 512
mongo_container_memory = 1024
mongo_version = "4.0"
tags = {
Environment = "Development"
TF-Managed = true
}
}
Examples
Try out the module functionality with an example defined here. We are using Terragrunt for remote state management. Do install it in case you want remote backend for state.
- Switch to examples directory
cd examples - Initialize Terraform to download required plugins
terraform init|terragrunt init - Run
planto find out all resources that are going to be createdterraform plan|terragrunt plan - Run
applyto create those resourcesterraform apply|terragrunt apply - Install mongo shell and run
mongo <public_ip>:27017to access mongodb - Make sure to destroy them once you are done exploring
terraform destroy|terragrunt destroy
Note:
- Make sure to delete the EBS volume manually as
terraform destroywon't delete it. - If remote state is used then delete the state management resources (S3 bucket and Dynamo table) created by Terragrunt manually.
Inputs
| Name | Description | Type | Default | Required |
|---|---|---|---|---|
| name | Name to be used on all the resources as identifier | string | n/a |
yes |
| security_group_id | Id of the security group to attach to mongo instance | string | n/a |
yes |
| subnet_id | Id of subnet to create mongo instance in (Private subnet recommended) | string | n/a |
yes |
| ebs_volume_size | Size of EBS volume to be used by mongo container | number | n/a |
yes |
| ebs_volume_type | Type of EBS volume to be used by mongo container | string | n/a |
yes |
| instance_type | Type of EC2 instance to be used by ECS cluster for mongo task | string | n/a |
yes |
| region | Region to be used for creating all the above resources | string | n/a |
yes |
| stage | Stage of the deployment | string | n/a |
yes |
| mongo_container_cpu | CPU capacity to be allocated for mongo container | number | n/a |
yes |
| mongo_container_memory | Memory to be allocated for mongo container | number | n/a |
yes |
| mongo_version | Version tag of mongo docker image | string | n/a |
yes |
| tags | A map of default tags to add to all resources | map | {} | no |
Outputs
| Name | Description |
|---|---|
| ecs_cluster_arn | The arn of mongo ECS cluster |
| ecs_cluster_name | The name of mongo ECS cluster |
| ecs_cluster_region | The region where mongo ECS cluster is created |
| mongo_instance_private_ip | The private IPV4 address of mongo instance to connect to mongodb |
| mongo_instance_public_ip | The public IPV4 address of mongo instance to connect to mongodb (If created in public subnet) |
Contributing
We appreciate your help!
Open an issue or submit a pull request for an enhancement. Browse through the current open issues.
