Batch deploy application architectures with CADT API templates
Use Cloud Architect Design Tool (CADT) APIs and templates to efficiently batch deploy application architectures.
Use case
This tutorial demonstrates how to create multiple instances of the same application architecture. As an example, you will deploy 10 independent website backend environments, each with an EIP and an ECS instance.
Workflow

Prerequisites
You need a deployment server, an ECS instance with public network access, that uses the CentOS 7.9 64-bit image. This tutorial uses a deployment server configured with 8 vCPUs and 16 GiB of memory, located in the China (Beijing) region, with a VPC CIDR block of 192.168.0.0/16.
Procedure
Create and authorize a RAM user
For security, this tutorial uses a RAM user.
Create a RAM user by following the on-screen instructions.
NoteIn a production environment, we recommend enabling multi-factor authentication (MFA).
On the Create User page, set a logon name for the user, select both OpenAPI call access and Console access, complete the other settings as prompted, and then click OK.
Download and save the RAM user's AccessKey ID/AccessKey Secret. Click Download CSV File to save the access key information. You cannot retrieve this information after you close the page.
Grant the following permissions to the RAM user:
AliyunCADTFullAccess
AliyunECSFullAccess
AliyunEIPFullAccess
AliyunVPCFullAccess
On the Add Permissions page, set Authorization Scope to your entire cloud account, search for and select the four permission policies listed above, and then click OK to grant the permissions.
Log on as the RAM user. On the Overview page of the RAM console, go to the account information section and click Copy Logon URL. Use the copied URL to open the logon page. In the Username field, enter the full logon name of the RAM user (in the format
username@company.onaliyun.com), and click Next. Enter the password and click Log on.Perform the following steps as this RAM user. After you log on, the upper-right corner of the page shows the current user is cadt-user.
Create a CADT template
Create the application architecture
Create an application as described below. Name the application APP-001. The architecture includes an EIP, a VPC (172.16.0.0/12), a vSwitch (K-172.16.0.0/24), a security group, and an ECS instance (2 vCPUs, 4 GiB). The deployment region is China (Beijing).
Verify the configuration by testing the deployment. In the upper-right corner of the application editor, click Deploy Application. After the deployment is complete, the Resource Deployment Status dialog box shows that all resources have been deployed successfully.
After the deployment succeeds, release the application to conserve resources. At the bottom of the page, click Release All Resources.
Create a private template
Navigate to the Applications > My Applications page, find the application you just created, and click Save as Template.
Configure the template information. In the Save Template dialog box, enter a template name (for example,
APP deployment template) and a region tag (for example,Beijing). You can also select Set as the default template. Then, click OK.Navigate to the Create > My Solutions page and find the template you just saved.
Click Edit Template.
On the page that opens, record the template ID. You can find the template ID in the URL in your browser's address bar.
The template ID for this example is: XPQE4N36BLZMOWTB.
Install the CADT SDK
Log on to the deployment server. At the bottom of the CADT application details page, click Remote Login.
Run the following commands to check the operating system and Python version.
cat /etc/redhat-release python3 --versionWelcome to Alibaba Cloud Elastic Compute Service ! [root@ecs-deploy ~]# cat /etc/redhat-release CentOS Linux release 7.9.2009 (Core) [root@ecs-deploy ~]# python3 --version Python 3.6.8 [root@ecs-deploy ~]#Upgrade your pip version.
python3 -m pip install --upgrade pipInstall the CADT SDK.
pip3 install alibabacloud_bpstudio20210931
Download and configure the example scripts
On the deployment server, download the example scripts.
yum -y install git git clone https://code.aliyun.com/best-practice/301.git cd 301 lsYou will see the following three files:
createAppliationFromTemplate.py
getApplication.py
deployApplication.py
Open and edit each of the three scripts. Set
b.access_key_idandb.access_key_secretto the AccessKey ID and AccessKey Secret of your RAM user.if __name__ == '__main__': b = config.Config() b.access_key_id = "xxx" b.access_key_secret = "xxx" b.endpoint = "bpstudio.cn-beijing.aliyuncs.com" b.region_id = "cn-beijing" client = client.Client(b)
Batch deploy applications via CADT API
Deployment script usage:
Batch create applications from a template
python3 createAppliationFromTemplate.py {template-id}Note{template-id}is the ID of the CADT template.Batch deploy applications
python3 deployApplication.py {application-id}Note{application-id}is the ID of the application.Query application information
python3 getApplication.py {application-id}
On the deployment server, run the following command to batch create applications from the template.
python3 createAppliationFromTemplate.py XPQE4N36BLZMOWTBThe script is configured to create 10 applications:
[root@ecs-deploy 301]# python3 createAppliationFromTemplate.py XPQE4N36BLZMOWTB /.../CryptographyDeprecationWarning: Python 3.6 is no longer supported... Creating application from template: single-node-classic-arch-1, Application ID: JJQN2VZI8OILKMT9 Creating application from template: single-node-classic-arch-2, Application ID: ... Creating application from template: single-node-classic-arch-3, Application ID: ... Creating application from template: single-node-classic-arch-4, Application ID: ... Creating application from template: single-node-classic-arch-5, Application ID: ... Creating application from template: single-node-classic-arch-6, Application ID: ... Creating application from template: single-node-classic-arch-7, Application ID: ... Creating application from template: single-node-classic-arch-8, Application ID: ... Creating application from template: single-node-classic-arch-9, Application ID: ... Creating application from template: single-node-classic-arch-10, Application ID: ...Record the application IDs from the output. You will need them to deploy the applications.
In the CADT console, navigate to the Applications > My Applications page, where the 10 applications you created are listed.
Run the following command to deploy an application.
python3 deployApplication.py {application-id}Example:
python3 deployApplication.py JJQN2VZI8OILKMT9[root@ecs-deploy 301]# python3 deployApplication.py JJQN2VZI8OILKMT9 /.../CryptographyDeprecationWarning: Python 3.6 is no longer supported... Deploying application: JJQN2VZI8OILKMT9 Creating VPC... Creating vSwitch... Creating SecurityGroup... Creating ECS... Creating EIP bindEIP bindEIP bindEIP bindEIP... Deployment task submitted. The resource IDs are as follows: vpc-xxx vsw-xxx sg-xxx i-xxx eip-xxxRepeat this step for the other nine applications, replacing the application ID each time.
Run the following command to query application information.
python3 getApplication.py {application-id}For example:
python3 getApplication.py JJQN2VZI8OILKMT9[root@ecs-deploy 301]# python3 getApplication.py JJQN2VZI8OILKMT9 /.../CryptographyDeprecationWarning: Python 3.6 is no longer supported... {'ApplicationId': 'JJQN2VZI8OILKMT9', 'Status': 'Deployed_Success', 'CreateTime': '2023-04-21 15:13:11', 'ResourceList': [...], ...}You have now batch deployed the applications.