From 1ac5fdec16d77cc26e64132b552a1b6a6d66a7e0 Mon Sep 17 00:00:00 2001 From: CodeArtisanX Date: Thu, 27 Nov 2025 09:39:12 +0800 Subject: [PATCH] add tech-solution datav-for-atlas --- .../tech-solution/datav-for-atlas/README.md | 44 +++++++++ .../tech-solution/datav-for-atlas/main.tf | 93 +++++++++++++++++++ .../tech-solution/datav-for-atlas/output.tf | 31 +++++++ .../tech-solution/datav-for-atlas/variable.tf | 38 ++++++++ 4 files changed, 206 insertions(+) create mode 100644 solution/tech-solution/datav-for-atlas/README.md create mode 100644 solution/tech-solution/datav-for-atlas/main.tf create mode 100644 solution/tech-solution/datav-for-atlas/output.tf create mode 100644 solution/tech-solution/datav-for-atlas/variable.tf diff --git a/solution/tech-solution/datav-for-atlas/README.md b/solution/tech-solution/datav-for-atlas/README.md new file mode 100644 index 000000000..cd9bb9354 --- /dev/null +++ b/solution/tech-solution/datav-for-atlas/README.md @@ -0,0 +1,44 @@ +## Introduction + +本示例用于实现解决方案[一站式时空决策,释放空间数据价值](https://www.aliyun.com/solution/tech-solution/datav-for-atlas),涉及专有网络(VPC)、交换机(VSwitch)、RDS数据库(RDS)等资源的部署。 + + + +This example is used to implement solution [Unified spatio-temporal decision-making to unlock the value of spatial data](https://www.aliyun.com/solution/tech-solution/datav-for-atlas), which involves the creation and deployment of resources such as Virtual Private Cloud (VPC), Virtual Switch (VSwitch),RDS Database (RDS). + + + +## Providers + +| Name | Version | +|------|---------| +| [alicloud](#provider\_alicloud) | n/a | +| [random](#provider\_random) | n/a | + +## Modules + +No modules. + +## Resources + +| Name | Type | +|------|------| +| [alicloud_db_account_privilege.privilege](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/db_account_privilege) | resource | +| [alicloud_db_connection.public](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/db_connection) | resource | +| [alicloud_db_database.db_database](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/db_database) | resource | +| [alicloud_db_instance.db_instance](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/db_instance) | resource | +| [alicloud_rds_account.account](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/rds_account) | resource | +| [alicloud_vpc.vpc](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vpc) | resource | +| [alicloud_vswitch.vswitch](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vswitch) | resource | +| [random_id.suffix](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/id) | resource | +| [alicloud_db_zones.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/db_zones) | data source | + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| [db\_instance\_class](#input\_db\_instance\_class) | 实例规格 | `string` | `"pg.n4.2c.1m"` | no | +| [db\_name](#input\_db\_name) | RDS数据库名称 | `string` | `"food_test"` | no | +| [db\_password](#input\_db\_password) | RDS数据库密码 | `string` | n/a | yes | +| [rds\_db\_user](#input\_rds\_db\_user) | RDS数据库账号 | `string` | `"test_user"` | no | + \ No newline at end of file diff --git a/solution/tech-solution/datav-for-atlas/main.tf b/solution/tech-solution/datav-for-atlas/main.tf new file mode 100644 index 000000000..9f2757986 --- /dev/null +++ b/solution/tech-solution/datav-for-atlas/main.tf @@ -0,0 +1,93 @@ +# ------------------------------------------------------------------------------ +# 核心资源定义 +# +# 本文件包含了模块的核心基础设施资源 +# 这里的代码负责根据输入变量来创建和配置所有云资源 +# ------------------------------------------------------------------------------ + +# 配置阿里云提供商 +provider "alicloud" { + region = "cn-hangzhou" +} + +# 生成随机ID用于资源命名 +resource "random_id" "suffix" { + keepers = { + user = var.rds_db_user + } + byte_length = 4 +} + +# 定义本地变量 +locals { + instance_name = "datav-${var.rds_db_user}-${random_id.suffix.hex}" + connection_prefix = "datav-pg-${random_id.suffix.hex}" +} + +# 查询可用区 +data "alicloud_db_zones" "default" { + engine = "PostgreSQL" + engine_version = "17.0" + instance_charge_type = "PostPaid" + category = "Basic" + db_instance_storage_type = "cloud_essd" +} + +# 创建VPC +resource "alicloud_vpc" "vpc" { + vpc_name = "VPC_HZ_${random_id.suffix.hex}" + cidr_block = "192.168.0.0/16" +} + +# 创建VSwitch +resource "alicloud_vswitch" "vswitch" { + vpc_id = alicloud_vpc.vpc.id + cidr_block = "192.168.1.0/24" + vswitch_name = "vsw_001" + zone_id = data.alicloud_db_zones.default.zones[0].id +} + +# 创建PostgreSQL实例 +resource "alicloud_db_instance" "db_instance" { + engine = "PostgreSQL" + engine_version = "17.0" + instance_storage = "50" + instance_type = var.db_instance_class + instance_charge_type = "Postpaid" + instance_name = local.instance_name + zone_id = data.alicloud_db_zones.default.zones[0].id + vswitch_id = alicloud_vswitch.vswitch.id + db_instance_storage_type = "cloud_essd" + category = "Basic" + security_ips = ["47.99.0.0/16", "192.168.0.0/16"] +} + +# 创建公网连接 +resource "alicloud_db_connection" "public" { + instance_id = alicloud_db_instance.db_instance.id + connection_prefix = local.connection_prefix + port = "5432" +} + +# 创建RDS账号 +resource "alicloud_rds_account" "account" { + db_instance_id = alicloud_db_instance.db_instance.id + account_type = "Super" + account_password = var.db_password + account_name = var.rds_db_user +} + +# 创建RDS数据库 +resource "alicloud_db_database" "db_database" { + instance_id = alicloud_db_instance.db_instance.id + character_set = "utf8" + name = var.db_name +} + +# 设置RDS账号权限 +resource "alicloud_db_account_privilege" "privilege" { + instance_id = alicloud_db_instance.db_instance.id + account_name = alicloud_rds_account.account.account_name + privilege = "DBOwner" + db_names = alicloud_db_database.db_database.*.name +} \ No newline at end of file diff --git a/solution/tech-solution/datav-for-atlas/output.tf b/solution/tech-solution/datav-for-atlas/output.tf new file mode 100644 index 000000000..56beec1e2 --- /dev/null +++ b/solution/tech-solution/datav-for-atlas/output.tf @@ -0,0 +1,31 @@ +# ------------------------------------------------------------------------------ +# 模块输出值 +# +# 本文件定义了模块执行成功后返回给调用方的值 +# 这些输出可以被其他 Terraform 配置引用,或在 apply 命令结束后显示给用户 +# ------------------------------------------------------------------------------ + +# 输出PostgreSQL实例的公网连接地址 +output "pg_public_connection_string" { + value = alicloud_db_connection.public.connection_string + description = "PostgreSQL 外网连接地址" +} + +# 输出数据库账号。 +output "account_name" { + value = var.rds_db_user + description = "数据库账号" +} + +# 输出数据库密码。 +output "account_password" { + value = var.db_password + sensitive = true + description = "数据库密码" +} + +# 输出数据库名称。 +output "dbname" { + value = var.db_name + description = "数据库名称" +} \ No newline at end of file diff --git a/solution/tech-solution/datav-for-atlas/variable.tf b/solution/tech-solution/datav-for-atlas/variable.tf new file mode 100644 index 000000000..4bde12878 --- /dev/null +++ b/solution/tech-solution/datav-for-atlas/variable.tf @@ -0,0 +1,38 @@ +# ------------------------------------------------------------------------------ +# 模块输入变量 +# +# 本文件定义了该 Terraform 模块所有可配置的输入变量 +# 每个变量都包含了详细的说明,以帮助用户正确配置模块 +# ------------------------------------------------------------------------------ + +# RDS实例规格 +variable "db_instance_class" { + type = string + default = "pg.n4.2c.1m" + nullable = false + description = "实例规格" +} + +# RDS数据库账号 +variable "rds_db_user" { + type = string + default = "test_user" + nullable = false + description = "RDS数据库账号" +} + +# RDS数据库名称 +variable "db_name" { + type = string + default = "food_test" + nullable = false + description = "RDS数据库名称" +} + +# RDS数据库密码 +variable "db_password" { + type = string + sensitive = true + description = "RDS数据库密码" + #default = "" +} \ No newline at end of file