edit-icon download-icon

Configure ECS instance RAM role

Last Updated: Mar 02, 2018

To improve the security and convenience of application deployment, Alibaba Cloud SDK supports using the ECS Metadata Service to retrieve the authentication information of an ECS instance RAM role. Therefore, the applications deployed in the ECS instance can call APIs of other products without configuring credentials in the SDK. The SDK has the same permission as the RAM role attached to the ECS instance.

Note: Make sure that the ECS instance has configured a RAM role. For more information, see Access APIs of other cloud products by using the instance RAM role.

Code example

  1. #include <iostream>
  2. #include <alibabacloud/core/AlibabaCloud.h>
  3. #include <alibabacloud/core/InstanceProfileCredentialsProvider.h>
  4. #include <alibabacloud/ecs/EcsClient.h>
  5. using namespace AlibabaCloud;
  6. using namespace AlibabaCloud::Ecs;
  7. int main(int argc, char** argv)
  8. {
  9. // Initialize the SDK
  10. AlibabaCloud::InitializeSdk();
  11. ClientConfiguration configuration("<your-region-id>");
  12. EcsClient client(std::make_shared<InstanceProfileCredentialsProvider>("<your-role-name>"), configuration);
  13. // Create an API request and set parameters
  14. Model::DescribeInstancesRequest request;
  15. request.setPageSize(10);
  16. auto outcome = client.describeInstances(request);
  17. if (!outcome.isSuccess())
  18. {
  19. // Handle exceptions
  20. std::cout << outcome.error().errorCode() << std::endl;
  21. AlibabaCloud::ShutdownSdk();
  22. return -1;
  23. }
  24. std::cout << "totalCount: " << outcome.result().getTotalCount() << std::endl;
  25. // Close the SDK
  26. AlibabaCloud::ShutdownSdk();
  27. return 0;
  28. }

Where:

  • role-name is the name of the RAM role associated with the ECS instance.

  • region-id is the ID of the region that you are using. For more information, see Regions and zones.

Note: The region ID represents the region of the API for the target service (which the RAM role can access), but do not necessarily represent the region of the ECS instance.

Thank you! We've received your feedback.