This topic describes how to create an Elastic Compute Service (ECS) instance that supports dual-stack IPv4/IPv6 addressing in different operating systems by creating or updating a stack in the Resource Orchestration Service (ROS) console. The ECS instance is created based on different conditions.
Background information
When you create an ECS instance that is assigned both IPv4 and IPv6 addresses, you must separately create an IPv6 gateway, purchase an IPv6 Internet bandwidth plan, and configure a public IP address. The configuration methods vary based on the operating systems, which increases your costs and chances of errors. You can use a sample template to create an ECS instance that supports dual-stack IPv4/IPv6 addressing in different operating systems.
The conditions are defined by one or more functions of Fn::And, Fn::Or, Fn::Not, and Fn::Equals. The conditions are evaluated based on the input parameters that you specify when you create or update a stack. You can reference other conditions, parameter values, and mappings in each condition. The following example shows how to create an ECS instance that supports dual-stack IPv4/IPv6 addressing. For more information about the sample template, see Create an ECS instance bound to a Dual IPv4/IPv6 Stack.
The judgement condition used in the template is whether the InstanceImageId value starts with centos. The Fn::Equals, Fn::Select, and Fn::Split functions are used in the Conditions section to perform data processing and logical judgment on the specified InstanceImageId parameter. The following sample code is used:
Parameters:
InstanceImageId:
Type: String
Default: centos_7
Description:
zh-cn: Image ID
en: Image ID, <br>Linux System Select:<font color='red'><b>centos_7</b></font> <br>Windows System Select:<font color='red'><b>win2008r2; win2012r2; win2016</b></font>
Label:
zh-cn: Image
en: Image
Conditions:
CreateLinux:
Fn::Equals:
- centos
- Fn::Select:
- '0'
- Fn::Split:
- _
- Ref: InstanceImageId
When you run the UserData commands to initialize the configurations for creating an ECS instance, you can use the Fn::If function to evaluate the CreateLinux condition. This way, you can run different initialization commands in different operating systems to create an ECS instance that supports dual-stack IPv4/IPv6 addressing. The following sample code is used:
Fn::If:
- CreateLinux
- Fn::Replace:
- ros-notify:
Fn::GetAtt:
- WaitConditionHandle
- CurlCli
- Fn::Join:
- ''
- - '#!/bin/sh'
- |2
- |
cd /opt
- |
wget http://ecs-image-utils.oss-cn-hangzhou.aliyuncs.com/ipv6/rhel/ecs-utils-ipv6
- |
chmod +x ./ecs-utils-ipv6
- |
./ecs-utils-ipv6
- |
ros-notify -d "{\"Data\" : \"SUCCESS\", \"Status\" : \"SUCCESS\"}"
- Fn::Replace:
- ros-notify:
Fn::GetAtt:
- WaitConditionHandle
- PowerShellCurlCli
- Fn::Join:
- ''
- - "[powershell]\r\n"
- "New-Item -Path \"C:\\set_ipv6\" -Force -type directory\r\n"
- "cd C:\\set_ipv6 \r\n"
- "$install_dir=\"C:\\set_ipv6\" \r\n"
- "$install_path = \"$install_dir\\ecs-utils-ipv6.exe\" \r\n"
- "$tool_url = 'http://ecs-image-utils.oss-cn-hangzhou.aliyuncs.com/ipv6/win/64/ecs-utils-ipv6.exe' \r\n"
- "Invoke-WebRequest -uri $tool_url -OutFile $install_path \r\n"
- "Unblock-File $install_path \r\n"
- "Start-Process -FilePath \"$install_path\" -ArgumentList \"--noenterkey\" -NoNewWindow \r\n"
- "ros-notify\r\n"