Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
## Introduction
<!-- DOCS_DESCRIPTION_CN -->
本示例用于实现解决方案[SelectDB实现日志高效存储与实时分析](https://www.aliyun.com/solution/tech-solution/real-time-log-analysis-with-selectdb),涉及专有网络(VPC)、交换机(VSwitch)、云服务器(ECS)、SelectDB数据库(SelectDB)等资源的部署。
<!-- DOCS_DESCRIPTION_CN -->

<!-- DOCS_DESCRIPTION_EN -->
This example is used to implement solution [SelectDB enables efficient log storage and real-time analytics](https://www.aliyun.com/solution/tech-solution/real-time-log-analysis-with-selectdb), which involves the creation and deployment of resources such as Virtual Private Cloud (VPC), Virtual Switch (VSwitch), Elastic Compute Service (ECS), SelectDB Database (SelectDB).
<!-- DOCS_DESCRIPTION_EN -->

<!-- BEGIN_TF_DOCS -->
## Providers

| Name | Version |
|------|---------|
| <a name="provider_alicloud"></a> [alicloud](#provider\_alicloud) | n/a |
| <a name="provider_random"></a> [random](#provider\_random) | n/a |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [alicloud_ecs_command.run_tpcc_alicloud_ecs_command](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/ecs_command) | resource |
| [alicloud_ecs_invocation.run_tpcc_alicloud_ecs_invocation](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/ecs_invocation) | resource |
| [alicloud_instance.ecs_instance](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/instance) | resource |
| [alicloud_security_group.sg_select](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/security_group) | resource |
| [alicloud_selectdb_db_instance.selectdb_instance](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/selectdb_db_instance) | resource |
| [alicloud_vpc.vpc_select](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vpc) | resource |
| [alicloud_vswitch.vsw_select](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_images.image_id](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/images) | data source |
| [alicloud_zones.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/zones) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_db_password"></a> [db\_password](#input\_db\_password) | SelectDB admin账号密码 | `string` | n/a | yes |
| <a name="input_ecs_instance_password"></a> [ecs\_instance\_password](#input\_ecs\_instance\_password) | ECS服务器root账号密码 | `string` | n/a | yes |
| <a name="input_instance_class"></a> [instance\_class](#input\_instance\_class) | SelectDB实例规格 | `string` | `"selectdb.4xlarge"` | no |
| <a name="input_instance_type"></a> [instance\_type](#input\_instance\_type) | ECS实例类型,建议选择配备 16 vCPU 64 GiB 配置的实例 | `string` | `"ecs.g8i.4xlarge"` | no |
| <a name="input_selectdb_engine_version"></a> [selectdb\_engine\_version](#input\_selectdb\_engine\_version) | SelectDB内核版本 | `string` | `"4.0.4"` | no |
<!-- END_TF_DOCS -->
115 changes: 115 additions & 0 deletions solution/tech-solution/real-time-log-analysis-with-selectdb/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# ------------------------------------------------------------------------------
# 核心资源定义
#
# 本文件包含了模块的核心基础设施资源
# 这里的代码负责根据输入变量来创建和配置所有云资源
# ------------------------------------------------------------------------------

# 配置阿里云提供商
provider "alicloud" {
region = "cn-hangzhou"
}

# 创建随机ID用于资源命名
resource "random_id" "suffix" {
byte_length = 8
}

# 定义本地变量common_name
locals {
common_name = "SelectDB-${random_id.suffix.id}"
ecs_command = <<SHELL
#!/bin/bash
cd /root
export ROS_DEPLOY=true
wget https://help-static-aliyun-doc.aliyuncs.com/install-script/selectdb-observability/yc_log_demo_2.0.1.tar.gz
tar -zxvf yc_log_demo_2.0.1.tar.gz
cd /root/log_demo
bash install.sh
sudo chown -R root:root /root/log_demo
SHELL
}

# 查询可用区
data "alicloud_zones" "default" {
available_disk_category = "cloud_essd"
available_instance_type = var.instance_type
}

# 创建VPC
resource "alicloud_vpc" "vpc_select" {
cidr_block = "192.168.0.0/16"
vpc_name = "${local.common_name}-vpc"
}

# 创建VSwitch
resource "alicloud_vswitch" "vsw_select" {
vpc_id = alicloud_vpc.vpc_select.id
zone_id = data.alicloud_zones.default.ids[0]
cidr_block = "192.168.0.0/24"
vswitch_name = "${local.common_name}-vsw"
}

# 创建安全组
resource "alicloud_security_group" "sg_select" {
security_group_name = "${local.common_name}-sg"
vpc_id = alicloud_vpc.vpc_select.id
}

# 获取阿里云官方镜像
data "alicloud_images" "image_id" {
name_regex = "^aliyun_3_9_x64_20G*"
owners = "system" # 官方镜像
most_recent = true # 获取最新版本
}

# 创建ECS实例
resource "alicloud_instance" "ecs_instance" {
vpc_id = alicloud_vpc.vpc_select.id
vswitch_id = alicloud_vswitch.vsw_select.id
security_groups = [alicloud_security_group.sg_select.id]
image_id = data.alicloud_images.image_id.images[0].id
instance_name = "${local.common_name}-ecs"
instance_type = var.instance_type
system_disk_category = "cloud_essd"
system_disk_size = 100
internet_max_bandwidth_out = 10
password = var.ecs_instance_password
}

# 创建SelectDB实例
resource "alicloud_selectdb_db_instance" "selectdb_instance" {
vpc_id = alicloud_vpc.vpc_select.id
zone_id = data.alicloud_zones.default.ids[0]
vswitch_id = alicloud_vswitch.vsw_select.id
db_instance_description = "${local.common_name}-selectdb"
db_instance_class = var.instance_class
cache_size = 100
payment_type = "PayAsYouGo"
engine_minor_version = var.selectdb_engine_version
admin_pass = var.db_password
desired_security_ip_lists {
group_name = "default"
security_ip_list = "192.168.0.0/16"
}
}

# 创建ECS命令资源
resource "alicloud_ecs_command" "run_tpcc_alicloud_ecs_command" {
name = "commond_install"
description = "commond_install_description"
type = "RunShellScript"
command_content = base64encode(local.ecs_command)
timeout = 3600
working_dir = "/root"
}

# 创建ECS命令调用资源
resource "alicloud_ecs_invocation" "run_tpcc_alicloud_ecs_invocation" {
instance_id = [alicloud_instance.ecs_instance.id]
command_id = alicloud_ecs_command.run_tpcc_alicloud_ecs_command.id
depends_on = [alicloud_selectdb_db_instance.selectdb_instance]
timeouts {
create = "60m"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# ------------------------------------------------------------------------------
# 模块输出值
#
# 本文件定义了模块执行成功后返回给调用方的值
# 这些输出可以被其他 Terraform 配置引用,或在 apply 命令结束后显示给用户
# ------------------------------------------------------------------------------

# 输出ECS登录用户
output "ecs_account" {
description = "ECS登录用户"
value = "root"
}

# 输出ECS用户root密码
output "ecs_login_password" {
description = "ECS用户root密码"
value = var.ecs_instance_password
sensitive = true
}

# 输出SelectDB登录用户
output "selectdb_account" {
description = "SelectDB登录用户"
value = "admin"
}

# 输出SelectDB用户admin密码
output "selectdb_login_password" {
description = "SelectDB用户admin密码"
value = var.db_password
sensitive = true
}

# 输出SelectDB VPC地址
output "selectdb_vpc_connection_string" {
description = "SelectDB VPC地址"
value = alicloud_selectdb_db_instance.selectdb_instance.instance_net_infos[1].connection_string
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# ------------------------------------------------------------------------------
# 模块输入变量
#
# 本文件定义了该 Terraform 模块所有可配置的输入变量
# 每个变量都包含了详细的说明,以帮助用户正确配置模块
# ------------------------------------------------------------------------------

# 指定ECS实例的规格型号
variable "instance_type" {
type = string
default = "ecs.g8i.4xlarge"
description = "ECS实例类型,建议选择配备 16 vCPU 64 GiB 配置的实例"
}

# ECS服务器root账号密码
variable "ecs_instance_password" {
type = string
sensitive = true
description = "ECS服务器root账号密码"
#default = ""
validation {
condition = can(regex("^[0-9A-Za-z_!@#$%^&*()_+\\-=\\+]+$", var.ecs_instance_password)) && length(var.ecs_instance_password) >= 8 && length(var.ecs_instance_password) <= 30
error_message = "长度8-30,必须包含三项(大写字母、小写字母、数字、 !@#$%^&*()_+-=中的特殊符号)"
}
}

# SelectDB实例规格
variable "instance_class" {
type = string
default = "selectdb.4xlarge"
description = "SelectDB实例规格"
}

# SelectDB内核版本
variable "selectdb_engine_version" {
type = string
default = "4.0.4"
description = "SelectDB内核版本"
validation {
condition = contains(["3.0.12", "4.0.4"], var.selectdb_engine_version)
error_message = "无效的配置信息,请检查并重新输入"
}
}

# SelectDB admin账号密码
variable "db_password" {
type = string
sensitive = true
description = "SelectDB admin账号密码"
#default = ""
validation {
condition = can(regex("^[0-9A-Za-z_!@#$%^&*()_+\\-=\\+]+$", var.db_password)) && length(var.db_password) >= 8 && length(var.db_password) <= 30
error_message = "长度8-30,必须包含三项(大写字母、小写字母、数字、 !@#$%^&*()_+-=中的特殊符号)"
}
}
Loading