tencent cloud

Tencent Cloud WeData

Python

PDF
聚焦模式
字号
最后更新时间: 2026-02-05 15:48:04

Feature Description

Edit Python code on node, and support submitting commands to WeData's Workflow Scheduling System for scheduling execution.

Parameter Description
Parameter
Description
Python version
Support Python 2 and Python 3.

Non-Use of secret_id & secret_key to Obtain SSM Credential Content

Use Cases

Hope to hide secret_id and secret_key, and also obtain the secret value stored in SSM.

Usage Method 1

The system has built-in functions to retrieve secret_value, which can be directly used in Python Task. The following is an example.
Use parameters of wedatautils.get_secret_value, the print result will be encrypted.
import wedatautils
secret_name = "test_secret_name" #credential name
version_id = "1.0" #Credential version
region="ap-guangzhou" #The region where the certificate is located
secret_value = wedatautils.get_secret_value(secret_name,version_id,region) #Get credential content
print("this is"+secret_value)

Usage Method 2

Step 1: Install the Required Python Package on the Resource Group

1. Enter Project Management > Project Execution Resource Group > Scheduling Resource Group, select a scheduling resource group and click the name to enter the details page.
2. On the resource group operation and maintenance interface, create an installation package command, select the following two installation packages that need to be installed, and click OK.
3. In the list operation column of the installation command, click Run, refresh to check the status. If the status is running successfully, it indicates successful installation.
tencentcloud-sdk-python-common
tencentcloud-sdk-python-ssm


Step 2: Configure the Resource File

1. Enter Offline Development > Resource Management, after creating a resource and uploading the resource file, click Confirm.
2. Create a resource using a local .py file. For example, the resource name is test_ssm, and the resource content is as follows.
import os
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.ssm.v20190923 import ssm_client, models


def get_secret_value(secret_name, version_id, region):
try:
secret_id = os.environ.get('dynamic_secret_id', '')
secret_key = os.environ.get('dynamic_secret_key', '')
dynamic_token = os.environ.get('dynamic_token', '')

# Initialize a credential object
cred = credential.Credential(secret_id, secret_key,dynamic_token)


# Initialize the client object for the product (ssm)
client = ssm_client.SsmClient(cred, region)
# Initialize a request object
req = models.GetSecretValueRequest()
# Set the credential name
req.SecretName = secret_name
req.VersionId = version_id
# Send request
resp = client.GetSecretValue(req)
# Output JSON format response
#print(resp.to_json_string())
# Return the credential value
return resp.SecretString
except TencentCloudSDKException as err:
print(f"Error occurred: {err}")
return None

Step 3: Obtain SSM Credential Content in Python Task

Reference the resource in Python tasks.
The way to obtain credential content in Python task code, with sample code as follows:
import os
from test_ssm import get_secret_value #test_ssm is the resource name

secret_name = "test_lola" #Credential Name
version_id = "1.0" #Credential Version
region="ap-singapore" #the region of Credential
secret_value = get_secret_value(secret_name,version_id,region) #Get the credential content
print(secret_value)
##@resource_reference("test_ssm")


帮助和支持

本页内容是否解决了您的问题?

填写满意度调查问卷,共创更好文档体验。

文档反馈