All Products
Search
Document Center

:Use an SDK to create an elastic container instance

Last Updated:Jun 14, 2023

Elastic Container Instance provides SDKs for three programming languages: Go, Python, and Java. This topic describes how to use an SDK to create an elastic container instance. In this topic, Alibaba Cloud SDK for Java is used as an example.

Install Alibaba Cloud SDK for Java

We recommend that you use Apache Maven to install Alibaba Cloud SDK for Java. Add the following dependencies to the pom.xml file in the Maven project:

  • The core library of Alibaba Cloud SDK for Java (aliyun-java-sdk-core)

  • The aliyun-java-sdk-eci and gson dependencies

  • (Optional) The aliyun-java-sdk-vpc dependency

  • (Optional) The aliyun-java-sdk-ecs and fastjson dependencies

Note
  • Update the version of each dependency in a timely manner to use new features of the dependency. For more information, visit Maven.

  • If a VPC, vSwitch, and security group already exist, you do not need to install Alibaba Cloud SDK for Java to create a VPC, vSwitch, and security group. In this case, you can skip the installation of the aliyun-java-sdk-vpc, aliyun-java-sdk-ecs, and fastjson dependencies.

<dependencies>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <optional>true</optional>
            <version>4.6.3</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.10</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-eci</artifactId>
            <version>1.3.3</version>
        </dependency>
          <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-ecs</artifactId>
            <version>4.24.42</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.19</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-vpc</artifactId>
            <version>3.2.9</version>
        </dependency>
</dependencies>

The following procedure describes how to install Alibaba Cloud SDK for Java if you use the IntelliJ IDEA Java development tool:

  1. Open IDEA and use one of the following methods to create a Maven project:

    • Method 1: Create a Maven project in IDEA

      1. In the top navigation bar, choose File > New > Project...

      2. In the dialog box that appears, click Maven and complete the project configuration.

        sdk1
    • Method 2: Convert an existing project into a Maven project

      1. Right-click the project that you want to convert and select Add Framework Support... in the menu that appears.

        sdk2
      2. In the dialog box that appears, select Maven and click OK.

        sdk3
  2. Open the pom.xml file in the Maven project directory and add dependencies.

    After the dependencies are added, Maven automatically downloads the corresponding JAR packages.

    sdk4

Obtain an AccessKey pair

When you call API operations, you must use an AccessKey pair to complete identity verification. To protect the AccessKey pair of your Alibaba Cloud account, we recommend that you create a RAM user, grant the RAM user the permissions to access elastic container instances, and then use the AccessKey pair of the RAM user to call Elastic Container Instance SDK for Java.

For more information, see the following topics:

  1. Create a RAM user

  2. Grant permissions to RAM users

  3. Create an AccessKey pair

Create resources required by elastic container instances

Before you create an elastic container instance, you must create a VPC, vSwitch, and security group.

Note

If a VPC, vSwitch, and security group already exist, you can obtain the IDs of the VPC, vSwitch, and security group to create an elastic container instance.

  1. Create a VPC.

    The following table describes the parameters. For more information, see CreateVpc.

    Parameter

    Example

    Description

    RegionId

    cn-shanghai

    The ID of the region in which the VPC is deployed.

    Select a region where Elastic Container Instance is available. For more information, see Regions and zones.

    CidrBlock

    192.168.0.0/16

    The CIDR block of the VPC.

    You can use one of the following CIDR blocks or their subsets: 192.168.0.0/16, 172.16.0.0/12, and 10.0.0.0/8. These CIDR blocks are standard private CIDR blocks as defined by Request for Comments (RFC) documents. The subnet mask must be 8 to 28 bits in length.

    Sample code:

    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.profile.DefaultProfile;
    import com.google.gson.Gson;
    import com.aliyuncs.vpc.model.v20160428.*;
    
    public class CreateVpc {
    
        public static void main(String[] args) {
            DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai","<accessKeyId>", "<accessSecret>");
            IAcsClient client = new DefaultAcsClient(profile);
    
            CreateVpcRequest request = new CreateVpcRequest();
            request.setSysRegionId("cn-shanghai");
            request.setCidrBlock("192.168.0.0/16");
    
            try {
                CreateVpcResponse response = client.getAcsResponse(request);
                System.out.println(new Gson().toJson(response));
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                System.out.println("ErrCode:" + e.getErrCode());
                System.out.println("ErrMsg:" + e.getErrMsg());
                System.out.println("RequestId:" + e.getRequestId());
            }
    
        }
    }

    The following response indicates that the VPC is created:

    {
    	"vpcId": "vpc-uf6d60ru912b6jvco****",
    	"vRouterId": "vrt-uf68fw505rxavz4hk****",
    	"requestId": "2C7AA1E2-4407-5B41-89F2-3DADC5AEE18E",
    	"routeTableId": "vtb-uf6orqxrmsyuv8gba****",
    	"resourceGroupId": "rg-acfmzw2jz2z****"
    }
  2. Create a vSwitch in the VPC.

    The following table describes the parameters. For more information, see CreateVSwitch.

    Parameter

    Example

    Description

    VpcId

    vpc-uf6d60ru912b6jvco****

    The ID of the VPC to which the vSwitch belongs.

    ZoneId

    cn-shanghai-m

    The ID of the zone to which the vSwitch belongs.

    Select a recommended zone in the selected region. For more information, see Regions and zones.

    CidrBlock

    192.168.0.0/24

    The CIDR block of the vSwitch. The CIDR block of the vSwitch must meet the following requirements:

    • The mask must be 16 to 29 bits in length.

    • The CIDR block of the vSwitch must fall within the CIDR block of the VPC to which the vSwitch belongs.

    • The CIDR block of the vSwitch cannot be the same as the destination CIDR blocks of the routes added to the VPC. However, the CIDR block of the vSwitch can be a subset of the destination CIDR blocks.

    Sample code:

    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.profile.DefaultProfile;
    import com.google.gson.Gson;
    import java.util.*;
    import com.aliyuncs.vpc.model.v20160428.*;
    
    public class CreateVSwitch {
    
        public static void main(String[] args) {
            DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "<accessKeyId>", "<accessSecret>");
            IAcsClient client = new DefaultAcsClient(profile);
    
            CreateVSwitchRequest request = new CreateVSwitchRequest();
            request.setSysRegionId("cn-shanghai");
            request.setCidrBlock("192.168.0.0/24");
            request.setVpcId("vpc-uf6d60ru912b6jvco****");
            request.setZoneId("cn-shanghai-m");
    
            try {
                CreateVSwitchResponse response = client.getAcsResponse(request);
                System.out.println(new Gson().toJson(response));
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                System.out.println("ErrCode:" + e.getErrCode());
                System.out.println("ErrMsg:" + e.getErrMsg());
                System.out.println("RequestId:" + e.getRequestId());
            }
    
        }
    }

    The following response indicates that the vSwitch is created:

    {
    	"vSwitchId": "vsw-uf6uma67wlgxwuyb5****",
    	"requestId": "B94FAE14-043C-56B5-A763-F2E03C75AC3F"
    }
  3. Create a security group.

    You must specify a VPC when you create a security group. Sample code:

    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.profile.DefaultProfile;
    import com.google.gson.Gson;
    import java.util.*;
    import com.aliyuncs.ecs.model.v20140526.*;
    
    public class CreateSecurityGroup {
    
        public static void main(String[] args) {
            DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "<accessKeyId>", "<accessSecret>");
            IAcsClient client = new DefaultAcsClient(profile);
    
            CreateSecurityGroupRequest request = new CreateSecurityGroupRequest();
            request.setSysRegionId("cn-shanghai");
            request.setVpcId("vpc-uf6d60ru912b6jvco****");
    
            try {
                CreateSecurityGroupResponse response = client.getAcsResponse(request);
                System.out.println(new Gson().toJson(response));
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                System.out.println("ErrCode:" + e.getErrCode());
                System.out.println("ErrMsg:" + e.getErrMsg());
                System.out.println("RequestId:" + e.getRequestId());
            }
    
        }
    }

    The following response indicates that the security group is created:

    {
    	"securityGroupId": "sg-uf63jawzncdab6xb****",
    	"requestId": "73D5964C-686A-59AF-8433-209067AB51D7"
    }

Create an elastic container instance

The following table describes the parameters. For more information, see CreateContainerGroup.

Parameter

Example

Description

RegionId

cn-shanghai

The region ID of the elastic container instance.

SecurityGroupId

sg-uf63jawzncdab6xb****

The ID of the security group with which you want to associate the elastic container instance.

VSwitchId

vsw-uf6uma67wlgxwuyb5****

The vSwitch with which you want to associate the elastic container instance.

ContainerGroupName

test

The name of the elastic container instance.

Container.N.Image

registry-vpc.cn-shanghai.aliyuncs.com/eci_open/nginx:1.14.2

The image of container N.

Container.N.Name

nginx

The name of container N.

Sample code:

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.eci.model.v20180808.*;
public class CreateContainerGroup {
    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "<accessKeyId>", "<accessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);
        CreateContainerGroupRequest request = new CreateContainerGroupRequest();
        request.setRegionId("cn-shanghai");
        request.setSecurityGroupId("sg-uf63jawzncdab6xb****");
        request.setVSwitchId("vsw-uf6uma67wlgxwuyb5****");
        request.setContainerGroupName("test");
        List<CreateContainerGroupRequest.Container> containerList = new ArrayList<CreateContainerGroupRequest.Container>();
        CreateContainerGroupRequest.Container container1 = new CreateContainerGroupRequest.Container();
        container1.setImage("registry-vpc.cn-shanghai.aliyuncs.com/eci_open/nginx:1.14.2");
        container1.setName("nginx");
        containerList.add(container1);
        request.setContainers(containerList);
        try {
            CreateContainerGroupResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
}

The following response indicates that the elastic container instance is created. The containerGroupId value indicates the ID of the instance.

{
	"requestId": "FA8DB391-3F54-5998-BDA0-8195113108E8",
	"containerGroupId": "eci-uf6g69s8wou20ns7****"
}