All Products
Search
Document Center

ApsaraDB for MongoDB:Integrate ApsaraDB for MongoDB by using Terraform

Last Updated:Sep 13, 2024

This topic describes how to create an ApsaraDB for MongoDB instance by using Terraform.

For more information about Terraform, see What is Terraform? For more information about the ApsaraDB for MongoDB resource types supported by Terraform, see Overview. You can also query supported resource types by service and resource that work with Terraform. For more information, see Terraform Document.

Resource architecture

image.png

You can create a virtual private cloud (VPC), a vSwitch, and a replica set instance in a specific region.

Procedure

Install Terraform

  • Install and configure Terraform on a local PC. For more information, see Install and configure Terraform in the local PC.

    After Terraform is installed, you can open the command-line interface (CLI) and then enter terraform version. If version information is returned, Terraform is installed.

  • If you do not want to install Terraform, use Cloud Shell provided by Alibaba Cloud. Cloud Shell provides a built-in runtime environment for Terraform.

Grant required permissions to a RAM user

You must grant a Resource Access Management (RAM) user required permissions to execute a Terraform template. You must create a RAM user, obtain an AccessKey pair, and then attach a permission policy to the RAM user. You can use sample Terraform templates to create VPCs, vSwitches, and ApsaraDB for MongoDB instances. You must grant the following permissions to the RAM user:

  • AliyunVPCFullAccess: provides full access to Virtual Private Cloud.

  • AliyunMongoDBFullAccess: provides full access to ApsaraDB for MongoDB.

Create a template

Create a file named main.tf and enter the following content based on the instance architecture that you use.

Standalone instance

# Declare the alicloud provider.
provider "alicloud" {}

# Declare a variable named name.
variable "name" {
  default = "terraform-example"
}

# Specify the alicloud_mongodb_zones parameter to query zone information.
data "alicloud_mongodb_zones" "default" {
  
}

# Set the zone_id parameter to the ID of the last zone in the value of the alicloud_mongodb_zones parameter.
locals {
  index   = length(data.alicloud_mongodb_zones.default.zones) - 1
  zone_id = data.alicloud_mongodb_zones.default.zones[local.index].id
}

# Create a VPC.
resource "alicloud_vpc" "default" {
  vpc_name   = var.name
  description = var.name
  cidr_block = "172.17.3.0/24"
}

# Create a vSwitch under the VPC in the zone specified by the local.zone_id parameter.
resource "alicloud_vswitch" "default" {
  vswitch_name = var.name
  cidr_block   = "172.17.3.0/24"
  vpc_id       = alicloud_vpc.default.id
  zone_id      = local.zone_id
}

# Use the VPC and vSwitch to create a standalone resource.
resource "alicloud_mongodb_instance" "singleNode" {
  engine_version      = "4.0"
  db_instance_class   = "dds.sn4.xlarge.1"
  db_instance_storage = 20
  vswitch_id          = alicloud_vswitch.default.id
  zone_id             = local.zone_id
  security_ip_list    = [
        "10.168.1.12",
        "100.69.7.112"
    ]
  name                = var.name
  storage_type        = "cloud_essd1"
  tags = {
    Created = "TF"
    For     = "example"
    }
}

For more information about how to configure the alicloud_mongodb_instance resource type, see alicloud_mongodb_instance.

Replica set instance

# Declare the alicloud provider.
provider "alicloud" {}

# Declare a variable named name.
variable "name" {
  default = "terraform-example"
}

# Specify the alicloud_mongodb_zones parameter to query zone information.
data "alicloud_mongodb_zones" "default" {
  
}

# Set the zone_id parameter to the ID of the last zone in the value of the alicloud_mongodb_zones parameter.
locals {
  index   = length(data.alicloud_mongodb_zones.default.zones) - 1
  zone_id = data.alicloud_mongodb_zones.default.zones[local.index].id
}

# Create a VPC.
resource "alicloud_vpc" "default" {
  vpc_name   = var.name
  description = var.name
  cidr_block = "172.17.3.0/24"
}

# Create a vSwitch under the VPC in the zone specified by the local.zone_id parameter.
resource "alicloud_vswitch" "default" {
  vswitch_name = var.name
  cidr_block   = "172.17.3.0/24"
  vpc_id       = alicloud_vpc.default.id
  zone_id      = local.zone_id
}

# Use the VPC and VSwitch to create a replica set resource.
resource "alicloud_mongodb_instance" "default" {
  engine_version      = "5.0"
  db_instance_class   = "mdb.shard.2x.xlarge.d"
  db_instance_storage = 20
  vswitch_id          = alicloud_vswitch.default.id
  security_ip_list    = ["10.168.1.12", "100.69.7.112"]
  name                = var.name
  tags = {
    Created = "TF"
    For     = "example"
  }
}

For more information about how to configure the alicloud_mongodb_instance resource type, see alicloud_mongodb_instance.

Sharded cluster instance

# Declare the alicloud provider.
provider "alicloud" {}

# Declare a variable named name.
variable "name" {
  default = "terraform-example"
}

# Specify the alicloud_mongodb_zones parameter to query zone information.
data "alicloud_mongodb_zones" "default" {
  
}

# Set the zone_id parameter to the ID of the last zone in the value of the alicloud_mongodb_zones parameter.
locals {
  index   = length(data.alicloud_mongodb_zones.default.zones) - 1
  zone_id = data.alicloud_mongodb_zones.default.zones[local.index].id
}

# Create a VPC.
resource "alicloud_vpc" "default" {
  vpc_name   = var.name
  description = var.name
  cidr_block = "172.17.3.0/24"
}

# Create a vSwitch under the VPC in the zone specified by the local.zone_id parameter.
resource "alicloud_vswitch" "default" {
  vswitch_name = var.name
  cidr_block   = "172.17.3.0/24"
  vpc_id       = alicloud_vpc.default.id
  zone_id      = local.zone_id
}

# Use the VPC and VSwitch to create a sharded cluster resource.
resource "alicloud_mongodb_sharding_instance" "default" {
  engine_version      = "7.0"
  vswitch_id          = alicloud_vswitch.default.id
  name                = var.name
  zone_id = local.zone_id
  mongo_list {
    node_class = "mdb.shard.2x.large.c"
  }
  mongo_list {
    node_class = "mdb.shard.2x.large.c"
  }
  shard_list {
    node_class   = "mdb.shard.2x.large.c"
    node_storage = "20"
  }
  shard_list {
    node_class        = "mdb.shard.2x.large.c"
    node_storage      = "20"
    readonly_replicas = "1"
  }
  config_server_list {
    node_class ="mdb.shard.2x.large.c"
    node_storage = "20"
  }
  tags = {
    Created = "TF"
    For     = "Example"
  }
}

For more information about how to configure the alicloud_mongodb_sharding_instance resource type, see alicloud_mongodb_sharding_instance.

Note

If an error indicating that a specified parameter is not found is returned when you run the template, you can run the terraform version command to check whether the current version of Terraform is the same as its official version. If the current version is not the latest version, you must execute the following statement to specify the latest version in the provider "alicloud" {} field of the main.tf file. For more information about the official version of Terraform, see alicloud_mongodb_instance.

provider "alicloud" {version = "~> v*.***.*"}

Replace v*.***.* with the latest version number.

Execute the template

  1. Initialize Terraform configuration files, such as the configuration files for loaded providers, provisioners, and modules.

    terraform init

    image

  2. Check whether the template syntax is correct.

    terraform validate

    image

  3. Create an execution plan.

    terraform plan

    7dee7a7bdb291024a81457306cec277c

  4. Deploy the template.

    terraform apply

    image.png

    The deployment information is recorded in logs.

    image

    Enter yes to change resources.

  5. Release the resources that are created from the template.

    terraform destroy

    imageimageEnter yes to destroy the resources.