本教程详细介绍如何使用Alibaba Cloud SDK for Java为ECS实例添加RAM角色,并查询ECS实例上已经被赋予的RAM角色。

前提条件

在使用本教程之前,请确保已完成以下操作:

  • 使用ECS管理控制台或通过API获取您需要的ECS实例ID,API接口请参见DescribeInstances
  • 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
  • 确保您已经安装了Alibaba Cloud SDK for Java,准确的SDK版本号,请参见 阿里云开发工具包(SDK)
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-ecs-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-ecs</artifactId>
            <version>4.17.4</version>
        </dependency>
    </dependencies>

代码示例

本文操作示例主要以代码形式体现,具体代码如下:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.ecs.model.v20140526.AttachInstanceRamRoleRequest;
import com.aliyuncs.ecs.model.v20140526.AttachInstanceRamRoleResponse;
import com.aliyuncs.ecs.model.v20140526.DescribeInstanceRamRoleRequest;
import com.aliyuncs.ecs.model.v20140526.DescribeInstanceRamRoleResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;

/**
 * AttachInstanceRamRole    为一台或多台ECS实例授予 实例RAM角色
 * DescribeInstanceRamRole    查询一台或者多台ECS实例上的已赋予的实例RAM角色
 */
public class AttachInstanceRamRole {

    // 实例ID,最多支持100台实例。Json格式字符串(自行定义)
    // 格式:["instanceId1", "instanceId2", "instanceId3"…]
    private static String InstanceIds = "";
    // 实例RAM角色名称。您可以使用 RAM API ListRoles查询您已创建的实例RAM角色
    private static String RamRoleName = "";

    /**
     *  程序入口
     */
    public static void main(String[] args) {
        IAcsClient client = Initialization();
        testAttachInstanceRamRole(client, InstanceIds, RamRoleName);
        DescribeInstanceRamRole(client, InstanceIds);

    }

    /**
     * DescribeInstanceRamRole    查询一台或者多台ECS实例上的已赋予的实例RAM角色
     */
    private static void DescribeInstanceRamRole(IAcsClient client, String InstanceIds) {
        DescribeInstanceRamRoleRequest request = new DescribeInstanceRamRoleRequest();
        request.setInstanceIds(InstanceIds);
        try {
            DescribeInstanceRamRoleResponse response = client.getAcsResponse(request);
            System.out.println("--------------------查询ECS实例上已赋予的RAM角色--------------------");
            System.out.println(new Gson().toJson(response));
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }

    /**
     * AttachInstanceRamRole    为一台或多台ECS实例授予 实例RAM角色
     */
    private static void testAttachInstanceRamRole(IAcsClient client, String InstanceIds, String RamRoleName) {
        AttachInstanceRamRoleRequest request = new AttachInstanceRamRoleRequest();
        // 实例ID,最多支持100台实例。Json格式字符串
        request.setInstanceIds(InstanceIds);
        // 实例RAM角色名称。您可以使用 RAM API ListRoles查询您已创建的实例RAM角色
        request.setRamRoleName("AliyunECSImageExportDefaultRole");
        try {
            AttachInstanceRamRoleResponse response = client.getAcsResponse(request);
            System.out.println("--------------------为ECS实例赋予RAM角色--------------------");
            System.out.println(RamRoleName);
            System.out.println(new Gson().toJson(response));
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
    /**
     * Initialization  初始化公共请求参数
     */
    private static IAcsClient Initialization() {
        // 初始化请求参数
        DefaultProfile profile = DefaultProfile.getProfile(
                "<your-region-id>",             // 您的可用区ID
                "<your-access-key-id>",         // 您的AccessKey ID
                "<your-access-key-secret>");    // 您的AccessKey Secret
        return new DefaultAcsClient(profile);
    }
}

执行结果

正确的返回结果类似如下:
--------------------为ECS实例赋予RAM角色--------------------
{
    "requestId": "942F079D-6FD1-49BB-8B93-E4916D555139", 
    "totalCount": 1, 
    "failCount": 1, 
    "ramRoleName": "Aliyun*****", 
    "attachInstanceRamRoleResults": [
        {
            "instanceId": "i-bp13p*****", 
            "code": "InvalidRamRole.AlreadyExists", 
            "message": "A RAM role is already attached to the instance."
        }
    ]
}
--------------------查询ECS实例上已赋予的RAM角色--------------------
{
    "requestId": "90553787-361F-445A-9598-0B6528C51512", 
    "regionId": "cn-hangzhou-dg-a01", 
    "totalCount": 1, 
    "instanceRamRoleSets": [
        {
            "instanceId": "i-bp13p7*****", 
            "ramRoleName": "Aliyun*****"
        }
    ]
}