1+ provider "alicloud" {
2+ region = var. region
3+ }
4+
5+ # 生成随机字符串
6+ resource "random_string" "random_string" {
7+ length = 10
8+ special = false
9+ upper = false
10+ numeric = true
11+ lower = true
12+ }
13+
14+ # VPC
15+ resource "alicloud_vpc" "vpc" {
16+ vpc_name = " vpc_SDWebUI"
17+ cidr_block = " 192.168.0.0/16"
18+ }
19+
20+ # VSwitch
21+ resource "alicloud_vswitch" "vswitch" {
22+ vpc_id = alicloud_vpc. vpc . id
23+ availability_zone = var. zone_id
24+ cidr_block = " 192.168.0.0/18"
25+ vswitch_name = " vswitch_SDWebUI"
26+ }
27+
28+ # NAT网关
29+ resource "alicloud_nat_gateway" "nat_gateway" {
30+ vpc_id = alicloud_vpc. vpc . id
31+ vswitch_id = alicloud_vswitch. vswitch . id
32+ nat_gateway_name = " nat_SDWebUI"
33+ instance_charge_type = " PostPaid"
34+ internet_charge_type = " PayByLcu"
35+ nat_type = " Enhanced"
36+ network_type = " internet"
37+
38+ tags = {
39+ WebUI = " SD_WebUI"
40+ }
41+ }
42+
43+ # EIP
44+ resource "alicloud_eip" "eip" {
45+ name = " eip_SDWebUI"
46+ bandwidth = 200
47+ internet_charge_type = " PayByTraffic"
48+ }
49+
50+ # EIP关联到NAT网关
51+ resource "alicloud_eip_association" "eip_association" {
52+ allocation_id = alicloud_eip. eip . id
53+ instance_id = alicloud_nat_gateway. nat_gateway . id
54+ }
55+
56+ # SNAT条目
57+ resource "alicloud_snat_entry" "snat_entry" {
58+ snat_table_id = alicloud_nat_gateway. nat_gateway . snat_table_ids
59+ snat_ip = alicloud_eip. eip . ip_address
60+ source_cidr = " 192.168.0.0/18"
61+
62+ depends_on = [alicloud_eip_association . eip_association ]
63+ }
64+
65+ # 安全组
66+ resource "alicloud_security_group" "security_group" {
67+ vpc_id = alicloud_vpc. vpc . id
68+ security_group_name = " sg_SDWebUI"
69+ security_group_type = " normal"
70+ }
71+
72+ # 安全组入站规则 - 80端口
73+ resource "alicloud_security_group_rule" "allow_http" {
74+ type = " ingress"
75+ ip_protocol = " tcp"
76+ nic_type = " intranet"
77+ policy = " accept"
78+ port_range = " 80/80"
79+ priority = 1
80+ security_group_id = alicloud_security_group. security_group . id
81+ cidr_ip = " 0.0.0.0/0"
82+ }
83+
84+ # 安全组入站规则 - 443端口
85+ resource "alicloud_security_group_rule" "allow_https" {
86+ type = " ingress"
87+ ip_protocol = " tcp"
88+ nic_type = " intranet"
89+ policy = " accept"
90+ port_range = " 443/443"
91+ priority = 1
92+ security_group_id = alicloud_security_group. security_group . id
93+ cidr_ip = " 0.0.0.0/0"
94+ }
95+
96+ # NAS文件系统
97+ resource "alicloud_nas_file_system" "nas" {
98+ file_system_type = " standard"
99+ storage_type = " Performance"
100+ protocol_type = " NFS"
101+ encrypt_type = 0
102+ }
103+
104+ # NAS访问组
105+ resource "alicloud_nas_access_group" "nas_access_group" {
106+ access_group_type = " Vpc"
107+ access_group_name = " nas_accessgroup_SDWebUI"
108+ file_system_type = " standard"
109+ }
110+
111+ # NAS访问规则
112+ resource "alicloud_nas_access_rule" "nas_access_rule" {
113+ priority = 100
114+ user_access_type = " no_squash"
115+ access_group_name = alicloud_nas_access_group. nas_access_group . access_group_name
116+ source_cidr_ip = " 0.0.0.0/0"
117+ rw_access_type = " RDWR"
118+ file_system_type = " standard"
119+ }
120+
121+ # NAS挂载点
122+ resource "alicloud_nas_mount_target" "nas_mount_target" {
123+ vpc_id = alicloud_vpc. vpc . id
124+ vswitch_id = alicloud_vswitch. vswitch . id
125+ security_group_id = alicloud_security_group. security_group . id
126+ status = " Active"
127+ file_system_id = alicloud_nas_file_system. nas . id
128+ network_type = " Vpc"
129+ access_group_name = alicloud_nas_access_group. nas_access_group . access_group_name
130+
131+ depends_on = [alicloud_nas_access_rule . nas_access_rule ]
132+ }
133+
134+ # PAI-EAS服务
135+ resource "alicloud_pai_service" "pai_eas" {
136+ service_config = jsonencode ({
137+ metadata = {
138+ name = " sdwebui_${ random_string . random_string . result } "
139+ instance = 1
140+ type = " SDCluster"
141+ enable_webservice = " true"
142+ }
143+ cloud = {
144+ computing = {
145+ instance_type = var.instance_type
146+ instances = null
147+ }
148+ networking = {
149+ vpc_id = alicloud_vpc.vpc.id
150+ vswitch_id = alicloud_vswitch.vswitch.id
151+ security_group_id = alicloud_security_group.security_group.id
152+ }
153+ }
154+ storage = [
155+ {
156+ nfs = {
157+ path = " /"
158+ server = alicloud_nas_mount_target.nas_mount_target.mount_target_domain
159+ }
160+ properties = {
161+ resource_type = " model"
162+ }
163+ mount_path = " /code/stable-diffusion-webui/data-nas"
164+ }
165+ ]
166+ containers = [
167+ {
168+ image = " eas-registry-vpc.ap-southeast-1.cr.aliyuncs.com/pai-eas/stable-diffusion-webui:4.1"
169+ script = " ./webui.sh --listen --port 8000 --skip-version-check --no-hashing --no-download-sd-model --skip-install --api --filebrowser --cluster-status --sd-dynamic-cache --data-dir /code/stable-diffusion-webui/data-nas"
170+ port = 8000
171+ }
172+ ]
173+ meta = {
174+ type = " SDCluster"
175+ }
176+ options = {
177+ enableCache = true
178+ }
179+ })
180+ timeouts {
181+ create = " 20m"
182+ }
183+ }
0 commit comments