DEV Community

loading...
Cover image for Terraforming Alibaba Cloud - VPC and vSwitch

Terraforming Alibaba Cloud - VPC and vSwitch

Ankit Mehta
DevOps Engineer working with Central Tech. Helping developers to build secure cloud-native applications. Developing and deploying solutions for multi and poly-cloud environments.
・1 min read

This post will help to build an Alibaba Cloud VPC and vswitches. VPC is an isolated network segment. Vswiches are logically divided VPC into smaller groups. For the people coming from another cloud provider, vswitches are overall subnets.

As a best practice, it is recommended to use all the subnets in different subnets.

This code is an extension of the previous post.

variable "access_key" {}
variable "secret_key" {}
variable "region" {}

#Provider Definition
terraform {
  required_providers {
    alicloud = {
      source  = "aliyun/alicloud"
      version = "1.121.1"
    }
  }
}

#Provider Variables
provider "alicloud" {
  access_key = var.access_key
  secret_key = var.secret_key
  region     = var.region
}

#Resource Management Definition
resource "alicloud_resource_manager_resource_group" "development-rg" {
  resource_group_name = "aliyun-development-rg"
  display_name        = "aliyun-development-rg"
}

#VPC Definition
resource "alicloud_vpc" "vpc" {
  vpc_name          = "Development-VPC"
  description       = "This is development VPC"
  cidr_block        = "192.168.0.0/16"
  resource_group_id = alicloud_resource_manager_resource_group.development-rg.id
  tags = {
    type       = "development"
    managed-by = "Terraform"
  }
}

#VSwich Definition 
resource "alicloud_vswitch" "vswitch-1" {
  cidr_block = "192.168.1.0/24"
  zone_id    = "ap-southeast-1a"
  vpc_id     = alicloud_vpc.vpc.id

  vswitch_name = "development-subnet-1"

  description = "development-subnet-1"

  tags = {
    type       = "development"
    managed-by = "Terraform"
  }
}
resource "alicloud_vswitch" "vswitch-2" {
  cidr_block = "192.168.2.0/24"
  zone_id    = "ap-southeast-1b"
  vpc_id     = alicloud_vpc.vpc.id

  vswitch_name = "development-subnet-2"

  description = "development-subnet-2"

  tags = {
    type       = "development"
    managed-by = "Terraform"
  }
}
resource "alicloud_vswitch" "vswitch-3" {
  cidr_block = "192.168.3.0/24"
  zone_id    = "ap-southeast-1c"
  vpc_id     = alicloud_vpc.vpc.id

  vswitch_name = "development-subnet-3"

  description = "development-subnet-3"

  tags = {
    type       = "development"
    managed-by = "Terraform"
  }
}

Enter fullscreen mode Exit fullscreen mode

Discussion (0)