When you use features such as logging, VPC access, or asynchronous invocation, Function Compute requires permissions to access other cloud services. For example, to configure function logging, you must grant Function Compute write permissions to a specified Logstore to write function logs.
To simplify the authorization process, Function Compute supports a service-linked role. Functions use this role by default, allowing them to access features like logging, VPC access, and asynchronous invocation without extra role configuration.
If your function's code needs to access other cloud services or requires more granular permissions, you must configure a Function Role. When the function is invoked, Function Compute automatically assumes the configured role.
How it works
Function Compute, based on the role that you configure for a function, obtains temporary credentials (an STS token) by calling AssumeRole and passes them to your function through the Credentials or credentials parameter in the context. These temporary credentials grant access to all resources covered by the configured permissions, and you can use them in your function code to access other Alibaba Cloud services.
The temporary credentials are valid for a fixed duration of 36 hours. Because a function's maximum execution duration is 24 hours, the credentials will not expire during its execution.
The location of the Credentials or credentials parameter varies depending on the runtime. For details on specific runtimes, see the following links. For custom runtimes and custom images, the temporary credentials are injected into the HTTP request headers.
Example: Grant access to OSS
This example shows how to grant a function permission to manage OSS. You will create a role with OSS permissions and then bind it to the function.
Prerequisites
Procedure
Step 1: Create a role and grant permissions
-
Log on to the RAM console. In the left-side navigation pane, choose Roles > Create Role.
-
On the Create Role page, select Cloud Service for Principal Type and Function Compute / FC for Principal Name. Then, click OK.
-
In the Create Role dialog box that appears, set the Role Name, such as
mytestrole, and then click OK. This redirects you to the role details page. -
On the Permissions tab, click Grant Permission. In the Grant Permission panel, follow these steps to grant permissions to the role.
-
Select the Resource Scope. The Principal is the target role by default. From the policy list, select the check box next to a system or custom policy to add it to the Selected Policies list. Then, click OK. For more information, see Policies and examples.
-
Account: The permissions take effect within the current Alibaba Cloud account.
-
Resource Group: The permissions take effect within the specified resource group. This option is available only if the cloud service supports resource groups. For more information, see Cloud services that support resource groups.
In this example, to manage OSS, you must add the AliyunOSSFullAccess system policy to the new role.
-
Step 2: Bind the role to the target function
-
Log on to the Function Compute console. In the left-side navigation pane, choose .
-
In the top menu bar, select a region. On the Functions page, find the target function and click Configuration in the Actions column.
-
On the function details page, select the Configuration tab. In the Advanced Settings section, click Modify. In the Advanced Settings panel, expand the Permissions section. For Function Role, select the
mytestrolerole that you created in Step 1. Then, click Deploy.
Step 3: Test the function
Test whether the function associated with the mytestrole role has permissions to manage OSS.
-
On the function list page, click the target function. On the Code tab, click the arrow next to Test Function and select Configure Test Parameters. The following is a sample test event.
{ "endpoint": "http://oss-cn-hangzhou.aliyuncs.com", "bucket": "web****", "objectName": "myObj", "message": "your-message" }In this example, replace the value of
bucketwith the name of your bucket, which must be in the same region as the function. -
On the Code tab, enter the code in the code editor, and then click Deploy.
This example uses the Python runtime. The code uses the temporary credentials provided by Function Compute to access OSS.
import json import oss2 def handler(event, context): evt = json.loads(event) creds = context.credentials # Use the temporary credentials injected by Function Compute. # Your Alibaba Cloud account AccessKey pair grants full access to all API operations. # We recommend that you use a RAM user for API access and routine O&M. # To prevent security risks, do not hardcode the AccessKey ID and AccessKey secret in your project code. # This example shows how to obtain the AccessKey ID and AccessKey secret from the context. auth = oss2.StsAuth(creds.access_key_id, creds.access_key_secret, creds.security_token) bucket = oss2.Bucket(auth, evt['endpoint'], evt['bucket']) bucket.put_object(evt['objectName'], evt['message']) return 'success' -
Click Test Function. After the function executes successfully, log on to the OSS console, find the target bucket, and verify that the target object's content is updated to the value of the
messageparameter from the test event.
References
-
Function Compute 3.0 supports service-linked roles for least-privilege authorization. For details on the service-linked role policy, see AliyunServiceRoleForFC.
-
To learn how to configure a function role, see Create a function.