
# 创建安全组 - CreateSecurityGroup
#### 功能介绍
创建安全组。
![](https://support.huaweicloud.com/api-vpc/public_sys-resources/note_3.0-zh-cn.png)
在创建安全组后，默认配置允许任何IP地址访问TCP协议的22端口和3389端口，这意味着允许SSH连接和RDP访问，存在一定安全隐患。
#### 调试
您可以在[API Explorer](https://apiexplorer.developer.huaweicloud.com/apiexplorer/doc?product=VPC&version=v2&api=CreateSecurityGroup)中调试该接口。
#### 授权信息
账号具备所有API的调用权限，如果使用账号下的IAM用户调用当前API，该IAM用户需具备调用API所需的权限。
- 如果使用角色与策略授权，具体权限要求请参见[权限和授权项](https://support.huaweicloud.com/api-vpc/vpc_permission_0000.html)。
- 如果使用身份策略授权，需具备如下身份策略权限。
  
  | 授权项                                                               | 访问级别  | 资源类型（\*为必须）       | 条件键                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | 别名                                      | 依赖的授权项                                 |
  |:---|:---|:---|:---|:---|:---|
  | vpc:securityGroups:create  | Write | securityGroup \*  | -                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  | -  | -  |
  | vpc:securityGroups:create  | Write | securityGroupRule | - vpc:SecurityGroupRuleDirection  - vpc:SecurityGroupRuleProtocol  - vpc:SecurityGroupRuleAction  - vpc:SecurityGroupRulePort  - vpc:SecurityGroupRuleRemoteIpAddressGroup  - vpc:SecurityGroupRuleRemoteSecurityGroup  - vpc:SecurityGroupRuleRemoteIpPrefix   | -  | -  |
  | vpc:securityGroups:create  | Write | -                 | - [g:EnterpriseProjectId](https://support.huaweicloud.com/usermanual-iam5/iam_01_1287.html)  - [g:RequestTag/\<tag-key\>](https://support.huaweicloud.com/usermanual-iam5/iam_01_1287.html)  - [g:TagKeys](https://support.huaweicloud.com/usermanual-iam5/iam_01_1287.html)                                                                                                                                                                                                                                            | -  | -  |
     
  
 
#### URI
POST /v1/{project_id}/security-groups
参数说明请参见[表1]。
 表1参数说明 
| 名称         | 是否必选 | 说明                                                                                 |
|:---|:---|:---|
| project_id | 是    | 项目ID，获取项目ID请参见[获取项目ID](https://support.huaweicloud.com/api-vpc/vpc_api_0011.html)。 |
   
#### 请求参数
表2请求参数 
| 名称             | 是否必选 | 参数类型                                                       | 说明                                                |
|:---|:---|:---|:---|
| security_group | 是    | [security_group] object | 安全组对象，请参见[表3]。 |
   
 表3security_group字段说明 
| 名称                    | 是否必选 | 参数类型   | 说明                                                                                                                                                                                                                                                                                                                                                                                                                                  |
|:---|:---|:---|:---|
| name                  | 是    | String | - 功能说明：安全组名称。  - 取值范围：1-64个字符，支持数字、字母、中文字符、_(下划线)、-（中划线）、.（点）。                                                                                                                                                                                                |
| vpc_id                | 否    | String | 安全组所在的vpc的资源标识。 说明： 当前该参数只作提示用，不约束安全组在此vpc下，不建议继续使用。                                                                                                                                                                                                                                                                                                                 |
| enterprise_project_id | 否    | String | - 功能说明：企业项目ID。创建安全组时，给安全组绑定企业项目ID。  - 取值范围：最大长度36字节，带"-"连字符的UUID格式，或者是字符串"0"。"0"表示默认企业项目。   说明： 关于企业项目ID的获取及企业项目特性的详细信息，请参见[《企业管理用户指南》](https://support.huaweicloud.com/usermanual-em/zh-cn_topic_0126101490.html)。 |
   
#### 请求示例
- 创建一个安全组，命名为sg-01，所在vpc id为3ec3b33f-ac1c-4630-ad1c-7dba1ed79d85。
  ```
  POST https://{Endpoint}/v1/{project_id}/security-groups
  {
      "security_group": {
          "name": "sg-01", 
          "vpc_id": "3ec3b33f-ac1c-4630-ad1c-7dba1ed79d85",
          "enterprise_project_id": "0aad99bc-f5f6-4f78-8404-c598d76b0ed2"
      }
  }
  ```
  
 
#### 响应参数
表4响应参数 
| 名称             | 参数类型                                                       | 说明                                                |
|:---|:---|:---|
| security_group | [security_group] object | 安全组对象，请参见[表5]。 |
   
 表5security_group字段说明 
| 名称                    | 参数类型                                                                      | 说明                                                                                                                                                                                                                                                                                                                                                                                                                                         |
|:---|:---|:---|
| name                  | String                                                                    | 安全组名称。                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| description           | String                                                                    | 安全组描述。                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| id                    | String                                                                    | 安全组唯一标识。                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| vpc_id                | String                                                                    | 安全组所在的vpc的资源标识。 说明： 当前该参数只作提示用，不约束安全组在此vpc下，不建议继续使用。                                                                                                                                                                                                                                                                                                                          |
| security_group_rules  | Array of [security_group_rule] objects | 默认安全组规则列表，保证安全组内互通。                                                                                                                                                                                                                                                                                                                                                                                                                        |
| enterprise_project_id | String                                                                    | - 功能说明：企业项目ID。创建安全组时，给安全组绑定企业项目ID。  - 取值范围：最大长度36字节，带"-"连字符的UUID格式，或者是字符串"0"。"0"表示默认企业项目。   说明： 关于企业项目ID的获取及企业项目特性的详细信息，请参见[《企业管理用户指南》](https://support.huaweicloud.com/usermanual-em/zh-cn_topic_0126101490.html)。 |
   
 表6security_group_rule对象 
| 名称                      | 参数类型    | 说明                                                                                                                                                                                                                                                                                                                                                                                                                        |
|:---|:---|:---|
| id                      | String  | 安全组规则标识。                                                                                                                                                                                                                                                                                                                                                                                                                  |
| description             | String  | - 功能说明：安全组规则描述。  - 取值范围：0-255个字符，支持数字、字母、中文字符。                                                                                                                                                                                                          |
| security_group_id       | String  | 安全组标识。                                                                                                                                                                                                                                                                                                                                                                                                                    |
| direction               | String  | - 功能说明：出入控制方向。  - 取值范围： - egress：出方向  - ingress：入方向                               |
| ethertype               | String  | - 功能说明：IP协议类型。  - 取值范围：IPv4，IPv6                                                                                                                                                                                                                      |
| protocol                | String  | - 功能说明：协议类型。  - 取值范围：icmp、tcp、udp、icmpv6、IP协议号（0\~255）(如gre协议号为47)。  - 约束：为空表示支持所有协议。                                                                                         |
| port_range_min          | Integer | - 功能说明：起始端口值。  - 取值范围：1\~65535  - 约束：不能大于port_range_max的值，为空表示所有端口，如果协议是icmp类型，取值范围请参见[安全组规则icmp协议名称对应关系表](https://support.huaweicloud.com/api-vpc/vpc_api_0009.html)。         |
| port_range_max          | Integer | - 功能说明：结束端口值。  - 取值范围：1\~65535  - 约束：协议不为icmp时，取值不能小于port_range_min的值，为空表示所有端口，如果协议是icmp类型，取值范围请参见[安全组规则icmp协议名称对应关系表](https://support.huaweicloud.com/api-vpc/vpc_api_0009.html)。   |
| remote_ip_prefix        | String  | - 功能说明：远端IP地址，当direction是egress时为虚拟机访问端的地址，当direction是ingress时为访问虚拟机的地址。  - 取值范围：IP地址，或者cidr格式  - 约束：和remote_group_id，remote_address_group_id功能互斥。                                |
| remote_group_id         | String  | - 功能说明：对端安全组id。  - 约束：和remote_ip_prefix，remote_address_group_id功能互斥。                                                                                                                                                                                |
| remote_address_group_id | String  | - 功能说明：远端IP地址组ID。  - 约束：和remote_ip_prefix，remote_group_id功能互斥。                                                                                                                                                                                            |
| tenant_id               | String  | - 功能说明：安全组规则所属项目ID。                                                                                                                                                                                                                                                                                                    |
   
#### 响应示例
```
{
    "security_group": {
        "id": "16b6e77a-08fa-42c7-aa8b-106c048884e6", 
        "name": "qq", 
        "description": "", 
        "vpc_id": "3ec3b33f-ac1c-4630-ad1c-7dba1ed79d85", 
        "enterprise_project_id": "0aad99bc-f5f6-4f78-8404-c598d76b0ed2",
        "security_group_rules": [
           {
                "id": "f11a3824-ac19-4fad-b4f1-c5f4a6dd0a80",
                "tenant_id": "060576782980d5762f9ec014dd2f1148", 
                "security_group_id": "69c999ad-d9ef-4d79-94fd-35e6ceb75325", 
                "remote_group_id": "69c999ad-d9ef-4d79-94fd-35e6ceb75325", 
                "direction": "ingress", 
                "protocol": null, 
                "description": "", 
                "ethertype": "IPv6", 
                "remote_ip_prefix": null, 
                "remote_address_group_id": null,
                "port_range_max": null,
                "port_range_min": null
            }, 
            {
                "id": "3d6480e8-9ea4-46dc-bb1b-8db190cd5677",
                "tenant_id": "060576782980d5762f9ec014dd2f1148", 
                "security_group_id": "69c999ad-d9ef-4d79-94fd-35e6ceb75325", 
                "remote_group_id": null, 
                "direction": "egress", 
                "protocol": null, 
                "description": "", 
                "ethertype": "IPv6", 
                "remote_ip_prefix": null, 
                "remote_address_group_id": null,
                "port_range_max": null,
                "port_range_min": null
            }, 
            {
                "id": "9581f18c-1fdd-43da-ace9-7758a56ef28a", 
                "tenant_id": "060576782980d5762f9ec014dd2f1148", 
                "security_group_id": "69c999ad-d9ef-4d79-94fd-35e6ceb75325", 
                "remote_group_id": null, 
                "direction": "egress", 
                "protocol": null, 
                "description": "", 
                "ethertype": "IPv4", 
                "remote_ip_prefix": null, 
                "remote_address_group_id": null,
                "port_range_max": null,
                "port_range_min": null
            }, 
            {
                "id": "a3ba270e-e58b-432d-a912-aeb7eace9fb8", 
                "tenant_id": "060576782980d5762f9ec014dd2f1148", 
                "security_group_id": "69c999ad-d9ef-4d79-94fd-35e6ceb75325", 
                "remote_group_id": "69c999ad-d9ef-4d79-94fd-35e6ceb75325", 
                "direction": "ingress", 
                "protocol": null, 
                "description": "", 
                "ethertype": "IPv4", 
                "remote_ip_prefix": null, 
                "remote_address_group_id": null,
                "port_range_max": null,
                "port_range_min": null
            }
        ]
    }
}
```
#### 状态码
请参见[状态码](https://support.huaweicloud.com/api-vpc/vpc_api_0002.html)。
#### 错误码
请参考[错误码](https://support.huaweicloud.com/api-vpc/vpc_api_0003.html)。
