Alicloud_vpc depending on alicloud_vswitch

I’m writing a terraform codes for AliCloud that will create a new vpc with new vswitch

The problem is it will get error on the first run as the vswitch has not been created yet

I want to ensure vswitch is created before the vpc

I’m aware there’s a Depends on function on TF, how do i use it in this context ?

vpc1 = {

create            = true

name              = "vpcname"

region            = "cn-shanghai"

resource_group_id = ""

cidr              = "10.20.0.0/16"

vswitch = {

  names              = ["vswitchname1", "vswitchname2"]

  availability_zones = ["cn-shanghai-b"           , "cn-shanghai-e"]

  cidrs              = ["10.20.1.0/24"            , "10.20.2.0/24"] 

}

Hi @sklau,

From reading the documentation for alicloud_vswitch and alicloud_vpc, it seems like it is the switch that must depend on the VPC rather than the other way around, because the switch requires the VPC as an argument:

resource "alicloud_vpc" "example" {
  vpc_name   = local.vpc1.name
  cidr_block = local.vpc1.cidr

  resource_group_id = local.vpc1.resource_group_id != "" ? local.vpc1.resource_group_id : null
}

resource "alicloud_vswitch" "example" {
  vpc_id = alicloud_vpc.example.id
  # ...
}

Using alicloud_vpc.example as part of the configuration for alicloud_vswitch.example will create the necessary dependency to ensure that the VPC is created first, and then the vSwitch, which seems to be what this system requires.

I didn’t include the full body of resource "alicloud_vswitch" "example" here because I wasn’t sure what your intent was in having all of the attributes of local.vpc1.vswitch being lists rather than single strings. If your intent was to define more than one vswitch then the more typical way to do that would be a single list of objects, where each object represents all of the data for just one switch, but that’s secondary to what you’re asking so I’ll not elaborate more on that for now.

1 Like