ROSTemplateFormatVersion: '2015-09-01'
Description:
en: This template deploys custom image for single instance, supports creating new
VPC and specifying VPC.
zh-cn: 單一實例自訂鏡像部署,帶資料盤,公網IP可選(支援建立新VPC和指定VPC)。
Conditions:
CreateVpcConditions:
Fn::Equals:
- true
- Ref: WhetherCreateVpc
IfAllocatePublicIP:
Fn::Equals:
- Ref: AllocatePublicIP
- true
Parameters:
PayType:
Type: String
Label:
en: ECS Instance Charge Type
zh-cn: 付費類型
AssociationProperty: ChargeType
AssociationPropertyMetadata:
LocaleKey: InstanceChargeType
Default: PostPaid
AllowedValues:
- PostPaid
- PrePaid
PayPeriodUnit:
Type: String
Label:
en: Pay Period Unit
zh-cn: 購買資源時間長度周期
AssociationProperty: PayPeriodUnit
AssociationPropertyMetadata:
Visible:
Condition:
Fn::Not:
Fn::Equals:
- ${PayType}
- PostPaid
Default: Month
AllowedValues:
- Month
- Year
PayPeriod:
Type: Number
Label:
en: Period
zh-cn: 購買資源時間長度
AssociationProperty: PayPeriod
AssociationPropertyMetadata:
Visible:
Condition:
Fn::Not:
Fn::Equals:
- ${PayType}
- PostPaid
Default: 1
AllowedValues:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
EcsInstanceType:
Type: String
Label:
en: Instance Type
zh-cn: 執行個體類型
AssociationProperty: ALIYUN::ECS::Instance::InstanceType
AssociationPropertyMetadata:
ZoneId: ${ZoneId}
InstanceChargeType: ${InstanceChargeType}
ZoneId:
Type: String
Label:
en: Availability Zone
zh-cn: 可用性區域
AssociationProperty: ALIYUN::ECS::Instance::ZoneId
WhetherCreateVpc:
Type: Boolean
Label:
en: WhetherCreateVpc
zh-cn: 是否建立VPC
Default: false
VpcCidrBlock:
Type: String
Label:
en: VPC CIDR IPv4 Block
zh-cn: 專用網路IPv4網段
Description:
zh-cn: VPC的ip位址區段範圍,<br>您可以使用以下的ip位址區段或其子網:<br><font color='green'>[10.0.0.0/8]</font><br><font
color='green'>[172.16.0.0/12]</font><br><font color='green'>[192.168.0.0/16]</font>
en: 'The ip address range of the VPC in the CidrBlock form; <br>You can use
the following ip address ranges and their subnets: <br><font color=''green''>[10.0.0.0/8]</font><br><font
color=''green''>[172.16.0.0/12]</font><br><font color=''green''>[192.168.0.0/16]</font>'
AssociationProperty: ALIYUN::VPC::VPC::CidrBlock
AssociationPropertyMetadata:
Visible:
Condition:
Fn::Equals:
- ${WhetherCreateVpc}
- true
Default: 192.168.0.0/16
VSwitchCidrBlock:
Type: String
Label:
en: VSwitch CIDR Block
zh-cn: 交換器子網網段
Description:
zh-cn: 必須屬於VPC的子網段。
en: Must belong to the subnet segment of VPC.
AssociationProperty: ALIYUN::VPC::VSwitch::CidrBlock
AssociationPropertyMetadata:
VpcCidrBlock: VpcCidrBlock
Visible:
Condition:
Fn::Equals:
- ${WhetherCreateVpc}
- true
Default: 192.168.1.0/24
VpcId:
Type: String
Label:
en: VPC ID
zh-cn: Virtual Private Cloud執行個體ID
AssociationProperty: ALIYUN::ECS::VPC::VPCId
AssociationPropertyMetadata:
Visible:
Condition:
Fn::Equals:
- ${WhetherCreateVpc}
- false
Default: ''
VSwitchId:
Type: String
Label:
en: VSwitch ID
zh-cn: 交換器執行個體ID
AssociationProperty: ALIYUN::ECS::VSwitch::VSwitchId
AssociationPropertyMetadata:
VpcId: ${VpcId}
ZoneId: ${ZoneId}
Visible:
Condition:
Fn::Equals:
- ${WhetherCreateVpc}
- false
Default: ''
InstancePassword:
Type: String
Label:
en: Instance Password
zh-cn: 執行個體密碼
Description:
en: Server login password, Length 8-30, must contain three(Capital letters,
lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol
in)
zh-cn: 伺服器登入密碼,長度8-30,必須包含三項(大寫字母、小寫字母、數字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符號)
ConstraintDescription:
en: Length 8-30, must contain three(Capital letters, lowercase letters, numbers,
()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in)
zh-cn: 長度8-30,必須包含三項(大寫字母、小寫字母、數字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符號)
AssociationProperty: ALIYUN::ECS::Instance::Password
AllowedPattern: '[0-9A-Za-z\_\-\&:;''<>,=%`~!@#\(\)\$\^\*\+\|\{\}\[\]\.\?\/]+$'
MinLength: 8
MaxLength: 30
NoEcho: true
SystemDiskCategory:
Type: String
Label:
en: System Disk Category
zh-cn: 系統硬碟類型
AssociationProperty: ALIYUN::ECS::Disk::SystemDiskCategory
AssociationPropertyMetadata:
LocaleKey: DiskCategory
InstanceType: ${EcsInstanceType}
AllowedValues:
- cloud_efficiency
- cloud_ssd
- cloud_essd
SystemDiskSize:
Type: Number
Label:
zh-cn: 系統硬碟空間 (GB)
en: System Disk Space (GB)
Default: 200
DataDiskCategory:
Type: String
Label:
zh-cn: 資料盤類型
en: Data disk type
AssociationProperty: ALIYUN::ECS::Disk::DataDiskCategory
AssociationPropertyMetadata:
InstanceType: EcsInstanceType
ZoneId: ZoneId
LocaleKey: DiskCategory
DataDiskSize:
Type: Number
Label:
zh-cn: 資料盤空間
en: Data disk space
Description:
zh-cn: ECS執行個體資料盤大小,單位為GiB。取值範圍:20~32768
en: 'ECS Instance disk size, range of values: 20-32768, units: GB'
Default: 200
MinValue: 20
MaxValue: 32768
AllocatePublicIP:
Type: Boolean
Label:
zh-cn: 開啟公網IP
en: allocate public ip
Default: true
InternetMaxBandwidthOut:
Type: Number
Label:
zh-cn: 流量公網頻寬
en: Internet Max Bandwidth Out
Description:
zh-cn: 取值範圍0-100, 0為不開公網ip
en: no public ip if zero
AssociationPropertyMetadata:
Visible:
Condition:
Fn::Equals:
- ${AllocatePublicIP}
- true
Default: 5
MinValue: 0
MaxValue: 100
Resources:
EcsVpc:
Type: ALIYUN::ECS::VPC
Condition: CreateVpcConditions
Properties:
CidrBlock:
Ref: VpcCidrBlock
VpcName:
Ref: ALIYUN::StackName
EcsVSwitch:
Type: ALIYUN::ECS::VSwitch
Condition: CreateVpcConditions
Properties:
ZoneId:
Ref: ZoneId
VpcId:
Ref: EcsVpc
CidrBlock:
Ref: VSwitchCidrBlock
EcsSecurityGroup:
Type: ALIYUN::ECS::SecurityGroup
Properties:
# 按照軟體名稱設定安全性群組名稱
#SecurityGroupName: nginx-sg
VpcId:
Fn::If:
- CreateVpcConditions
- Ref: EcsVpc
- Ref: VpcId
# 按照軟體監聽的連接埠,設定入方向規則(安全性群組預設出方向的全部連接埠都是允許存取的)
#SecurityGroupIngress:
# - PortRange: 80/80
# Priority: 1
# SourceCidrIp: 0.0.0.0/0
# IpProtocol: tcp
EcsInstanceGroup:
Type: ALIYUN::ECS::InstanceGroup
Properties:
ZoneId:
Ref: ZoneId
VpcId:
Fn::If:
- CreateVpcConditions
- Ref: EcsVpc
- Ref: VpcId
VSwitchId:
Fn::If:
- CreateVpcConditions
- Ref: EcsVSwitch
- Ref: VSwitchId
SecurityGroupId:
Ref: EcsSecurityGroup
ImageId: centos_7
IoOptimized: optimized
InstanceChargeType:
Ref: PayType
PeriodUnit:
Ref: PayPeriodUnit
Period:
Ref: PayPeriod
SystemDiskCategory:
Ref: SystemDiskCategory
# 系統硬碟大小可以根據需要調整
SystemDiskSize:
Ref: SystemDiskSize
# 資料盤配置
DiskMappings:
- Category:
Ref: DataDiskCategory
Size:
Ref: DataDiskSize
MaxAmount: 1
InstanceType:
Ref: EcsInstanceType
Password:
Ref: InstancePassword
# 公網頻寬, 為 0 代表不開公網
InternetMaxBandwidthOut:
Fn::If:
- IfAllocatePublicIP
- Ref: InternetMaxBandwidthOut
- 0
InstanceName:
Fn::Join:
- '-'
- - Ref: ALIYUN::StackName
- '[1,4]'
WaitConditionHandle:
Type: ALIYUN::ROS::WaitConditionHandle
Properties: {}
WaitCondition:
Type: ALIYUN::ROS::WaitCondition
Properties:
Count: 1
Handle:
Ref: WaitConditionHandle
# 等待命令執行完成的逾時時間
Timeout: 300
InstanceRunCommand:
Type: ALIYUN::ECS::RunCommand
Properties:
InstanceIds:
Fn::GetAtt:
- EcsInstanceGroup
- InstanceIds
CommandContent:
Fn::Sub:
- |
#!/bin/bash
# 1. 如果使用了資料盤,需要對資料盤進行格式化,並掛載到指定目錄,這裡假定掛載到/data
init_and_mount_data_disk() {
local fs_type=$1
local mount_point=$2
# 找出磁碟裝置(排除分區)
devices=(`lsblk -o NAME,FSTYPE --noheadings --nodeps | awk '$2 == "" {print $1}'`)
# 找出不存在檔案系統的磁碟裝置
for device in ${!devices[@]};
do
lsblk -o NAME,FSTYPE --noheadings | awk '$2 != "" {print $1}' | grep ${!device}
if [[ $? -eq 1 ]]; then
data_disk=${!device}
break
fi
done
# 為資料盤建立檔案系統
mkfs -t ${!fs_type} /dev/${!data_disk}
cp /etc/fstab /etc/fstab.bak
mkdir ${!mount_point}
# 更新/etc/fstab並掛載磁碟
echo `blkid /dev/${!data_disk} | awk '{print $2}' | sed 's/\\\"//g'` ${!mount_point} ext4 defaults 0 0 >> /etc/fstab
mount -a
}
# 掛盤到/data
init_and_mount_data_disk ext4 /data
# 2. 安裝軟體,或者對軟體進行初始化配置,比如將資料檔案目錄指定到資料盤目錄
# yum install -y nginx
# 3. 如果需要開機自啟動,請添加系統服務並啟用
# systemctl enable nginx.service
# 4. 配置啟動指令碼或者啟動系統服務
# systemctl start nginx.service
# 5. 執行成功回調WaitCondition結束waitCondition的等待
${CurlCli} -d "{\"Data\" : \"Success\", \"status\" : \"SUCCESS\"}"
# 指令碼裡任何地方如果發現執行失敗,使用以下命令返回FAILURE和錯誤資訊
# ${CurlCli} -d "{\"Data\" : \"error message\", \"status\" : \"FAILURE\"}"
- CurlCli:
Fn::GetAtt:
- WaitConditionHandle
- CurlCli
Type: RunShellScript
# 按照命令執行時間設定逾時,單位是秒;命令執行逾時則部署失敗
Timeout: 300
# outputs是根據軟體需求定義
# Outputs are defined according to software requirements
# 以下是以nginx為例,輸出了網站公網訪問地址
# The following is an example of nginx, which outputs the public network access address of the website
#Outputs:
# endpoint:
# Condition: IfAllocatePublicIP
# Description:
# zh-cn: 對外暴露的公網IP地址
# en: Public IP Addresses
# Value:
# Fn::Sub:
# - http://${ServerAddress}
# - ServerAddress:
# Fn::Select:
# - 0
# - Fn::GetAtt:
# - EcsInstanceGroup
# - PublicIps
Metadata:
ALIYUN::ROS::Interface:
## 參數組的順序會確定使用者建立服務執行個體頁面的參數排列順序
## The order of the parameter groups determines the order in which the parameters are arranged on the user creation service instance page
## 如果絕大部分執行個體規格都適用,比如x86架構,可以按注釋中將可用性區域放在前面選擇
## If most of the instance specifications are applicable, such as the x86 architecture, you can select the availability zone in front of the note
# ParameterGroups:
# - Parameters:
# - ZoneId
# Label:
# default:
# zh-cn: 可用性區域配置
# en: Zone Configuration
# - Parameters:
# - WhetherCreateVpc
# - VpcCidrBlock
# - VSwitchCidrBlock
# - VpcId
# - VSwitchId
# Label:
# default:
# zh-cn: VPC配置
# en: VPC Configuration
# - Parameters:
# - PayType
# - PayPeriodUnit
# - PayPeriod
# Label:
# default:
# en: PayType Configuration
# zh-cn: 付費類型配置
# - Parameters:
# - EcsInstanceType
# - InstancePassword
# - SystemDiskCategory
# - SystemDiskSize
# - DataDiskCategory
# - DataDiskSize
# - AllocatePublicIP
# - InternetMaxBandwidthOut
# Label:
# default:
# en: Instance
# zh-cn: ECS執行個體配置
ParameterGroups:
- Parameters:
- PayType
- PayPeriodUnit
- PayPeriod
Label:
default:
en: PayType Configuration
zh-cn: 付費類型配置
- Parameters:
- EcsInstanceType
Label:
default:
zh-cn: ECS執行個體規格配置
en: ECS Instance Type Configuration
- Parameters:
- ZoneId
Label:
default:
zh-cn: 可用性區域配置
en: Zone Configuration
- Parameters:
- WhetherCreateVpc
- VpcCidrBlock
- VSwitchCidrBlock
- VpcId
- VSwitchId
Label:
default:
zh-cn: VPC配置
en: VPC Configuration
- Parameters:
- InstancePassword
- SystemDiskCategory
- SystemDiskSize
- DataDiskCategory
- DataDiskSize
- AllocatePublicIP
- InternetMaxBandwidthOut
Label:
default:
en: Instance
zh-cn: ECS執行個體詳細配置
TemplateTags:
- acs:example:ISV軟體部署:單一實例自訂鏡像部署帶資料盤公網IP可選