This topic describes how to call the JoinSecurityGroup operation by using Alibaba Cloud CLI to add an ECS instance to the specified security group.

Background information

You can call the JoinSecurityGroup operation to add an ECS instance to the specified security group. We recommend that you read the description of the operation before you call the operation. 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, the instance name is used to query the ID of the instance.

    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 you want to add the target ECS instance.
    aliyun ecs DescribeSecurityGroups --RegionId cn-hangzhou --DryRun false --output cols=SecurityGroupId rows=SecurityGroups.SecurityGroup[]
    Sample response:
    SecurityGroupId
    ---------------
    sg-bp1i4c0xgqxadew2****
  3. Add the i-bp14a7xie8erwsvo**** instance to the sg-bp1i4c0xgqxadew2**** security group.
    aliyun ecs JoinSecurityGroup --SecurityGroupId sg-bp1i4c0xgqxadew2**** --InstanceId i-bp14a7xie8erwsvo****

Result

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