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