This topic describes how to use Python and Pulumi to create a Linux ECS instance.
Description
Create a Pulumi project.
Deploy a Linux ECS instance.
The instance is deployed in a zone of a specified region. The instance is configured with security group rules, and the network of the instance is VPC.
Prerequisites
Python 3 or later is used.
virtualenv is installed.
Pulumi is installed based on the procedure in Install Pulumi.
Create a Pulumi project
1. Use the pulumi new -- dir command to create a working directory on your local system. Select an appropriate template as prompted.
2. Go to the next page and select the alicloud-python template.
3. Enter the project name, description, stack name, and Alibaba Cloud region (the default region is cn-beijing) as prompted.
After the project is created, a message is displayed, indicating that the project has been created. You can use the provided command to start the first deployment.
4. Confirm the files generated in the local directory after the project is created.
Where:
The __main__.py file is used to define deployment configurations.
Pulumi.demo-dev.yaml records public configurations of the stack, such as the region.
Pulumi.yaml records the project metadata, including the project name, description, and language.
The requirements.txt file defines the Python module required to run the task.
5. Go to the working directory as instructed.
cd project-demo
6. Activate the independent Python environment. Skip this step if there is only one Python version on the server.
virtualenv -p python3 venv
source venv/bin/activate
7. Install the pulumi and pulumi-alicloud modules.
pip3 install -r requirements.txt
Deploy a Linux ECS instance
1. Define the deployment file.
The content of the __main__.py file is as follows:
import pulumi
import pulumi_alicloud as alicloud
vpc = alicloud.vpc.Network("my-vpc",cidr_block="172.16.0.0/12")
az = "cn-hangzhou-i"
sg = alicloud.ecs.SecurityGroup("pulumi_sg",description="pulumi security_groups",vpc_id=vpc.id)
vswitch = alicloud.vpc.Switch("pulumi_vswitch",availability_zone=az,cidr_block="172.16.0.0/21",vpc_id=vpc.id)
sg_ids= [sg.id]
sg_rule= alicloud.ecs.SecurityGroupRule("sg_rule",security_group_id=sg.id,ip_protocol = "tcp", type= "ingress",nic_type = "intranet",port_range="22/22",cidr_ip="0.0.0.0/0")
instance=alicloud.ecs.Instance("ecs-instance2",availability_zone=az,instance_type ="ecs.t6-c1m1.large" , security_groups =sg_ids,image_id="ubuntu_18_04_64_20G_alibase_20190624.vhd",instance_name ="ecsCreatedByPulumi2",vswitch_id=vswitch.id,internet_max_bandwidth_out = 10)
The preceding code imports the pulumi_alicloud module and defines the VPC, VSwitch, security group, security group rules, and finally the ECS instance.
2. Use the Pulumi CLI to make the configurations take effect.
Run the pulumi up command.
Before applying the configurations, view the execution plan and select details.
After you confirm the settings, select yes to run the configurations. The following figure shows that five resources have been created.
3. You can also click Permalink to view the execution result on the Pulumi official website.
You have created an Alibaba Cloud ECS instance by using Pulumi.