├── .github └── workflows │ ├── e2e.yml │ └── weekly_e2e.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README-CN.md ├── README.md ├── TestRecord.md ├── examples ├── complete │ ├── README.md │ ├── main.tf │ └── outputs.tf └── use-default-vpc │ ├── README.md │ ├── main.tf │ └── outputs.tf ├── locals.tf ├── main.tf ├── outputs.tf ├── scripts ├── curl_fc_trigger.go ├── e2e_check.go └── terraform-test.sh ├── variables.tf └── versions.tf /.github/workflows/e2e.yml: -------------------------------------------------------------------------------- 1 | name: E2E Test Check 2 | on: 3 | pull_request: 4 | branches: 5 | - master 6 | - main 7 | types: [ 'opened', 'synchronize' ] 8 | paths: 9 | - '.github/**' 10 | - '.github/workflows/**' 11 | - '**/*.tf' 12 | 13 | jobs: 14 | terraform-fmt: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - name: checkout 18 | uses: actions/checkout@v3 19 | - name: fmt-check 20 | run: | 21 | if [ ! -f /usr/local/bin/terraform ]; then 22 | wget -q https://releases.hashicorp.com/terraform/1.6.0/terraform_1.6.0_linux_amd64.zip 23 | unzip terraform_1.6.0_linux_amd64.zip -d /usr/local/bin/ 24 | fi 25 | error=false 26 | echo "===> Terraform fmt -diff checking" 27 | terraform fmt -diff -recursive -check 28 | if [[ $? -ne 0 ]]; then 29 | echo -e "\033[31m[ERROR]\033[0m: Some codes has not been formatted, and please running terraform fmt --recursive command before pushing." 30 | exit 1 31 | fi 32 | 33 | terraform-validate: 34 | runs-on: ubuntu-latest 35 | steps: 36 | - name: checkout 37 | uses: actions/checkout@v3 38 | - name: validate-check 39 | run: | 40 | if [ ! -f /usr/local/bin/terraform ]; then 41 | wget -q https://releases.hashicorp.com/terraform/1.6.0/terraform_1.6.0_linux_amd64.zip 42 | unzip terraform_1.6.0_linux_amd64.zip -d /usr/local/bin/ 43 | fi 44 | exp="examples" 45 | output_file="combined_output.txt" 46 | if [[ -d "$exp" ]]; then 47 | find $exp -type d -print -mindepth 1 -maxdepth 1 >> $output_file 48 | fi 49 | 50 | exitCode=0 51 | while IFS= read -r line 52 | do 53 | echo "===> Terraform validate checking in $line" 54 | terraform -chdir=$line init -upgrade 55 | terraform -chdir=$line validate 56 | if [[ $? -ne 0 ]]; then 57 | echo -e "\033[31m[ERROR]\033[0m: Some codes contain errors, and please running terraform validate command before pushing." 58 | exitCode=1 59 | fi 60 | done < $output_file 61 | rm $output_file 62 | exit $exitCode 63 | 64 | tflint: 65 | runs-on: ubuntu-latest 66 | steps: 67 | - name: checkout 68 | uses: actions/checkout@v3 69 | 70 | - uses: actions/checkout@v4 71 | name: Checkout source code 72 | 73 | - uses: actions/cache@v4 74 | name: Cache plugin dir 75 | with: 76 | path: ~/.tflint.d/plugins 77 | key: ${{ matrix.os }}-tflint-${{ hashFiles('.tflint.hcl') }} 78 | 79 | - uses: terraform-linters/setup-tflint@v4 80 | name: Setup TFLint 81 | with: 82 | tflint_version: v0.52.0 83 | 84 | - name: Init TFLint 85 | run: tflint --init 86 | env: 87 | GITHUB_TOKEN: ${{ github.token }} 88 | 89 | - name: tflint 90 | run: | 91 | tflint --recursive \ 92 | --enable-rule=terraform_comment_syntax \ 93 | --enable-rule=terraform_deprecated_index \ 94 | --enable-rule=terraform_deprecated_interpolation \ 95 | --enable-rule=terraform_deprecated_lookup \ 96 | --enable-rule=terraform_documented_outputs \ 97 | --enable-rule=terraform_documented_variables \ 98 | --enable-rule=terraform_typed_variables \ 99 | --enable-rule=terraform_unused_declarations \ 100 | --enable-rule=terraform_required_version \ 101 | --enable-rule=terraform_standard_module_structure \ 102 | --disable-rule=terraform_required_providers 103 | if [[ $? -ne 0 ]]; then 104 | exit_code=1 105 | fi 106 | 107 | e2e-check: 108 | # if: github.event.review.state == 'approved' || github.event.review.body == 'approved' 109 | needs: [terraform-fmt, terraform-validate, tflint] 110 | runs-on: ubuntu-latest 111 | name: 'e2e check' 112 | steps: 113 | - name: checkout 114 | uses: actions/checkout@v3 115 | - name: set id 116 | id: set-job-id 117 | uses: ayachensiyuan/get-action-job-id@v1.6 118 | env: 119 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 120 | with: 121 | job-name: 'e2e check' 122 | - name: Get pull request info 123 | run: | 124 | echo "repo name is" ${{github.event.pull_request.head.repo.full_name}} 125 | echo "branch is" ${{github.event.pull_request.head.ref}} 126 | echo "The current job id is ${{ steps.set-job-id.outputs.jobId }}" 127 | - name: e2e test 128 | run: | 129 | objectPath="github-action/${{github.repository}}/e2e/Action-${{github.run_number}}-${{github.run_id}}-${{ steps.set-job-id.outputs.jobId }}" 130 | go run scripts/curl_fc_trigger.go ${{github.event.pull_request.head.ref}} ${{github.event.pull_request.head.repo.full_name}} ${objectPath} 131 | go run scripts/e2e_check.go ${objectPath} -------------------------------------------------------------------------------- /.github/workflows/weekly_e2e.yml: -------------------------------------------------------------------------------- 1 | name: Weekly E2E Test Check 2 | on: 3 | workflow_dispatch: 4 | schedule: 5 | - cron: '0 0 * * 0' 6 | 7 | jobs: 8 | weekly-e2e-check: 9 | if: github.repository_owner == 'alibabacloud-automation' 10 | name: 'weekly e2e check' 11 | runs-on: ubuntu-latest 12 | permissions: write-all 13 | steps: 14 | - name: checkout 15 | uses: actions/checkout@v3 16 | - name: set id 17 | id: set-job-id 18 | uses: ayachensiyuan/get-action-job-id@v1.6 19 | env: 20 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 21 | with: 22 | job-name: 'weekly e2e check' 23 | - name: Get job id 24 | run: | 25 | echo "The current job id is ${{ steps.set-job-id.outputs.jobId }}" 26 | - name: Extract branch name 27 | shell: bash 28 | run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})" 29 | id: extract_branch 30 | - name: weekly e2e test 31 | run: | 32 | objectPath="github-action/${{github.repository}}/weekly-e2e/Action-${{github.run_number}}-${{github.run_id}}-${{ steps.set-job-id.outputs.jobId }}" 33 | echo "default branch: ${{ steps.extract_branch.outputs.branch }}" 34 | go run scripts/curl_fc_trigger.go ${{ steps.extract_branch.outputs.branch }} ${{github.repository}} ${objectPath} 35 | go run scripts/e2e_check.go ${objectPath} 36 | - name: whether to upload test record 37 | id: whether-to-upload-test-record 38 | run: | 39 | REPO_NAME=$(echo $GITHUB_REPOSITORY | cut -d'/' -f2) 40 | modules=$(curl -sL "https://terraform-fc-test-for-example-module.oss-ap-southeast-1.aliyuncs.com/testRecordReleased.json") 41 | if echo "$modules" | jq --arg key "$REPO_NAME" -re 'has($key)' | grep -q true; then 42 | echo "The key exists at the JSON object." 43 | echo "::set-output name=output_value::0" 44 | else 45 | echo "The key does not exist at the JSON object." 46 | echo "::set-output name=output_value::1" 47 | fi 48 | - name: update test record 49 | if: steps.whether-to-upload-test-record.outputs.output_value == 1 50 | run: | 51 | git add TestRecord.md 52 | cd .git 53 | sudo chmod -R a+rwX . 54 | sudo find . -type d -exec chmod g+s '{}' + 55 | - name: Commit & Push changes 56 | if: steps.whether-to-upload-test-record.outputs.output_value == 1 57 | uses: actions-js/push@master 58 | with: 59 | github_token: ${{ secrets.GITHUB_TOKEN }} 60 | message: 'Update TestRecord' 61 | branch: ${{ steps.extract_branch.outputs.branch }} -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled files 2 | *.tfstate 3 | *.tfstate.backup 4 | *.terraform.* 5 | 6 | # Module directory 7 | .terraform/ 8 | 9 | # terraform log 10 | *.log 11 | 12 | # auto-generated key pair file 13 | *.pem 14 | 15 | # tools files 16 | .DS_Store 17 | .idea 18 | 19 | # others 20 | *.bak 21 | *.bk 22 | **/.terraform/* 23 | .terraform.lock.hcl 24 | .terraform.tfstate.lock.info -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 1.12.0 (Unreleased) 2 | ## 1.11.0 (September 10, 2024) 3 | 4 | - module: improve the data source alicloud_vpcs [GH-45](https://github.com/alibabacloud-automation/terraform-alicloud-vpc/pull/45) 5 | 6 | ## 1.10.0 (April 13, 2022) 7 | IMPROVEMENTS: 8 | - Modified author contact information [GH-35](https://github.com/terraform-alicloud-modules/terraform-alicloud-vpc/pull/35) 9 | 10 | BUG FIXES: 11 | - Fixed Invalid index bug and deprecated attribute name [GH-39](https://github.com/terraform-alicloud-modules/terraform-alicloud-vpc/pull/39) 12 | 13 | ## 1.9.0 (August 29, 2021) 14 | 15 | IMPROVEMENTS: 16 | - Removes the provider setting and improves the Readme [GH-34](https://github.com/terraform-alicloud-modules/terraform-alicloud-vpc/pull/34) 17 | 18 | ## 1.8.1 (July 28, 2020) 19 | 20 | BUG FIXES: 21 | 22 | - Fix the output of `resource_group_id` [GH-27](https://github.com/terraform-alicloud-modules/terraform-alicloud-vpc/pull/27) 23 | 24 | ## 1.8.0 (July 20, 2020) 25 | 26 | IMPROVEMENTS: 27 | 28 | - Support `resource_group_id` parameter and add README-CN docs [GH-26](https://github.com/terraform-alicloud-modules/terraform-alicloud-vpc/pull/26) 29 | 30 | ## 1.7.1 (Feburary 24, 2020) 31 | 32 | IMPROVEMENTS: 33 | 34 | - add profile for readme and examples [GH-25](https://github.com/terraform-alicloud-modules/terraform-alicloud-vpc/pull/25) 35 | 36 | ## 1.7.0 (Feburary 20, 2020) 37 | 38 | IMPROVEMENTS: 39 | 40 | - improve outputs name [GH-24](https://github.com/terraform-alicloud-modules/terraform-alicloud-vpc/pull/24) 41 | - improve(vpc): Increased output parameters [GH-23](https://github.com/terraform-alicloud-modules/terraform-alicloud-vpc/pull/23) 42 | 43 | ## 1.6.0 (Feburary 17, 2020) 44 | 45 | IMPROVEMENTS: 46 | 47 | - improve(vpc): modify provider version issues [GH-21](https://github.com/terraform-alicloud-modules/terraform-alicloud-vpc/pull/21) 48 | 49 | ## 1.5.0 (February 1, 2020) 50 | 51 | IMPROVEMENTS: 52 | 53 | - add create parameter [GH-19](https://github.com/terraform-alicloud-modules/terraform-alicloud-vpc/pull/19) 54 | - add tags for the resources [GH-19](https://github.com/terraform-alicloud-modules/terraform-alicloud-vpc/pull/19) 55 | - add examples for it [GH-19](https://github.com/terraform-alicloud-modules/terraform-alicloud-vpc/pull/19) 56 | 57 | ## 1.4.2 (December 5, 2019) 58 | 59 | IMPROVEMENTS: 60 | 61 | - update readme [GH-17](https://github.com/terraform-alicloud-modules/terraform-alicloud-vpc/pull/17) 62 | 63 | BUG FIXES: 64 | 65 | - fix output parameter this_route_table_id grammar error [GH-18](https://github.com/terraform-alicloud-modules/terraform-alicloud-vpc/pull/18) 66 | 67 | ## 1.4.1 (December 2, 2019) 68 | 69 | BUG FIXES: 70 | 71 | - fix output and input parameter grammar error [GH-16](https://github.com/terraform-alicloud-modules/terraform-alicloud-vpc/pull/16) 72 | 73 | ## 1.4.0 (December 2, 2019) 74 | 75 | IMPROVEMENTS: 76 | 77 | - support retrieving the existing VPC by name regex and tags automatically [GH-15](https://github.com/terraform-alicloud-modules/terraform-alicloud-vpc/pull/15) 78 | 79 | BUG FIXES: 80 | 81 | - Fix count.index error [GH-15](https://github.com/terraform-alicloud-modules/terraform-alicloud-vpc/pull/15) 82 | 83 | ## 1.3.1 (October 30, 2019) 84 | 85 | IMPROVEMENTS: 86 | 87 | - Support output cidr_blocks 88 | 89 | 90 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Terraform Alibaba Cloud Modules 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README-CN.md: -------------------------------------------------------------------------------- 1 | Alicloud VPC, VSwitch and Route Entry Terraform Module 2 | terraform-alicloud-vpc 3 | ===================================================================== 4 | 5 | 本 Module,用于创建阿里云 VPC、多个虚拟交换机和配置路由条目。 6 | 7 | - 该模块包含一个 VPC、几个交换机和几个自定义路由条目。 8 | - 如果未指定 VPC,则模块将使用默认参数创建一个新资源。 9 | - 虚拟交换机的数量取决于参数 `VSwitch_cidrs` 的数量。 10 | - 自定义路由条目的数量取决于参数 `destination_cidr` 的数量。 11 | - 每个虚拟交换机需要一个可用区。如果 `availability_zones` 的数量小于 `vswitch_cidrs` 的数量,`availability_zones` 将被重复使用。 12 | 13 | 本 Module 支持创建以下资源: 14 | 15 | * [VPC](https://www.terraform.io/docs/providers/alicloud/r/vpc.html) 16 | * [VSwitch](https://www.terraform.io/docs/providers/alicloud/r/vswitch.html) 17 | * [Route Entry](https://www.terraform.io/docs/providers/alicloud/r/route_entry.html) 18 | 19 | ## Terraform 版本 20 | 21 | 本 Module 要求使用 Terraform 0.13 和 阿里云 Provider 1.56.0+。 22 | 23 | ## 用法 24 | 25 | ```hcl 26 | module "vpc" { 27 | source = "alibaba/vpc/alicloud" 28 | region = "cn-hangzhou" 29 | profile = "Your-Profile-Name" 30 | 31 | create = true 32 | vpc_name = "my-env-vpc" 33 | vpc_cidr = "10.10.0.0/16" 34 | resource_group_id = "rg-acfmwvvtg5o****" 35 | 36 | availability_zones = ["cn-hangzhou-e", "cn-hangzhou-f", "cn-hangzhou-g"] 37 | vswitch_cidrs = ["10.10.1.0/24", "10.10.2.0/24", "10.10.3.0/24"] 38 | 39 | vpc_tags = { 40 | Owner = "user" 41 | Environment = "staging" 42 | Name = "complete" 43 | } 44 | 45 | vswitch_tags = { 46 | Project = "Secret" 47 | Endpoint = "true" 48 | } 49 | 50 | destination_cidrs = var.destination_cidrs 51 | nexthop_ids = var.server_ids 52 | } 53 | ``` 54 | 55 | ## 示例 56 | 57 | * [创建完整 VPC 示例](https://github.com/terraform-alicloud-modules/terraform-alicloud-vpc/tree/master/examples/complete) 58 | * [使用默认 VPC 示例](https://github.com/terraform-alicloud-modules/terraform-alicloud-vpc/tree/master/examples/use-default-vpc) 59 | 60 | ## 注意事项 61 | 本Module从版本v1.9.0开始已经移除掉如下的 provider 的显式设置: 62 | 63 | ```hcl 64 | provider "alicloud" { 65 | profile = var.profile != "" ? var.profile : null 66 | shared_credentials_file = var.shared_credentials_file != "" ? var.shared_credentials_file : null 67 | region = var.region != "" ? var.region : null 68 | skip_region_validation = var.skip_region_validation 69 | configuration_source = "terraform-alicloud-modules/vpc" 70 | } 71 | ``` 72 | 73 | 如果你依然想在Module中使用这个 provider 配置,你可以在调用Module的时候,指定一个特定的版本,比如 1.8.0: 74 | 75 | ```hcl 76 | module "vpc" { 77 | source = "alibaba/vpc/alicloud" 78 | 79 | version = "1.8.0" 80 | region = "cn-hangzhou" 81 | profile = "Your-Profile-Name" 82 | 83 | create = true 84 | vpc_name = "my-env-vpc" 85 | // ... 86 | } 87 | ``` 88 | 89 | 如果你想对正在使用中的Module升级到 1.9.0 或者更高的版本,那么你可以在模板中显式定义一个相同Region的provider: 90 | ```hcl 91 | provider "alicloud" { 92 | region = "cn-hangzhou" 93 | profile = "Your-Profile-Name" 94 | } 95 | module "vpc" { 96 | source = "alibaba/vpc/alicloud" 97 | 98 | create = true 99 | vpc_name = "my-env-vpc" 100 | // ... 101 | } 102 | ``` 103 | 或者,如果你是多Region部署,你可以利用 `alias` 定义多个 provider,并在Module中显式指定这个provider: 104 | 105 | ```hcl 106 | provider "alicloud" { 107 | region = "cn-hangzhou" 108 | profile = "Your-Profile-Name" 109 | alias = "hz" 110 | } 111 | 112 | module "vpc" { 113 | source = "alibaba/vpc/alicloud" 114 | 115 | providers = { 116 | alicloud = alicloud.hz 117 | } 118 | 119 | create = true 120 | vpc_name = "my-env-vpc" 121 | // ... 122 | } 123 | ``` 124 | 125 | 定义完provider之后,运行命令 `terraform init` 和 `terraform apply` 来让这个provider生效即可。 126 | 127 | 更多provider的使用细节,请移步[How to use provider in the module](https://www.terraform.io/docs/language/modules/develop/providers.html#passing-providers-explicitly) 128 | 129 | ## Terraform 版本 130 | 131 | | Name | Version | 132 | |------|---------| 133 | | [terraform](#requirement\_terraform) | >= 0.13.0 | 134 | | [alicloud](#requirement\_alicloud) | >= 1.56.0 | 135 | 136 | 提交问题 137 | ------- 138 | 如果在使用该 Terraform Module 的过程中有任何问题,可以直接创建一个 [Provider Issue](https://github.com/terraform-providers/terraform-provider-alicloud/issues/new),我们将根据问题描述提供解决方案。 139 | 140 | **注意:** 不建议在该 Module 仓库中直接提交 Issue。 141 | 142 | 作者 143 | ------- 144 | Created and maintained by Alibaba Cloud Terraform Team(terraform@alibabacloud.com) 145 | 146 | 许可 147 | ---- 148 | Apache 2 Licensed. See LICENSE for full details. 149 | 150 | 参考 151 | --------- 152 | * [Terraform-Provider-Alicloud Github](https://github.com/terraform-providers/terraform-provider-alicloud) 153 | * [Terraform-Provider-Alicloud Release](https://releases.hashicorp.com/terraform-provider-alicloud/) 154 | * [Terraform-Provider-Alicloud Docs](https://www.terraform.io/docs/providers/alicloud/index.html) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Alicloud VPC, VSwitch and Route Entry Terraform Module 2 | terraform-alicloud-vpc 3 | ========================================= 4 | 5 | English | [简体中文](https://github.com/terraform-alicloud-modules/terraform-alicloud-vpc/blob/master/README-CN.md) 6 | 7 | A terraform module used to create an Alibaba Cloud VPC, several VSwitches and configure route entry. 8 | 9 | - The module contains one VPC, several VSwitches and several custom route entries. 10 | - If VPC is not specified, the module will launch a new one using default parameters. 11 | - The number of VSwitch depends on the length of the parameter `vswitch_cidrs`. 12 | - The number of custom route entry depends on the length of the parameter `destination_cidrs` 13 | - Each VSwitch needs an availability zone. If the length of `availability_zones` is less than the length of `vswitch_cidrs`, `availability_zones` item will be used repeatedly. 14 | 15 | The following resources are supported: 16 | 17 | * [VPC](https://www.terraform.io/docs/providers/alicloud/r/vpc.html) 18 | * [VSwitch](https://www.terraform.io/docs/providers/alicloud/r/vswitch.html) 19 | * [Route Entry](https://www.terraform.io/docs/providers/alicloud/r/route_entry.html) 20 | 21 | Usage 22 | ----- 23 | 24 | ```hcl 25 | module "vpc" { 26 | source = "alibaba/vpc/alicloud" 27 | 28 | create = true 29 | vpc_name = "my-env-vpc" 30 | vpc_cidr = "10.10.0.0/16" 31 | resource_group_id = "rg-acfmwvvtg5o****" 32 | 33 | availability_zones = ["cn-hangzhou-e", "cn-hangzhou-f", "cn-hangzhou-g"] 34 | vswitch_cidrs = ["10.10.1.0/24", "10.10.2.0/24", "10.10.3.0/24"] 35 | 36 | vpc_tags = { 37 | Owner = "user" 38 | Environment = "staging" 39 | Name = "complete" 40 | } 41 | 42 | vswitch_tags = { 43 | Project = "Secret" 44 | Endpoint = "true" 45 | } 46 | 47 | destination_cidrs = var.destination_cidrs 48 | nexthop_ids = var.server_ids 49 | } 50 | ``` 51 | 52 | ## Examples 53 | 54 | * [Complete VPC example](https://github.com/terraform-alicloud-modules/terraform-alicloud-vpc/tree/master/examples/complete) 55 | * [Use Default VPC example](https://github.com/terraform-alicloud-modules/terraform-alicloud-vpc/tree/master/examples/use-default-vpc) 56 | 57 | ## Notes 58 | From the version v1.9.0, the module has removed the following `provider` setting: 59 | 60 | ```hcl 61 | provider "alicloud" { 62 | profile = var.profile != "" ? var.profile : null 63 | shared_credentials_file = var.shared_credentials_file != "" ? var.shared_credentials_file : null 64 | region = var.region != "" ? var.region : null 65 | skip_region_validation = var.skip_region_validation 66 | configuration_source = "terraform-alicloud-modules/vpc" 67 | } 68 | ``` 69 | 70 | If you still want to use the `provider` setting to apply this module, you can specify a supported version, like 1.8.0: 71 | 72 | ```hcl 73 | module "vpc" { 74 | source = "alibaba/vpc/alicloud" 75 | 76 | version = "1.8.0" 77 | region = "cn-hangzhou" 78 | profile = "Your-Profile-Name" 79 | 80 | create = true 81 | vpc_name = "my-env-vpc" 82 | // ... 83 | } 84 | ``` 85 | 86 | If you want to upgrade the module to 1.9.0 or higher in-place, you can define a provider which same region with 87 | previous region: 88 | 89 | ```hcl 90 | provider "alicloud" { 91 | region = "cn-hangzhou" 92 | profile = "Your-Profile-Name" 93 | } 94 | module "vpc" { 95 | source = "alibaba/vpc/alicloud" 96 | 97 | create = true 98 | vpc_name = "my-env-vpc" 99 | // ... 100 | } 101 | ``` 102 | or specify an alias provider with a defined region to the module using `providers`: 103 | 104 | ```hcl 105 | provider "alicloud" { 106 | region = "cn-hangzhou" 107 | profile = "Your-Profile-Name" 108 | alias = "hz" 109 | } 110 | 111 | module "vpc" { 112 | source = "alibaba/vpc/alicloud" 113 | 114 | providers = { 115 | alicloud = alicloud.hz 116 | } 117 | 118 | create = true 119 | vpc_name = "my-env-vpc" 120 | // ... 121 | } 122 | ``` 123 | 124 | and then run `terraform init` and `terraform apply` to make the defined provider effect to the existing module state. 125 | 126 | More details see [How to use provider in the module](https://www.terraform.io/docs/language/modules/develop/providers.html#passing-providers-explicitly) 127 | 128 | ## Terraform versions 129 | 130 | | Name | Version | 131 | |------|---------| 132 | | [terraform](#requirement\_terraform) | >= 0.13.0 | 133 | | [alicloud](#requirement\_alicloud) | >= 1.56.0 | 134 | 135 | Submit Issues 136 | ------------- 137 | If you have any problems when using this module, please opening a [provider issue](https://github.com/terraform-providers/terraform-provider-alicloud/issues/new) and let us know. 138 | 139 | **Note:** There does not recommend to open an issue on this repo. 140 | 141 | Authors 142 | ------- 143 | Created and maintained by Alibaba Cloud Terraform Team(terraform@alibabacloud.com) 144 | 145 | License 146 | ---- 147 | Apache 2 Licensed. See LICENSE for full details. 148 | 149 | Reference 150 | --------- 151 | * [Terraform-Provider-Alicloud Github](https://github.com/terraform-providers/terraform-provider-alicloud) 152 | * [Terraform-Provider-Alicloud Release](https://releases.hashicorp.com/terraform-provider-alicloud/) 153 | * [Terraform-Provider-Alicloud Docs](https://www.terraform.io/docs/providers/alicloud/index.html) -------------------------------------------------------------------------------- /TestRecord.md: -------------------------------------------------------------------------------- 1 | ## 08 Jun 2025 03:28 UTC 2 | 3 | success : false 4 | 5 | ### Versions 6 | 7 | Terraform v1.6.0 8 | on linux_amd64 9 | + provider registry.terraform.io/hashicorp/alicloud v1.250.0 10 | 11 | ## 01 Jun 2025 04:01 UTC 12 | 13 | success : true 14 | 15 | ### Versions 16 | 17 | Terraform v1.6.0 18 | on linux_amd64 19 | + provider registry.terraform.io/hashicorp/alicloud v1.250.0 20 | 21 | ## 25 May 2025 02:22 UTC 22 | 23 | success : true 24 | 25 | ### Versions 26 | 27 | Terraform v1.6.0 28 | on linux_amd64 29 | + provider registry.terraform.io/hashicorp/alicloud v1.249.0 30 | 31 | ## 18 May 2025 03:25 UTC 32 | 33 | success : true 34 | 35 | ### Versions 36 | 37 | Terraform v1.6.0 38 | on linux_amd64 39 | + provider registry.terraform.io/hashicorp/alicloud v1.249.0 40 | 41 | ## 11 May 2025 03:16 UTC 42 | 43 | success : true 44 | 45 | ### Versions 46 | 47 | Terraform v1.6.0 48 | on linux_amd64 49 | + provider registry.terraform.io/hashicorp/alicloud v1.248.0 50 | 51 | ## 04 May 2025 02:37 UTC 52 | 53 | success : false 54 | 55 | ### Versions 56 | 57 | Terraform v1.6.0 58 | on linux_amd64 59 | + provider registry.terraform.io/hashicorp/alicloud v1.248.0 60 | 61 | ## 27 Apr 2025 03:24 UTC 62 | 63 | success : false 64 | 65 | ### Versions 66 | 67 | Terraform v1.6.0 68 | on linux_amd64 69 | + provider registry.terraform.io/hashicorp/alicloud v1.248.0 70 | 71 | ## 20 Apr 2025 03:37 UTC 72 | 73 | success : true 74 | 75 | ### Versions 76 | 77 | Terraform v1.6.0 78 | on linux_amd64 79 | + provider registry.terraform.io/hashicorp/alicloud v1.248.0 80 | 81 | ## 13 Apr 2025 03:42 UTC 82 | 83 | success : true 84 | 85 | ### Versions 86 | 87 | Terraform v1.6.0 88 | on linux_amd64 89 | + provider registry.terraform.io/hashicorp/alicloud v1.247.0 90 | 91 | ## 06 Apr 2025 02:23 UTC 92 | 93 | success : true 94 | 95 | ### Versions 96 | 97 | Terraform v1.6.0 98 | on linux_amd64 99 | + provider registry.terraform.io/hashicorp/alicloud v1.247.0 100 | 101 | ## 30 Mar 2025 02:20 UTC 102 | 103 | success : true 104 | 105 | ### Versions 106 | 107 | Terraform v1.6.0 108 | on linux_amd64 109 | + provider registry.terraform.io/hashicorp/alicloud v1.246.2 110 | 111 | ## 23 Mar 2025 03:29 UTC 112 | 113 | success : true 114 | 115 | ### Versions 116 | 117 | Terraform v1.6.0 118 | on linux_amd64 119 | + provider registry.terraform.io/hashicorp/alicloud v1.245.0 120 | 121 | ## 16 Mar 2025 07:00 UTC 122 | 123 | success : true 124 | 125 | ### Versions 126 | 127 | Terraform v1.6.0 128 | on linux_amd64 129 | + provider registry.terraform.io/hashicorp/alicloud v1.244.0 130 | 131 | ## 09 Mar 2025 04:27 UTC 132 | 133 | success : true 134 | 135 | ### Versions 136 | 137 | Terraform v1.6.0 138 | on linux_amd64 139 | + provider registry.terraform.io/hashicorp/alicloud v1.244.0 140 | 141 | ## 02 Mar 2025 07:04 UTC 142 | 143 | success : true 144 | 145 | ### Versions 146 | 147 | Terraform v1.6.0 148 | on linux_amd64 149 | + provider registry.terraform.io/hashicorp/alicloud v1.244.0 150 | 151 | ## 23 Feb 2025 06:02 UTC 152 | 153 | success : true 154 | 155 | ### Versions 156 | 157 | Terraform v1.6.0 158 | on linux_amd64 159 | + provider registry.terraform.io/hashicorp/alicloud v1.243.0 160 | 161 | ## 16 Feb 2025 03:59 UTC 162 | 163 | success : true 164 | 165 | ### Versions 166 | 167 | Terraform v1.6.0 168 | on linux_amd64 169 | + provider registry.terraform.io/hashicorp/alicloud v1.242.0 170 | 171 | ## 09 Feb 2025 05:04 UTC 172 | 173 | success : true 174 | 175 | ### Versions 176 | 177 | Terraform v1.6.0 178 | on linux_amd64 179 | + provider registry.terraform.io/hashicorp/alicloud v1.242.0 180 | 181 | ## 02 Feb 2025 05:05 UTC 182 | 183 | success : true 184 | 185 | ### Versions 186 | 187 | Terraform v1.6.0 188 | on linux_amd64 189 | + provider registry.terraform.io/hashicorp/alicloud v1.242.0 190 | 191 | ## 26 Jan 2025 04:37 UTC 192 | 193 | success : true 194 | 195 | ### Versions 196 | 197 | Terraform v1.6.0 198 | on linux_amd64 199 | + provider registry.terraform.io/hashicorp/alicloud v1.242.0 200 | 201 | ## 19 Jan 2025 05:02 UTC 202 | 203 | success : true 204 | 205 | ### Versions 206 | 207 | Terraform v1.6.0 208 | on linux_amd64 209 | + provider registry.terraform.io/hashicorp/alicloud v1.241.0 210 | 211 | ## 12 Jan 2025 05:23 UTC 212 | 213 | success : true 214 | 215 | ### Versions 216 | 217 | Terraform v1.6.0 218 | on linux_amd64 219 | + provider registry.terraform.io/hashicorp/alicloud v1.240.0 220 | 221 | ## 05 Jan 2025 05:15 UTC 222 | 223 | success : true 224 | 225 | ### Versions 226 | 227 | Terraform v1.6.0 228 | on linux_amd64 229 | + provider registry.terraform.io/hashicorp/alicloud v1.240.0 230 | 231 | ## 29 Dec 2024 04:19 UTC 232 | 233 | success : true 234 | 235 | ### Versions 236 | 237 | Terraform v1.6.0 238 | on linux_amd64 239 | + provider registry.terraform.io/hashicorp/alicloud v1.239.0 240 | 241 | ## 22 Dec 2024 04:41 UTC 242 | 243 | success : true 244 | 245 | ### Versions 246 | 247 | Terraform v1.6.0 248 | on linux_amd64 249 | + provider registry.terraform.io/hashicorp/alicloud v1.239.0 250 | 251 | ## 15 Dec 2024 04:30 UTC 252 | 253 | success : true 254 | 255 | ### Versions 256 | 257 | Terraform v1.6.0 258 | on linux_amd64 259 | + provider registry.terraform.io/hashicorp/alicloud v1.237.0 260 | 261 | ## 08 Dec 2024 05:23 UTC 262 | 263 | success : true 264 | 265 | ### Versions 266 | 267 | Terraform v1.6.0 268 | on linux_amd64 269 | + provider registry.terraform.io/hashicorp/alicloud v1.237.0 270 | 271 | ## 01 Dec 2024 05:53 UTC 272 | 273 | success : true 274 | 275 | ### Versions 276 | 277 | Terraform v1.6.0 278 | on linux_amd64 279 | + provider registry.terraform.io/hashicorp/alicloud v1.236.0 280 | 281 | ## 24 Nov 2024 04:44 UTC 282 | 283 | success : true 284 | 285 | ### Versions 286 | 287 | Terraform v1.6.0 288 | on linux_amd64 289 | + provider registry.terraform.io/hashicorp/alicloud v1.235.0 290 | 291 | ## 17 Nov 2024 06:22 UTC 292 | 293 | success : true 294 | 295 | ### Versions 296 | 297 | Terraform v1.6.0 298 | on linux_amd64 299 | + provider registry.terraform.io/hashicorp/alicloud v1.234.0 300 | 301 | ## 10 Nov 2024 04:10 UTC 302 | 303 | success : true 304 | 305 | ### Versions 306 | 307 | Terraform v1.6.0 308 | on linux_amd64 309 | + provider registry.terraform.io/hashicorp/alicloud v1.233.0 310 | 311 | ## 03 Nov 2024 04:08 UTC 312 | 313 | success : true 314 | 315 | ### Versions 316 | 317 | Terraform v1.6.0 318 | on linux_amd64 319 | + provider registry.terraform.io/hashicorp/alicloud v1.233.0 320 | 321 | ## 27 Oct 2024 04:37 UTC 322 | 323 | success : true 324 | 325 | ### Versions 326 | 327 | Terraform v1.6.0 328 | on linux_amd64 329 | + provider registry.terraform.io/hashicorp/alicloud v1.232.0 330 | 331 | ## 20 Oct 2024 04:10 UTC 332 | 333 | success : true 334 | 335 | ### Versions 336 | 337 | Terraform v1.6.0 338 | on linux_amd64 339 | + provider registry.terraform.io/hashicorp/alicloud v1.231.0 340 | 341 | ## 13 Oct 2024 04:25 UTC 342 | 343 | success : true 344 | 345 | ### Versions 346 | 347 | Terraform v1.6.0 348 | on linux_amd64 349 | + provider registry.terraform.io/hashicorp/alicloud v1.231.0 350 | 351 | ## 06 Oct 2024 04:09 UTC 352 | 353 | success : true 354 | 355 | ### Versions 356 | 357 | Terraform v1.6.0 358 | on linux_amd64 359 | + provider registry.terraform.io/hashicorp/alicloud v1.231.0 360 | 361 | ## 29 Sep 2024 04:00 UTC 362 | 363 | success : true 364 | 365 | ### Versions 366 | 367 | Terraform v1.6.0 368 | on linux_amd64 369 | + provider registry.terraform.io/hashicorp/alicloud v1.231.0 370 | 371 | ## 22 Sep 2024 04:03 UTC 372 | 373 | success : false 374 | 375 | ### Versions 376 | 377 | Terraform v1.6.0 378 | on linux_amd64 379 | + provider registry.terraform.io/hashicorp/alicloud v1.230.1 380 | 381 | ## 15 Sep 2024 05:32 UTC 382 | 383 | success : true 384 | 385 | ### Versions 386 | 387 | Terraform v1.6.0 388 | on linux_amd64 389 | + provider registry.terraform.io/hashicorp/alicloud v1.230.1 390 | 391 | ## 08 Sep 2024 05:14 UTC 392 | 393 | success : true 394 | 395 | ### Versions 396 | 397 | Terraform v1.6.0 398 | on linux_amd64 399 | + provider registry.terraform.io/hashicorp/alicloud v1.230.0 400 | 401 | ## 01 Sep 2024 05:09 UTC 402 | 403 | success : true 404 | 405 | ### Versions 406 | 407 | Terraform v1.6.0 408 | on linux_amd64 409 | + provider registry.terraform.io/hashicorp/alicloud v1.229.1 410 | 411 | ## 19 May 2024 01:34 UTC 412 | 413 | success : false 414 | 415 | ### Versions 416 | 417 | Terraform v1.6.0 418 | on linux_amd64 419 | + provider registry.terraform.io/hashicorp/alicloud v1.223.1 420 | 421 | ## 12 May 2024 01:36 UTC 422 | 423 | success : false 424 | 425 | ### Versions 426 | 427 | Terraform v1.6.0 428 | on linux_amd64 429 | + provider registry.terraform.io/hashicorp/alicloud v1.223.0 430 | 431 | -------------------------------------------------------------------------------- /examples/complete/README.md: -------------------------------------------------------------------------------- 1 | # Complete Security Group example 2 | 3 | Configuration in this directory creates set of Security Group and Security Group Rules resources in various combinations. 4 | 5 | Data sources are used to discover existing VPC resources (VPC and default security group). 6 | 7 | ## Usage 8 | 9 | To run this example you need to execute: 10 | 11 | ```bash 12 | $ terraform init 13 | $ terraform plan 14 | $ terraform apply 15 | ``` 16 | 17 | Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources. 18 | 19 | -------------------------------------------------------------------------------- /examples/complete/main.tf: -------------------------------------------------------------------------------- 1 | variable "profile" { 2 | default = "default" 3 | } 4 | variable "region" { 5 | default = "cn-hangzhou" 6 | } 7 | provider "alicloud" { 8 | region = var.region 9 | profile = var.profile 10 | } 11 | 12 | data "alicloud_resource_manager_resource_groups" "default" { 13 | } 14 | 15 | 16 | module "vpc" { 17 | source = "../../" 18 | region = var.region 19 | profile = var.profile 20 | 21 | vpc_name = "complete-example" 22 | 23 | vpc_cidr = "10.10.0.0/16" 24 | resource_group_id = data.alicloud_resource_manager_resource_groups.default.ids.0 25 | 26 | 27 | availability_zones = ["cn-hangzhou-e", "cn-hangzhou-f", "cn-hangzhou-g"] 28 | vswitch_cidrs = ["10.10.1.0/24", "10.10.2.0/24", "10.10.3.0/24"] 29 | 30 | vpc_tags = { 31 | Owner = "user" 32 | Environment = "staging" 33 | Name = "complete" 34 | } 35 | 36 | vswitch_tags = { 37 | Project = "Secret" 38 | Endpoint = "true" 39 | } 40 | } 41 | 42 | # This vpc and other resources won't be created 43 | module "vpc_zero" { 44 | source = "../../" 45 | region = var.region 46 | profile = var.profile 47 | 48 | create = false 49 | vpc_name = "complete-example" 50 | 51 | vpc_cidr = "10.10.0.0/16" 52 | 53 | availability_zones = ["cn-hangzhou-e", "cn-hangzhou-f", "cn-hangzhou-g"] 54 | vswitch_cidrs = ["10.10.1.0/24", "10.10.2.0/24", "10.10.3.0/24"] 55 | } -------------------------------------------------------------------------------- /examples/complete/outputs.tf: -------------------------------------------------------------------------------- 1 | # VPC 2 | output "vpc_id" { 3 | description = "The ID of the VPC" 4 | value = module.vpc.this_vpc_id 5 | } 6 | 7 | output "vpc_cidr_block" { 8 | description = "The VPC cidr block" 9 | value = module.vpc.this_vpc_cidr_block 10 | } 11 | 12 | output "vpc_tags" { 13 | description = "The tags of the VPC" 14 | value = module.vpc.this_vpc_tags 15 | } 16 | 17 | output "vpc_name" { 18 | description = "The name of the VPC" 19 | value = module.vpc.this_vpc_name 20 | } 21 | 22 | output "this_resource_group_id" { 23 | description = "The Id of resource group which the instance belongs." 24 | value = module.vpc.this_resource_group_id 25 | } 26 | 27 | # Subnets 28 | output "vswitch_ids" { 29 | description = "List of IDs of vswitch" 30 | value = module.vpc.this_vswitch_ids 31 | } 32 | output "vswitch_tags" { 33 | description = "List of IDs of vswitch" 34 | value = module.vpc.this_vswitch_tags 35 | } 36 | 37 | output "vswitch_cidr_block" { 38 | description = "The vswitch cidr block" 39 | value = module.vpc.this_vswitch_cidr_blocks 40 | } 41 | 42 | output "vswitch_name" { 43 | description = "The name of vswitch" 44 | value = module.vpc.this_vswitch_names 45 | } -------------------------------------------------------------------------------- /examples/use-default-vpc/README.md: -------------------------------------------------------------------------------- 1 | # Disabled Security Group example 2 | 3 | Configuration in this directory creates set of Security Group and Security Group Rules resources in various combination. 4 | 5 | Data sources are used to discover existing VPC resources (VPC and default security group). 6 | 7 | This example aims to show setting `create = false` will not create a new security group. 8 | 9 | ## Usage 10 | 11 | To run this example you need to execute: 12 | 13 | ```bash 14 | $ terraform init 15 | $ terraform plan 16 | $ terraform apply 17 | ``` 18 | 19 | Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources. 20 | 21 | 22 | ## Outputs 23 | 24 | | Name | Description | 25 | |------|-------------| 26 | | this\_security\_group\_id | The ID of the security group | 27 | 28 | 29 | -------------------------------------------------------------------------------- /examples/use-default-vpc/main.tf: -------------------------------------------------------------------------------- 1 | variable "profile" { 2 | default = "default" 3 | } 4 | variable "region" { 5 | default = "cn-hangzhou" 6 | } 7 | provider "alicloud" { 8 | region = var.region 9 | profile = var.profile 10 | } 11 | 12 | ################################### 13 | # Data sources to get default VPC # 14 | ################################## 15 | data "alicloud_vpcs" "default" { 16 | is_default = true 17 | } 18 | 19 | locals { 20 | default_vpc_cidr_block = data.alicloud_vpcs.default.vpcs.0.cidr_block 21 | } 22 | module "vpc" { 23 | source = "../../" 24 | region = var.region 25 | profile = var.profile 26 | 27 | vpc_id = data.alicloud_vpcs.default.ids.0 28 | resource_group_id = "rg-acfmwvvtg5o****" 29 | 30 | availability_zones = ["cn-hangzhou-e", "cn-hangzhou-f"] 31 | vswitch_cidrs = [cidrsubnet(local.default_vpc_cidr_block, 8, 10), cidrsubnet(local.default_vpc_cidr_block, 8, 11), cidrsubnet(local.default_vpc_cidr_block, 8, 12)] 32 | 33 | vswitch_tags = { 34 | Project = "Secret" 35 | Endpoint = true 36 | DefaultVpc = true 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /examples/use-default-vpc/outputs.tf: -------------------------------------------------------------------------------- 1 | # VPC 2 | output "vpc_id" { 3 | description = "The ID of the VPC" 4 | value = module.vpc.this_vpc_id 5 | } 6 | 7 | output "vpc_tags" { 8 | description = "The tags of the VPC" 9 | value = module.vpc.this_vpc_tags 10 | } 11 | 12 | # Subnets 13 | output "vswitch_ids" { 14 | description = "List of IDs of vswitch" 15 | value = module.vpc.this_vswitch_ids 16 | } 17 | 18 | output "vswitch_tags" { 19 | description = "List of IDs of vswitch" 20 | value = module.vpc.this_vswitch_tags 21 | } -------------------------------------------------------------------------------- /locals.tf: -------------------------------------------------------------------------------- 1 | locals { 2 | route_table_id = var.vpc_id == "" ? concat(alicloud_vpc.vpc.*.route_table_id, [""])[0] : data.alicloud_route_tables.this.ids.0 3 | 4 | # Get ID of created Security Group 5 | this_vpc_id = var.vpc_id != "" ? var.vpc_id : concat(alicloud_vpc.vpc.*.id, [""])[0] 6 | # Whether to create other resources in which the vpc 7 | create_sub_resources = var.vpc_id != "" || var.create ? true : false 8 | this_vpc_cidr_block = var.vpc_id != "" ? concat(data.alicloud_vpcs.this[0].vpcs.*.cidr_block, [""])[0] : concat(alicloud_vpc.vpc.*.cidr_block, [""])[0] 9 | this_vpc_name = var.vpc_id != "" ? concat(data.alicloud_vpcs.this[0].vpcs.*.vpc_name, [""])[0] : concat(alicloud_vpc.vpc.*.vpc_name, [""])[0] 10 | } 11 | 12 | data "alicloud_route_tables" "this" { 13 | vpc_id = local.this_vpc_id 14 | } 15 | 16 | data "alicloud_vpcs" "this" { 17 | count = var.vpc_id != "" ? 1 : 0 18 | ids = [var.vpc_id] 19 | } 20 | -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | // If there is not specifying vpc_id, the module will launch a new vpc 2 | resource "alicloud_vpc" "vpc" { 3 | count = var.vpc_id != "" ? 0 : var.create ? 1 : 0 4 | vpc_name = var.vpc_name 5 | cidr_block = var.vpc_cidr 6 | resource_group_id = var.resource_group_id 7 | description = var.vpc_description 8 | tags = merge( 9 | { 10 | "Name" = format("%s", var.vpc_name) 11 | }, 12 | var.vpc_tags, 13 | ) 14 | } 15 | 16 | // According to the vswitch cidr blocks to launch several vswitches 17 | resource "alicloud_vswitch" "vswitches" { 18 | count = local.create_sub_resources ? length(var.vswitch_cidrs) : 0 19 | vpc_id = var.vpc_id != "" ? var.vpc_id : concat(alicloud_vpc.vpc.*.id, [""])[0] 20 | cidr_block = var.vswitch_cidrs[count.index] 21 | zone_id = element(var.availability_zones, count.index) 22 | vswitch_name = length(var.vswitch_cidrs) > 1 || var.use_num_suffix ? format("%s%03d", var.vswitch_name, count.index + 1) : var.vswitch_name 23 | description = var.vswitch_description 24 | tags = merge( 25 | { 26 | Name = format( 27 | "%s%03d", 28 | var.vswitch_name, 29 | count.index + 1 30 | ) 31 | }, 32 | var.vswitch_tags, 33 | ) 34 | } 35 | 36 | // According to the destination cidr block to launch a new route entry 37 | resource "alicloud_route_entry" "route_entry" { 38 | count = local.create_sub_resources ? length(var.destination_cidrs) : 0 39 | route_table_id = local.route_table_id 40 | destination_cidrblock = var.destination_cidrs[count.index] 41 | nexthop_type = "Instance" 42 | nexthop_id = var.nexthop_ids[count.index] 43 | } -------------------------------------------------------------------------------- /outputs.tf: -------------------------------------------------------------------------------- 1 | # Output the IDs of the ECS instances created 2 | output "vpc_id" { 3 | description = "Deprecated and use this_vpc_id instead" 4 | value = local.this_vpc_id 5 | } 6 | 7 | output "cidr_block" { 8 | description = "Deprecated and use this_vpc_cidr_block instead" 9 | value = concat(alicloud_vpc.vpc.*.cidr_block, [""])[0] 10 | } 11 | 12 | output "vswitch_ids" { 13 | description = "Deprecated and use this_vswitch_ids instead" 14 | value = alicloud_vswitch.vswitches.*.id 15 | } 16 | 17 | output "availability_zones" { 18 | description = "Deprecated and use this_availability_zones instead" 19 | value = alicloud_vswitch.vswitches.*.availability_zone 20 | } 21 | 22 | output "router_id" { 23 | description = "Deprecated and use this_router_id instead" 24 | value = alicloud_route_entry.route_entry.*.router_id 25 | } 26 | 27 | output "route_table_id" { 28 | description = "Deprecated and use this_route_table_id instead" 29 | value = alicloud_route_entry.route_entry.*.route_table_id 30 | } 31 | 32 | output "this_vpc_id" { 33 | description = "The VPC id" 34 | value = local.this_vpc_id 35 | } 36 | 37 | output "this_vpc_name" { 38 | description = "The VPC name" 39 | value = local.this_vpc_name 40 | } 41 | 42 | output "this_vpc_cidr_block" { 43 | description = "The VPC cidr block" 44 | value = local.this_vpc_cidr_block 45 | } 46 | 47 | output "this_vpc_tags" { 48 | description = "The VPC tags" 49 | value = concat(alicloud_vpc.vpc.*.tags, [{}])[0] 50 | } 51 | 52 | output "this_resource_group_id" { 53 | description = "The Id of resource group which the instance belongs." 54 | value = concat(alicloud_vpc.vpc.*.resource_group_id, [""])[0] 55 | } 56 | 57 | output "this_vswitch_ids" { 58 | description = "List of vswitch ids" 59 | value = alicloud_vswitch.vswitches.*.id 60 | } 61 | 62 | output "this_vswitch_names" { 63 | description = "List of vswitch names" 64 | value = alicloud_vswitch.vswitches.*.name 65 | } 66 | 67 | output "this_vswitch_cidr_blocks" { 68 | description = "The vswitch cidr block" 69 | value = alicloud_vswitch.vswitches.*.cidr_block 70 | } 71 | 72 | output "this_vswitch_tags" { 73 | description = "List of vswitch tags." 74 | value = alicloud_vswitch.vswitches.*.tags 75 | } 76 | 77 | output "this_availability_zones" { 78 | description = "List of availability zones in which vswitches launched." 79 | value = alicloud_vswitch.vswitches.*.availability_zone 80 | } 81 | 82 | output "this_route_table_id" { 83 | description = "The vpc route table id." 84 | value = local.route_table_id 85 | } 86 | 87 | output "this_router_id" { 88 | description = "The vpc router id." 89 | value = concat(alicloud_route_entry.route_entry.*.router_id, [""])[0] 90 | } -------------------------------------------------------------------------------- /scripts/curl_fc_trigger.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "crypto/rand" 5 | "encoding/json" 6 | "fmt" 7 | "io" 8 | "log" 9 | "math/big" 10 | "net/http" 11 | "os" 12 | "strings" 13 | ) 14 | 15 | var urlPrefix = "https://terraform-fc-test-for-example-module.oss-ap-southeast-1.aliyuncs.com" 16 | 17 | func main() { 18 | if len(os.Args)!=4{ 19 | log.Println("[ERROR] invalid args") 20 | return 21 | } 22 | branch := strings.TrimSpace(os.Args[1]) 23 | repoName := strings.TrimSpace(os.Args[2]) 24 | ossObjectPath := strings.TrimSpace(os.Args[3]) 25 | 26 | // get trigger url 27 | fcTriggerUrl := urlPrefix + "/fcUrls.json" 28 | response, err := http.Get(fcTriggerUrl) 29 | if err != nil { 30 | log.Println("[ERROR] get fc trigger url failed") 31 | } 32 | defer response.Body.Close() 33 | 34 | content, _ := io.ReadAll(response.Body) 35 | var data interface{} 36 | json.Unmarshal(content, &data) 37 | triggerMap := data.(map[string]interface{}) 38 | 39 | n, _ := rand.Int(rand.Reader, big.NewInt(100)) 40 | index := int(n.Int64()) % len(triggerMap) 41 | triggerUrl := triggerMap[fmt.Sprintf("%d", index)] 42 | fmt.Println(triggerUrl) 43 | 44 | // curl 45 | client := &http.Client{} 46 | req, err := http.NewRequest("GET", triggerUrl.(string), 47 | nil) 48 | if err != nil { 49 | panic(err) 50 | } 51 | req.Header.Add("X-Fc-Invocation-Type", "Async") 52 | 53 | query := req.URL.Query() 54 | query.Add("branch", branch) 55 | query.Add("repo_name", repoName) 56 | query.Add("oss_object_path", ossObjectPath) 57 | req.URL.RawQuery = query.Encode() 58 | 59 | if _, err := client.Do(req); err != nil { 60 | log.Printf("[ERROR] fail to trigger fc test, err: %s", err) 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /scripts/e2e_check.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "log" 7 | "net/http" 8 | "os" 9 | "strings" 10 | "time" 11 | ) 12 | 13 | var urlPrefix = "https://terraform-fc-test-for-example-module.oss-ap-southeast-1.aliyuncs.com" 14 | 15 | func main() { 16 | ossObjectPath := strings.TrimSpace(os.Args[1]) 17 | log.Println("run log path:", ossObjectPath) 18 | runLogFileName := "terraform.run.log" 19 | runResultFileName := "terraform.run.result.log" 20 | runLogUrl := urlPrefix + "/" + ossObjectPath + "/" + runLogFileName 21 | runResultUrl := urlPrefix + "/" + ossObjectPath + "/" + runResultFileName 22 | lastLineNum := 0 23 | deadline := time.Now().Add(time.Duration(24) * time.Hour) 24 | finish := false 25 | exitCode := 0 26 | log.Println(runLogUrl) 27 | errResultMessage := "" 28 | for !time.Now().After(deadline) { 29 | runLogResponse, err := http.Get(runLogUrl) 30 | if err != nil || runLogResponse.StatusCode != 200 { 31 | log.Println("waiting for job running...") 32 | time.Sleep(5 * time.Second) 33 | continue 34 | } 35 | defer runLogResponse.Body.Close() 36 | 37 | s, er := io.ReadAll(runLogResponse.Body) 38 | if er != nil && fmt.Sprint(er) != "EOF" { 39 | log.Println("[ERROR] reading run log response failed:", err) 40 | } 41 | lineNum := len(s) 42 | if runLogResponse.StatusCode == 200 { 43 | if lineNum > lastLineNum { 44 | fmt.Printf("%s", s[lastLineNum:lineNum]) 45 | lastLineNum = lineNum 46 | } 47 | } 48 | if finish { 49 | log.Println("run log path:", ossObjectPath) 50 | log.Println("run log url:", runLogUrl) 51 | if strings.Contains(ossObjectPath, "weekly") { 52 | updateTestRecord(ossObjectPath) 53 | exitCode = 0 54 | } 55 | if errResultMessage != "" { 56 | log.Println("[ERROR] run result:", errResultMessage) 57 | } 58 | os.Exit(exitCode) 59 | } 60 | runResultResponse, err := http.Get(runResultUrl) 61 | if err != nil || runResultResponse.StatusCode != 200 { 62 | time.Sleep(5 * time.Second) 63 | continue 64 | } 65 | defer runResultResponse.Body.Close() 66 | runResultContent := make([]byte, 100000) 67 | _, err = runResultResponse.Body.Read(runResultContent) 68 | if err != nil && fmt.Sprint(err) != "EOF" { 69 | log.Println("[ERROR] reading run result response failed:", err) 70 | } 71 | finish = true 72 | if !strings.HasPrefix(string(runResultContent), "PASS") { 73 | errResultMessage = string(runResultContent) 74 | exitCode = 1 75 | } 76 | } 77 | log.Println("[ERROR] Timeout: waiting for job finished timeout after 24 hours.") 78 | } 79 | 80 | func updateTestRecord(ossObjectPath string) { 81 | currentTestRecordFileName := "TestRecord.md" 82 | currentTestRecordFileUrl := urlPrefix + "/" + ossObjectPath + "/" + currentTestRecordFileName 83 | response, err := http.Get(currentTestRecordFileUrl) 84 | if err != nil { 85 | log.Println("[ERROR] failed to get test record from oss") 86 | return 87 | } 88 | defer response.Body.Close() 89 | data, _ := io.ReadAll(response.Body) 90 | if response.StatusCode != 200 || len(data) == 0 { 91 | return 92 | } 93 | currentTestRecord := string(data) + "\n" 94 | 95 | testRecordFileName := "TestRecord.md" 96 | var testRecordFile *os.File 97 | oldTestRecord := "" 98 | if _, err := os.Stat(testRecordFileName); os.IsNotExist(err) { 99 | testRecordFile, err = os.Create(testRecordFileName) 100 | if err != nil { 101 | log.Println("[ERROR] failed to create test record file") 102 | } 103 | } else { 104 | data, err := os.ReadFile(testRecordFileName) 105 | if err != nil { 106 | log.Println("[ERROR] failed to read test record file") 107 | return 108 | } 109 | oldTestRecord = string(data) 110 | 111 | testRecordFile, err = os.OpenFile(testRecordFileName, os.O_TRUNC|os.O_RDWR, 0666) 112 | if err != nil { 113 | log.Println("[ERROR] failed to open test record file") 114 | } 115 | } 116 | defer testRecordFile.Close() 117 | 118 | currentTestRecord += oldTestRecord 119 | testRecordFile.WriteString(currentTestRecord) 120 | } 121 | -------------------------------------------------------------------------------- /scripts/terraform-test.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | version="" 4 | updateFolder="examples/complete" 5 | tfvars="tfvars/01-update.tfvars" 6 | f=${1} 7 | success=true 8 | # echo $f 9 | exitCode=0 10 | terraformVersionFile="tfversion.md" 11 | 12 | if [ $# -ge 2 ]; then 13 | echo "" > $terraformVersionFile 14 | fi 15 | 16 | echo "" 17 | echo "====> Terraform testing in" $f 18 | # init 19 | terraform -chdir=$f init -upgrade >/dev/null 20 | if [[ $? -ne 0 ]]; then 21 | success=false 22 | exitCode=1 23 | echo -e "\033[31m[ERROR]\033[0m: running terraform init failed." 24 | else 25 | # plan 26 | echo "" 27 | echo -e "----> Plan Testing\n" 28 | terraform -chdir=$f plan >/dev/null 29 | if [[ $? -ne 0 ]]; then 30 | success=false 31 | exitCode=2 32 | echo -e "\033[31m[ERROR]\033[0m: running terraform plan failed." 33 | else 34 | echo -e "\033[32m - plan check: success\033[0m" 35 | # apply 36 | echo "" 37 | echo -e "----> Apply Testing\n" 38 | terraform -chdir=$f apply -auto-approve >/dev/null 39 | if [[ $? -ne 0 ]]; then 40 | success=false 41 | exitCode=3 42 | echo -e "\033[31m[ERROR]\033[0m: running terraform apply failed." 43 | else 44 | echo -e "\033[32m - apply check: success\033[0m" 45 | # update & check diff 46 | if [ $f == $updateFolder ] && [ -f "${updateFolder}/${tfvars}" ];then 47 | # if example is complete and has tfvars folder 48 | echo "" 49 | echo -e " ----> Apply Update Testing\n" 50 | terraform -chdir=$f apply -auto-approve -var-file=$tfvars >/dev/null 51 | if [[ $? -ne 0 ]]; then 52 | success=false 53 | exitCode=3 54 | echo -e "\033[31m[ERROR]\033[0m: running terraform apply update failed." 55 | else 56 | echo -e "\033[32m - apply update check: success\033[0m" 57 | echo "" 58 | echo -e " ----> Apply Diff Checking\n" 59 | terraform -chdir=$f plan -var-file=$tfvars -detailed-exitcode 60 | if [[ $? -ne 0 ]]; then 61 | success=false 62 | if [[ $exitCode -eq 0 ]]; then 63 | exitCode=4 64 | fi 65 | echo -e "\033[31m[ERROR]\033[0m: running terraform plan for checking diff failed." 66 | else 67 | echo -e "\033[32m - apply diff check: success\033[0m" 68 | fi 69 | fi 70 | else 71 | # if example is no need to update 72 | echo "" 73 | echo -e " ----> Apply Diff Checking\n" 74 | terraform -chdir=$f plan -detailed-exitcode 75 | if [[ $? -ne 0 ]]; then 76 | success=false 77 | exitCode=4 78 | echo -e "\033[31m[ERROR]\033[0m: running terraform plan for checking diff failed." 79 | else 80 | echo -e "\033[32m - apply diff check: success\033[0m" 81 | fi 82 | fi 83 | fi 84 | # destroy 85 | echo "" 86 | echo -e " ----> Destroying\n" 87 | terraform -chdir=$f destroy -auto-approve >/dev/null 88 | if [[ $? -ne 0 ]]; then 89 | success=false 90 | if [[ $exitCode -eq 0 ]]; then 91 | exitCode=5 92 | fi 93 | echo -e "\033[31m[ERROR]\033[0m: running terraform destroy failed." 94 | else 95 | echo -e "\033[32m - destroy: success\033[0m" 96 | fi 97 | fi 98 | fi 99 | 100 | version=$(terraform -chdir=$f version) 101 | row=`echo -e "$version" | sed -n '/^$/='` 102 | if [ -n "$row" ]; then 103 | version=`echo -e "$version" | sed -n "1,${row}p"` 104 | fi 105 | 106 | if [[ $exitCode -ne 1 ]]; then 107 | rm -rf $f/.terraform 108 | rm -rf $f/.terraform.lock.hcl 109 | fi 110 | 111 | if [ $# -ge 2 ]; then 112 | echo -e "### Versions\n" >> $terraformVersionFile 113 | echo -e "${version}" >> $terraformVersionFile 114 | fi 115 | 116 | exit $exitCode -------------------------------------------------------------------------------- /variables.tf: -------------------------------------------------------------------------------- 1 | variable "region" { 2 | description = "(Deprecated from version 1.9.0) The region used to launch this module resources." 3 | type = string 4 | default = "" 5 | } 6 | 7 | variable "profile" { 8 | description = "(Deprecated from version 1.9.0) The profile name as set in the shared credentials file. If not set, it will be sourced from the ALICLOUD_PROFILE environment variable." 9 | type = string 10 | default = "" 11 | } 12 | 13 | variable "shared_credentials_file" { 14 | description = "(Deprecated from version 1.9.0) This is the path to the shared credentials file. If this is not set and a profile is specified, $HOME/.aliyun/config.json will be used." 15 | type = string 16 | default = "" 17 | } 18 | 19 | variable "skip_region_validation" { 20 | description = "(Deprecated from version 1.9.0) Skip static validation of region ID. Used by users of alternative AlibabaCloud-like APIs or users w/ access to regions that are not public (yet)." 21 | type = bool 22 | default = false 23 | } 24 | 25 | # VPC variables 26 | variable "create" { 27 | description = "Whether to create vpc. If false, you can specify an existing vpc by setting 'vpc_id'." 28 | type = bool 29 | default = true 30 | } 31 | 32 | variable "vpc_id" { 33 | description = "The vpc id used to launch several vswitches. If set, the 'create' will be ignored." 34 | type = string 35 | default = "" 36 | } 37 | 38 | variable "vpc_name" { 39 | description = "The vpc name used to launch a new vpc." 40 | type = string 41 | default = "TF-VPC" 42 | } 43 | 44 | variable "vpc_description" { 45 | description = "The vpc description used to launch a new vpc." 46 | type = string 47 | default = "A new VPC created by Terrafrom module terraform-alicloud-vpc" 48 | } 49 | 50 | variable "vpc_cidr" { 51 | description = "The cidr block used to launch a new vpc." 52 | type = string 53 | default = "172.16.0.0/12" 54 | } 55 | 56 | variable "resource_group_id" { 57 | description = "The Id of resource group which the instance belongs." 58 | type = string 59 | default = "" 60 | } 61 | 62 | variable "vpc_name_regex" { 63 | description = "(Deprecated) It has been deprecated from 1.5.0." 64 | type = string 65 | default = "" 66 | } 67 | 68 | variable "vpc_tags" { 69 | description = "The tags used to launch a new vpc. Before 1.5.0, it used to retrieve existing VPC." 70 | type = map(string) 71 | default = {} 72 | } 73 | 74 | # VSwitch variables 75 | variable "vswitch_cidrs" { 76 | description = "List of cidr blocks used to launch several new vswitches. If not set, there is no new vswitches will be created." 77 | type = list(string) 78 | default = [] 79 | } 80 | 81 | variable "availability_zones" { 82 | description = "List available zones to launch several VSwitches." 83 | type = list(string) 84 | default = [] 85 | } 86 | 87 | variable "vswitch_name" { 88 | description = "The vswitch name prefix used to launch several new vswitches." 89 | default = "TF-VSwitch" 90 | } 91 | 92 | variable "use_num_suffix" { 93 | description = "Always append numerical suffix(like 001, 002 and so on) to vswitch name, even if the length of `vswitch_cidrs` is 1" 94 | type = bool 95 | default = false 96 | } 97 | 98 | variable "vswitch_description" { 99 | description = "The vswitch description used to launch several new vswitch." 100 | type = string 101 | default = "New VSwitch created by Terrafrom module terraform-alicloud-vpc." 102 | } 103 | 104 | variable "vswitch_tags" { 105 | description = "The tags used to launch serveral vswitches." 106 | type = map(string) 107 | default = {} 108 | } 109 | 110 | // According to the vswitch cidr blocks to launch several vswitches 111 | variable "destination_cidrs" { 112 | description = "List of destination CIDR block of virtual router in the specified VPC." 113 | type = list(string) 114 | default = [] 115 | } 116 | 117 | variable "nexthop_ids" { 118 | description = "List of next hop instance IDs of virtual router in the specified VPC." 119 | type = list(string) 120 | default = [] 121 | } -------------------------------------------------------------------------------- /versions.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_version = ">= 0.13" 3 | } --------------------------------------------------------------------------------