已在 Parameter Store 建立兩個參數
已建立以下 Lambda function Code
import boto3 import json def lambda_handler(event, context): # 初始化 SSM 客戶端 ssm = boto3.client('ssm') # 參數名稱 parameters_to_get = [ '/Dev/DBServer/MySQL/db-user', '/Dev/DBServer/MySQL/db-pwd' ] # 從 Parameter Store 獲取參數值 response = ssm.get_parameters( Names=parameters_to_get, WithDecryption=True # 如果參數是加密的,設置為 True ) # 初始化一個變數來存儲參數名稱和值 parameters_values = {} # 檢查並讀取每個參數的值 for param in response['Parameters']: parameters_values[param['Name']] = param['Value'] # 列印參數值 print(f"Parameter Values: {json.dumps(parameters_values)}") return { 'statusCode': 200, 'body': json.dumps(f"Parameter Values: {parameters_values}") }
賦予 Lambda 有讀取 SSM 的權限
執行結果
Test Event Name MyTest Response { "statusCode": 200, "body": "\"Parameter Values: {'/Dev/DBServer/MySQL/db-pwd': 'mypassword', '/Dev/DBServer/MySQL/db-user': 'admin'}\"" } Function Logs START RequestId: 7c95489a-f09d-44b8-9b5e-25968ca0b318 Version: $LATEST Parameter Values: {"/Dev/DBServer/MySQL/db-pwd": "mypassword", "/Dev/DBServer/MySQL/db-user": "admin"} END RequestId: 7c95489a-f09d-44b8-9b5e-25968ca0b318 REPORT RequestId: 7c95489a-f09d-44b8-9b5e-25968ca0b318 Duration: 2379.66 ms Billed Duration: 2380 ms Memory Size: 128 MB Max Memory Used: 78 MB Init Duration: 301.53 ms Request ID 7c95489a-f09d-44b8-9b5e-25968ca0b318
No comments:
Post a Comment