# AssociateSecurityGroupInstance

## 1. API Description

This API (AssociateSecurityGroupInstance) is used to apply a security group on an instance.

{% hint style="info" %} <mark style="color:blue;">**Note**</mark>

* <mark style="color:blue;">Status of security group should be</mark> <mark style="color:blue;">`Available`</mark><mark style="color:blue;">.</mark>
* <mark style="color:blue;">Status of instance should be</mark> <mark style="color:blue;">`RUNNING`</mark> <mark style="color:blue;">or</mark> <mark style="color:blue;">`STOPPED`</mark><mark style="color:blue;">.</mark>
* <mark style="color:blue;">You can apply at most 2 security groups on an instance.</mark>
  {% endhint %}

## 2. Input Parameters

The following request parameter list only provides API request parameters.

| Parameter Name  | Required | Type   | Description        |
| --------------- | -------- | ------ | ------------------ |
| securityGroupId | Yes      | String | Security group ID. |
| instanceId      | Yes      | String | Instance ID.       |

## 3. Output Parameters

| Parameter Name | Type   | Description                                                                                                           |
| -------------- | ------ | --------------------------------------------------------------------------------------------------------------------- |
| requestId      | String | <p>The unique request ID, which is returned for each request.</p><p>RequestId is required for locating a problem.</p> |

## 4. Code Example

{% tabs %}
{% tab title="Example" %}
**Apply a security group on an instance.**

```json
POST /api/v2/vm HTTP/1.1
Host: console.zenlayer.com
Content-Type: application/json
X-ZC-Action: AssociateSecurityGroupInstance
<Common Request Params>

Request:
{
  "securityGroupId": "your-security-group-id",
  "instanceId": "your-instance-id"
}

Response:
{
  "requestId": "T1B80EBC3-7C55-48CC-910C-345D79B795DB",
  "response": {
    "requestId": "T1B80EBC3-7C55-48CC-910C-345D79B795DB"
  }
}
```

{% endtab %}
{% endtabs %}

## 5. Developer Resources

Zenlayer Cloud API 2.0 integrates [SDKs](/api-reference/api-introduction/toolkit/api-sdk.md)，to make it easier for you to call APIs. More programming languages will be supported.

## 6. Error Codes

The following only lists the error codes related to the API business logic. For other error codes, see [Common Error Codes](/api-reference/api-introduction/instruction/commonerrorcode.md).

| HTTP Status Code | Error Code                                                  | Description                                                           |
| ---------------- | ----------------------------------------------------------- | --------------------------------------------------------------------- |
| 404              | INVALID\_SECURITY\_GROUP\_NOT\_FOUND                        | Security group does not exist.                                        |
| 403              | OPERATION\_DENIED\_SECURITY\_GROUP\_STATUS\_NOT\_AVAILABLE  | Status of security group should be `Available`.                       |
| 404              | INVALID\_INSTANCE\_NOT\_FOUND                               | Instance does not exist.                                              |
| 403              | OPERATION\_DENIED\_INSTANCE\_STATUS                         | Operations on the instance with current status are not supported.     |
| 403              | OPERATION\_DENIED\_INSTANCE\_SECURITY\_GROUP\_EXCEED\_LIMIT | Quantity of security groups applied in an instance exceeds the limit. |
| 403              | OPERATION\_DENIED\_INSTANCE\_NOT\_SUPPORT\_SECURITY\_GROUP  | Instance not support security groups.                                 |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.console.zenlayer.com/api-reference/compute/vm/security-group/associatesecuritygroupinstance.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
