This topic describes how to call the JoinSecurityGroup operation by using Alibaba Cloud command-line interface (CLI) to add an ECS instance to a specified security group.

Background information

You can call the JoinSecurityGroup operation to add an ECS instance to the specified security group. For more information, see JoinSecurityGroup.

When you call an API operation through Alibaba Cloud CLI, make sure that request parameter values of different data types are in required formats. For more information, see Parameter format overview.

Request examples

  1. Call the DescribeInstances operation to query the ID of the ECS instance that you want to add to a security group.

    In this example, query the ID of an ECS instance by using its name.

    aliyun ecs DescribeInstances --RegionId cn-hangzhou --InstanceName wpdemo --output cols=Instances.Instance[].InstanceId
    Sample response:
    Instances.Instance[].InstanceId
    -------------------------------
    [i-bp14a7xie8erwsvo****]
  2. Call the DescribeSecurityGroups operation to query the ID of the security group to which the target ECS instance is added.
    aliyun ecs DescribeSecurityGroups --RegionId cn-hangzhou --DryRun false --output cols=SecurityGroupId rows=SecurityGroups.SecurityGroup[]
    Sample response:
    SecurityGroupId
    ---------------
    sg-bp1i4c0xgqxadew2****
  3. Add ECS instance i-bp14a7xie8erwsvo**** to security group sg-bp1i4c0xgqxadew2****.
    aliyun ecs JoinSecurityGroup --SecurityGroupId sg-bp1i4c0xgqxadew2**** --InstanceId i-bp14a7xie8erwsvo****

Result

{
        "RequestId": "A8E6A3F5-F8CE-45EE-BB1F-53CE139401E1"
}