Get Hands-on with Amazon EKS - Workshop Event Series
Whether you're taking your first steps with Kubernetes or you're an experienced practitioner looking to sharpen your skills, our Amazon EKS workshop series delivers practical, real-world experience that moves you forward. Learn directly from AWS solutions architects and EKS specialists through hands-on sessions designed to build your confidence with Kubernetes. Register now and start building with Amazon EKS!
如何建立在 Amazon Redshift 執行查詢的 AWS Lambda 函數?
我想建立在 Amazon Redshift 執行查詢的 AWS Lambda 函數。
解決方法
**先決條件:**在建立 Lambda 函數之前,您必須先設定下列 Amazon Virtual Private Cloud 端點:
- 建立具有私有子網路的 VPC。
- 建立子網路群組。新增您剛才建立的 VPC 和子網路。
- 建立私有 Amazon Redshift 叢集。選取您剛才建立的 VPC 和子網路群組。
- 使用 AWS Secrets Manager 為 Amazon Redshift 建立新的祕密。將您的祕密命名為 redshift。
建立您的 Lambda 函數
若要建立 Lambda 函數以查詢 Amazon Redshift 叢集,請依照下列步驟執行:
-
開啟 Lambda 主控台。
-
選擇「建立函數」。
-
選擇「從頭開始撰寫」選項。
-
更新下列欄位:
**函數名稱:**輸入自訂名稱。
**執行期:**輸入您的程式碼環境。(此解決方案的範例與 Python 3.9 相容。)
**架構:**輸入您的系統架構。(此解決方案的範例與 x86_64 相容。)
**許可:**選擇「Create a new role with basic Lambda permissions」(使用基本 Lambda 許可建立新角色)。 -
選擇「建立函數」。
為您的 Lambda 函數設定正確的許可
在 Lambda 主控台,選擇「組態」。
- 選擇「許可」。
- 選擇為您的 Lambda 函數建立的角色。
- 選擇「新增許可」。
- 選擇「連接政策」。
- 將 AmazonRedshiftDataFullAccess 和 SecretsManagerReadWrite 政策新增至您的 Lambda 執行角色。
**注意:**最佳實務是僅授予執行任務所需的最低權限許可。如需詳細資訊,請參閱套用最低權限許可。
將 Python 程式碼新增到您的 Lambda 函數
- 在 Lambda 主控台,選擇「程式碼」。
- 將下列程式碼貼到「程式碼」方塊:
重要:
- 將 "dev" 替換為您資料庫的名稱。
- 在 Lambda 函數組態區段為索引鍵 SecretId 和 secret_name 新增環境變數。
import os import json import boto3 import botocore import botocore.session as bc from botocore.client import Config print('Loading function') secret_name=os.environ['SecretId'] # getting SecretId from Environment varibales session = boto3.session.Session() region = session.region_name # Initializing Secret Manager's client client = session.client( service_name='secretsmanager', region_name=region ) get_secret_value_response = client.get_secret_value( SecretId=secret_name ) secret_arn=get_secret_value_response['ARN'] secret = get_secret_value_response['SecretString'] secret_json = json.loads(secret) cluster_id=secret_json['dbClusterIdentifier'] # Initializing Botocore client bc_session = bc.get_session() session = boto3.Session( botocore_session=bc_session, region_name=region ) # Initializing Redshift's client config = Config(connect_timeout=5, read_timeout=5) client_redshift = session.client("redshift-data", config = config) def lambda_handler(event, context): print("Entered lambda_handler") query_str = "create table public.lambda_func (id int);" try: result = client_redshift.execute_statement(Database= 'dev', SecretArn= secret_arn, Sql= query_str, ClusterIdentifier= cluster_id) print("API successfully executed") except botocore.exceptions.ConnectionError as e: client_redshift_1 = session.client("redshift-data", config = config) result = client_redshift_1.execute_statement(Database= 'dev', SecretArn= secret_arn, Sql= query_str, ClusterIdentifier= cluster_id) print("API executed after reestablishing the connection") return str(result) except Exception as e: raise Exception(e) return str(result)
在此範例,Lambda 連線至 Amazon Redshift 資料庫,並在公有結構描述建立 lambda_func 資料表。
相關內容
- 已提問 1 年前

