All Products
Search
Document Center

Elastic Compute Service:Custom policy examples for ECS

Last Updated:Aug 11, 2026

Create custom RAM policies for fine-grained ECS access control when system policies do not meet your requirements.

Background

Sample custom policies

Create pay-as-you-go ECS instances

{
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "ecs:DescribeImages",
        "vpc:DescribeVpcs",
        "vpc:DescribeVSwitches",
        "ecs:DescribeSecurityGroups",
        "ecs:DescribeKeyPairs",
        "ecs:DescribeTags",
        "ecs:RunInstances"
      ],
      "Resource": "*"
    }
  ],
  "Version": "1"
}

Create subscription ECS instances

To query and pay for subscription orders, call bss-related API operations. The corresponding system policy is AliyunBSSOrderAccess.

Important

If you set autoPay to true when you call RunInstances to create subscription ECS instances, permissions for bss-related API operations are not required.

{
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "ecs:DescribeImages",
        "vpc:DescribeVpcs",
        "vpc:DescribeVSwitches",
        "ecs:DescribeSecurityGroups",
        "ecs:DescribeKeyPairs",
        "ecs:DescribeTags",
        "ecs:RunInstances",
        "bss:DescribeOrderList",
        "bss:DescribeOrderDetail",
        "bss:PayOrder",
        "bss:CancelOrder"
      ],
      "Resource": "*"
    }
  ],
  "Version": "1"
}

Restart ECS instances with MFA

The following policy indicates that the authorized RAM user can restart ECS instances. The ECS instances can be restarted only when MFA is enabled for the RAM user and the RAM user uses MFA to log on. In this case, the acs:MFAPresent condition key in the Condition element is set to true.

{
  "Statement": [
    {
      "Action": "ecs:RebootInstance",
      "Effect": "Allow",
      "Resource": "*",
      "Condition": {
        "Bool": {
          "acs:MFAPresent": "true"
        }
      }
    }
  ],
  "Version": "1"
}

Manage a specific ECS instance

The following policy indicates that the authorized RAM user can view all ECS instances under an Alibaba Cloud account, but the user can manage only the i-001 ECS instance.

{
  "Statement": [
    {
      "Action": "ecs:*",
      "Effect": "Allow",
      "Resource": "acs:ecs:*:*:instance/i-001"
    },
    {
      "Action": "ecs:Describe*",
      "Effect": "Allow",
      "Resource": "*"
    }
  ],
  "Version": "1"
}

Connect to a specific ECS instance with Workbench

The authorized RAM user can view all ECS instances and resources in an Alibaba Cloud account but can connect to only the i-001 instance with Workbench.

{
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "ecs:Describe*",
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": "ecs-workbench:LoginInstance",
            "Resource": "acs:ecs-workbench:*:*:workbench/i-001"
        }
    ],
    "Version": "1"
}

Deny VNC connections to ECS instances

This policy denies VNC connections to all ECS instances:

{
    "Statement": [
        {
            "Effect": "Deny",
            "Action": "ecs:DescribeInstanceVncUrl",
            "Resource": "*"
        }
    ],
    "Version": "1"
}

View ECS instances in a specific region

The following policy indicates that the authorized RAM user can view ECS instances in the China (Qingdao) region, but cannot view disks or snapshots in this region.

{
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "ecs:Describe*",
      "Resource": "acs:ecs:cn-qingdao:*:instance/*"
    }
  ],
  "Version": "1"
}

Manage security groups

The following policy allows a RAM user to manage all ECS security groups within an Alibaba Cloud account.

{
  "Version": "1",
  "Statement": [
    {
      "Action": "ecs:*SecurityGroup*",
      "Resource": "*",
      "Effect": "Allow"
    }
  ]
}

Create instance RAM roles

{
  "Version": "1",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "ecs:CreateInstance",
        "ecs:AttachInstanceRamRole",
        "ecs:DetachInstanceRAMRole"
      ],
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": "ram:PassRole",
      "Resource": "*"
    }
  ]
}

Query ECS instances and block storage devices

{
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["ecs:DescribeInstances", "ecs:DescribeDisks"],
      "Resource": "*"
    }
  ],
  "Version": "1"
}

Purchase savings plans

{
  "Version": "1",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "bssapi:CreateSavingsPlansInstance",
      "Resource": "*"
    }
  ]
}

Deny default VPC creation during instance creation

By default, RAM users with AliyunECSFullAccess can create, view, and restart ECS instances. If a RAM user creates an instance in a region with no Virtual Private Cloud (VPC), a default VPC is automatically created. Use this policy to deny default VPC creation while retaining other permissions.

{
  "Version": "1",
  "Statement": [
    {
      "Effect": "Deny",
      "Action": "*",
      "Resource": "*",
      "Condition": {
        "StringEquals": {
          "vpc:CreateDefaultVpc": ["true"]
        }
      }
    }
  ]
}

Use prefix lists

{
  "Statement": [
    {
      "Action": [
        "ecs:CreatePrefixList",
        "ecs:ModifyPrefixList",
        "ecs:DescribePrefixLists",
        "ecs:DescribePrefixListAssociations",
        "ecs:DescribePrefixListAttributes",
        "ecs:DeletePrefixList"
      ],
      "Resource": "*",
      "Effect": "Allow"
    }
  ],
  "Version": "1"
}

Use Cloud Assistant

See Cloud Assistant Agent-specific sample custom policies.

Read OSS buckets

{
  "Version": "1",
  "Statement": [
    {
      "Action": ["oss:GetObject", "oss:GetBucketLocation", "oss:GetBucketInfo"],
      "Resource": "*",
      "Effect": "Allow"
    }
  ]
}

Read and write OSS buckets

{
  "Version": "1",
  "Statement": [
    {
      "Action": [
        "oss:GetObject",
        "oss:GetBucketLocation",
        "oss:GetBucketInfo",
        "oss:PutObject",
        "oss:DeleteObject",
        "oss:AbortMultipartUpload",
        "oss:ListMultipartUploads",
        "oss:ListParts"
      ],
      "Resource": "*",
      "Effect": "Allow"
    }
  ]
}

Access ECS resources only over HTTPS

{
  "Statement": [
    {
      "Action": "ecs:*",
      "Effect": "Allow",
      "Resource": "*",
      "Condition": {
        "Bool": {
          "acs:SecureTransport": "true"
        }
      }
    }
  ],
  "Version": "1"
}

Create only encrypted disks

For enterprises with strict security compliance requirements, attach this policy to RAM users to enforce disk encryption for all ECS instances.

{
  "Version": "1",
  "Statement": [
    {
      "Action": [
        "ecs:RunInstances",
        "ecs:CreateInstance"
      ],
      "Resource": "*",
      "Condition": {
        "StringLike": {
          "ecs:IsDiskEncrypted": "*false*"
        }
      },
      "Effect": "Deny"
    },
    {
      "Action": [
        "ecs:RunInstances",
        "ecs:CreateInstance"
      ],
      "Resource": "*",
      "Condition": {
        "StringEquals": {
          "ecs:IsSystemDiskEncrypted": "false"
        }
      },
      "Effect": "Deny"
    },
    {
      "Action": "ecs:CreateDisk",
      "Resource": "*",
      "Condition": {
        "StringLike": {
          "ecs:IsDiskEncrypted": "*false*"
        }
      },
      "Effect": "Deny"
    }
  ]
}

Create only CMK-encrypted disks

This policy restricts disk creation to CMK-encrypted disks only. After attaching this policy to a RAM user, the user must select a customer master key (CMK) to encrypt disks when creating instances or data disks.

{
  "Version": "1",
  "Statement": [
    {
      "Action": [
        "ecs:RunInstances",
        "ecs:CreateInstance"
      ],
      "Resource": "*",
      "Condition": {
        "StringLike": {
          "ecs:IsDiskByokEncrypted": "*false*"
        }
      },
      "Effect": "Deny"
    },
    {
      "Action": [
        "ecs:RunInstances",
        "ecs:CreateInstance"
      ],
      "Resource": "*",
      "Condition": {
        "StringEquals": {
          "ecs:IsSystemDiskByokEncrypted": "false"
        }
      },
      "Effect": "Deny"
    },
    {
      "Action": "ecs:CreateDisk",
      "Resource": "*",
      "Condition": {
        "StringLike": {
          "ecs:IsDiskByokEncrypted": "*false*"
        }
      },
      "Effect": "Deny"
    }
  ]
}

Create ECS instances with custom images only

This policy allows a RAM user to create ECS instances only with custom images.

{
    "Version": "1",
    "Statement": [
        {
            "Action": [
                "ecs:RunInstances",
                "ecs:CreateInstance"
            ],
            "Effect": "Deny",
            "Resource": "acs:ecs:<Region ID>:*:instance/*",
            "Condition": {
                "StringNotEquals": {
                    "ecs:ImageSource": "Custom"
                }
            }
        }
    ]
}

Deny root login to ECS instances

This policy denies root login when creating instances, replacing system disks, attaching system disks, or resetting passwords online.

{
    "Version": "1",
    "Statement": [
        {
            "Action": [
                "ecs:RunInstances",
                "ecs:CreateInstance",
                "ecs:CreateOrder",
                "ecs:ReplaceSystemDisk",
                "ecs:AttachDisk",
                "ecs:InvokeCommand"
            ],
            "Resource": "*",
            "Condition": {
                "Bool": {
                    "ecs:LoginAsNonRoot": [
                        "false"
                    ]
                }
            },
            "Effect": "Deny"
        }
    ]
}

Deny password-based login to ECS instances

This policy denies password-based login when creating instances, replacing system disks, attaching system disks, or resetting passwords. After attaching this policy, the RAM user can only use key pairs or Session Manager to log on to ECS instances.

Note

Set ecs:ImagePlatform in the Condition element to limit the scope. For example, setting ecs:ImagePlatform to linux allows password login to Windows instances but denies it for Linux instances.

{
    "Version": "1",
    "Statement": [
        {
            "Action": [
                "ecs:RunInstances",
                "ecs:CreateInstance",
                "ecs:CreateOrder",
                "ecs:ReplaceSystemDisk"
            ],
            "Resource": "*",
            "Condition": {
                "Bool": {
                    "ecs:PasswordCustomized": [
                        "true"
                    ]
                },
                "StringEquals": {
                    "ecs:ImagePlatform": "linux"
                }
            },
            "Effect": "Deny"
        },
        {
            "Action": [
                "ecs:ModifyInstanceAttribute",
                "ecs:InvokeCommand",
                "ecs:AttachDisk"
            ],
            "Resource": "*",
            "Condition": {
                "Bool": {
                    "ecs:PasswordCustomized": [
                        "true"
                    ]
                }
            },
            "Effect": "Deny"
        }
    ]
}

Deny image-preset password login

This policy denies login with image-preset passwords when creating instances or replacing system disks.

{
    "Version": "1",
    "Statement": [
        {
            "Action": [
                "ecs:RunInstances",
                "ecs:CreateInstance",
                "ecs:CreateOrder",
                "ecs:ReplaceSystemDisk"
            ],
            "Resource": "*",
            "Condition": {
                "Bool": {
                    "ecs:PasswordInherit": [
                        "true"
                    ]
                }
            },
            "Effect": "Deny"
        }
    ]
}

Deny security group rules with 0.0.0.0/0

An inbound rule with 0.0.0.0/0 allows all external IP addresses to access ECS instances in the security group, which increases security risks. Use this policy to deny creation of rules with 0.0.0.0/0 and to prevent use of the default security group for instance creation.

{
  "Version": "1",
  "Statement": [
    {
      "Effect": "Deny",
      "Action": [
        "ecs:AuthorizeSecurityGroup",
        "ecs:ConfigureSecurityGroupPermissions",
        "ecs:ModifySecurityGroupRule"
      ],
      "Resource": "*",
      "Condition": {
        "ForAnyValue:StringLike": {
          "ecs:SecurityGroupIpProtocols": [
            "TCP"
          ]
        },
        "ForAnyValue:CIDRInRange": {
          "ecs:SecurityGroupSourceCidrIps": [
            "0.0.0.0/0"
          ]
        }
      }
    },
    {
      "Effect": "Deny",
      "Action": [
        "ecs:CreateInstance",
        "ecs:RunInstances"
      ],
      "Resource": "*",
      "Condition": {
        "Bool": {
          "ecs:NotSpecifySecureGroupId": [
            "true"
          ]
        }
      }
    }
  ]
}

Enforce security hardening mode for instance metadata

This policy requires RAM users to access instance metadata only in security hardening mode when calling RunInstances or CreateInstance to create instances, or ModifyInstanceMetadataOptions to modify metadata of existing instances.

{
  "Version": "1",
  "Statement": [
    {
      "Effect": "Deny",
      "Action": [
        "ecs:RunInstances",
        "ecs:CreateInstance",
        "ecs:ModifyInstanceMetadataOptions"
      ],
      "Resource": "*",
      "Condition": {
        "StringEquals": {
          "ecs:SecurityHardeningMode": ["false"]
        }
      }
    }
  ]
}