Adds an Elastic Compute Service (ECS) instance or an elastic network interface (ENI) to a security group.

Description

Note We recommend that you use the ModifyInstanceAttribute operation to add instances to or remove instances from a security group, and use the ModifyNetworkInterfaceAttribute operation to add ENIs to or remove ENIs from a security group.

When you call this operation, take note of the following items:

  • Before you add an instance to a security group, the instance must be in the Stopped or Running state.
  • Each instance can be added to up to five security groups by default.
  • You can increase this number to 16 by submitting a ticket.

  • A basic security group can contain up to 2,000 instances. An advanced security group can contain up to 65,536 instances.
  • The security group and the instance must reside in the same region.
  • The security group and the instance must be of the same network type. If the network type is Virtual Private Cloud (VPC), the security group and the instance must be in the same VPC.
  • An instance and an ENI cannot be added to a security group at the same time. You cannot specify the InstanceId and NetworkInterfaceId parameters at the same time.

Debugging

OpenAPI Explorer automatically calculates the signature value. For your convenience, we recommend that you call this operation in OpenAPI Explorer. OpenAPI Explorer dynamically generates the sample code of the operation for different SDKs.

Request parameters

ParameterTypeRequiredExampleDescription
ActionStringYesJoinSecurityGroup

The operation that you want to perform. Set the value to JoinSecurityGroup.

SecurityGroupIdStringYessg-bp67acfmxazb4p****

The IDs of the security groups to which the ENI belongs. You can call the DescribeSecurityGroups operation to query the most recent list of security groups.

InstanceIdStringNoi-bp67acfmxazb4p****

The ID of the instance

Note If this parameter is specified, the NetworkInterfaceId parameter must be left empty.
NetworkInterfaceIdStringNoeni-bp13kd656hxambfe****

The ID of the ENI N.

Note If this parameter is specified, the InstanceId parameter must be left empty.
RegionIdStringNocn-hangzhou

The ID of the region. You can call the DescribeRegions operation to query the most recent list of regions.

  • You do not need to specify a region ID when you add an instance to a security group.
  • You must specify a region ID when you add an ENI to a security group.

Response parameters

ParameterTypeExampleDescription
RequestIdString473469C7-AA6F-4DC5-B3DB-A3DC0DE3C83E

The ID of the request.

Examples

Sample requests

https://ecs.aliyuncs.com/?Action=JoinSecurityGroup
&InstanceId=i-bp67acfmxazb4p****
&SecurityGroupId=sg-bp67acfmxazb4p****
&<Common request parameters>

Sample success responses

XML format

HTTP/1.1 200 OK
Content-Type:application/xml

<JoinSecurityGroupResponse>
    <RequestId>473469C7-AA6F-4DC5-B3DB-A3DC0DE3C83E</RequestId>
</JoinSecurityGroupResponse>

JSON format

HTTP/1.1 200 OK
Content-Type:application/json

{
  "RequestId" : "473469C7-AA6F-4DC5-B3DB-A3DC0DE3C83E"
}

Error codes

HttpCodeError codeError messageDescription
400InstanceSecurityGroupLimitExceededExceeding the allowed amount of security groups that an instance can be in.The error message returned because the maximum number of security groups to which the instance can belong has been reached.
400InvalidInstanceId.MismatchSpecified instance and security group are not in the same VPC.The error message returned because the specified instance and security group do not belong to the same VPC or because one of the following cases occurs: 1. The security group is of the VPC network type but the instance is not. 2. The instance is of the VPC network type but the security group is not.
400InvalidOperation.InvalidEniState%sThe error message returned because the operation is not supported while the ENI is in the current state.
400NotBelongUser%sThe error message returned because you are not authorized to manage the specified resource.
400MissingParameter.RegionIdThe specified RegionId should not be null.The error message returned because the required RegionId parameter is not specified.
500InternalErrorThe request processing has failed due to some unknown error.The error message returned because an internal error has occurred. Try again later.
403IncorrectInstanceStatusThe current status of the resource does not support this operation.The error message returned because the operation is not supported while the resource is in the current state.
403InstanceLockedForSecurityThe specified operation is denied as your instance is locked for security reasons.The error message returned because the operation is not supported while the instance is locked for security reasons.
403SecurityGroupInstanceLimitExceededThe maximum number of instances in a security group is exceeded.The error message returned because the maximum number of instances in the specified security group has been reached.
403InvalidInstanceId.AlreadyExistsThe specified instance already exists in the specified security group.The error message returned because the specified instance already exists in the specified security group.
403SecurityGroupInstanceLimitExceeded%sThe error message returned because the maximum number of instances in the security group has been reached.
403AclLimitExceed%sThe error message returned because the value of AccessPoint exceeds the upper limit.
403InstanceSecurityGroupLimitExceeded%sThe error message returned because the maximum number of security groups to which the instance can belong has been reached.
403InvalidOperation.ResourceManagedByCloudProduct%sThe error message returned because security groups managed by cloud services cannot be modified.
403InvalidOperation.InvalidEniType%sThe error message returned because the operation is not supported while the ENI is of the current type.
403InvalidOperation.VpcMismatch%sThe error message returned because the operation is invalid. Check whether the VPC in the operation corresponds to other parameters.
403InvalidOperation.EniServiceManaged%sThe error message returned because the operation is invalid.
403InvalidParam.EniIdAndInstanceId.Conflict%sThe error message returned because the InstanceId and NetworkInterfaceId parameters are both specified. These two parameters are mutually exclusive and cannot be both specified.
404InvalidSecurityGroupId.NotFoundThe specified SecurityGroupId does not exist.The error message returned because the specified security group does not exist in this account. Check whether the security group ID is correct.
404InvalidInstanceId.NotFoundThe specified InstanceId does not exist.The error message returned because the specified InstanceId parameter is invalid.
404InvalidEniId.NotFound%sThe error message returned because the specified ENI ID does not exist.

For a list of error codes, see Service error codes.