This topic describes how to enable applications on ECS instances to access other Alibaba Cloud services by using STS temporary credentials through RAM roles. The examples show how to enable Python to access Object Storage Service (OSS).
Prerequisites
Background information
Previously, applications deployed on an ECS instance needed to use AccessKey pairs (AKs) to access other Alibaba Cloud services. An AK allows you to access Alibaba Cloud APIs with full permissions for your account. To facilitate the management of the AK by applications, you must store the AK in the application configuration files or otherwise store the AK in an ECS instance. These operations makes it more complicated to manage the AK and keep it confidential. If you need consistent deployment across regions, the AK is spread along with the images or instances created from the images. In these cases, you must update and redeploy each instance and image individually whenever you make changes to the AK.
You can attach a RAM role to an ECS instance, and use an STS temporary credential to access other cloud services from the applications within the instance. STS temporary credentials are generated and updated automatically. Applications can obtain the STS temporary credentials by using the instance metadata URL. You can use RAM roles and authorization policies to grant ECS instances with different or identical permissions to access other cloud services.
Procedure
- Step 1. Create a RAM role and configure an authorization policy.
- Step 2. Create an ECS instance and attach the RAM role to the instance.
- Step 3. Access the instance metadata URL within the instance to obtain the STS temporary credential.
- Step 4. Use SDK for Python to access OSS by using the STS temporary credential.
Step 1. Create a RAM role and configure an authorization policy.
Perform the following operations to create a RAM role and configure an authorization policy:
Step 2. Create an ECS instance and attach the RAM role to the instance.
- Attach the RAM role to an existing ECS.
Call the AttachInstanceRamRole operation to attach a RAM role to an existing VPC-type instance. The parameters are configured as follows:
- RegionId: the region ID of the ECS instance.
- RamRoleName: the name of the RAM role. EcsRamRoleTest is used in this example.
- InstanceIds: the IDs of VPC-type instances to which you want to attach the RAM role. ["i-bXXXXXXXX"] is used in this example.
- Create an ECS instance and attach the RAM role to the instance.
To create an ECS instance and attach the RAM role to the instance, perform the following steps:
- Create an instance.
Call the CreateInstance operation and set parameters based on your actual needs. The following parameters are required:
- RegionId: the region ID of the instance. cn-hangzhou is used in this example.
- ImageId: the image of the instance. centos_7_03_64_40G_alibase_20170503.vhd is used in this example.
- InstanceType: the instance type of the instance. ecs.g6.large is used in this example.
- VSwitchId: the ID of the vSwitch in the VPC to which the instance belongs. RAM roles can be attached to only VPC-type ECS instances. Therefore, the vSwitchId parameter is required.
- RamRoleName: the name of the RAM role. EcsRamRoleTest is used in this example.
If you want to authorize a RAM user to create an ECS instance with the specified RAM role attached, in addition to the permission to create an ECS instance, the RAM user must have the PassRole permission. Therefore, you must customize an authorization policy as follows and attach it to the RAM user.- If you want to configure the RAM user to create an ECS instance, [ECS RAM Action]
filed can be replaced with
ecs:CreateInstance
. You can also grant more permissions to the RAM user based on your actual needs. - If you want to grant all ECS permissions to the RAM user, [ECS RAM Action] must be
replaced with
ecs:*
.
Note For more information about values of [ECS RAM Action], see Authentication rules.{ "Statement": [ { "Action": "[ECS RAM Action]", "Resource": "*", "Effect": "Allow" }, { "Action": "ram:PassRole", "Resource": "*", "Effect": "Allow" } ], "Version": "1" }
- Configure the password and start the instance.
- Configure the instance to access the Internet in the ECS console or by calling an API operation.
- Create an instance.
Step 3. Access the instance metadata URL within the instance to obtain the STS temporary credential.
To obtain the STS temporary credential of the instance, perform the following steps.
Step 4. Use SDK for Python to access OSS by using the STS temporary credential.
In this example, SDK for Python is used to list 10 files in a specified OSS bucket located within the same region as the instance through the STS temporary credential.
- The ECS instance is connected.
- The ECS instance is installed with Python. If your instance is a Linux instance, pip is required.
- An OSS bucket is created in the same region as the instance and the name and endpoint
of the bucket are obtained. In this example, the bucket name is
ramroletest
, and the endpoint isoss-cn-hangzhou.aliyuncs.com
.
Perform the following steps to use SDK for Python to access OSS: