簡體   English   中英

如何從 lambda function 呼叫 Secret Manager

[英]How to call secret manager from lambda function

我是 AWS 的新手。 我正在嘗試將我的 lambda function 建立到 AWS Redshift,以便我可以查詢數據庫。 我已將憑據存儲在密鑰管理器中。

我知道密鑰管理器提供了一個示例代碼來檢索應用程序中的 sercet。 但是,在復制我的 lambda function 中的代碼后,我不知道如何開始。

處理程序.py

# Use this code snippet in your app.
# If you need more information about configurations or implementing the sample code, visit the AWS docs:   
# https://aws.amazon.com/developers/getting-started/python/

import boto3
import base64
from botocore.exceptions import ClientError


def get_secret():

    secret_name = "mykeyname"
    region_name = "myregionname"

    # Create a Secrets Manager client
    session = boto3.session.Session()
    client = session.client(
        service_name='secretsmanager',
        region_name=region_name
    )

    # In this sample we only handle the specific exceptions for the 'GetSecretValue' API.
    # See https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
    # We rethrow the exception by default.

    try:
        get_secret_value_response = client.get_secret_value(
            SecretId=secret_name
        )
    except ClientError as e:
        if e.response['Error']['Code'] == 'DecryptionFailureException':
            # Secrets Manager can't decrypt the protected secret text using the provided KMS key.
            # Deal with the exception here, and/or rethrow at your discretion.
            raise e
        elif e.response['Error']['Code'] == 'InternalServiceErrorException':
            # An error occurred on the server side.
            # Deal with the exception here, and/or rethrow at your discretion.
            raise e
        elif e.response['Error']['Code'] == 'InvalidParameterException':
            # You provided an invalid value for a parameter.
            # Deal with the exception here, and/or rethrow at your discretion.
            raise e
        elif e.response['Error']['Code'] == 'InvalidRequestException':
            # You provided a parameter value that is not valid for the current state of the resource.
            # Deal with the exception here, and/or rethrow at your discretion.
            raise e
        elif e.response['Error']['Code'] == 'ResourceNotFoundException':
            # We can't find the resource that you asked for.
            # Deal with the exception here, and/or rethrow at your discretion.
            raise e
    else:
        # Decrypts secret using the associated KMS CMK.
        # Depending on whether the secret is a string or binary, one of these fields will be populated.
        if 'SecretString' in get_secret_value_response:
            secret = get_secret_value_response['SecretString']
        else:
            decoded_binary_secret = base64.b64decode(get_secret_value_response['SecretBinary'])
            
    # Your code goes here. 

如何檢查連接是否已建立以及如何從 redshift 查詢?

而且我知道我們需要在代碼中包含 lambda_handler(event,context)。

在 Amazon Redshift 中運行查詢有兩種方法。

SQL 客戶

Amazon Redshift 基於 PostgreSQL。因此,您可以使用任何知道如何與 PostgreSQL 通信的SQL 客戶端

對於 Python,一個流行的選擇是使用Psycopg – PostgreSQL Python 數據庫適配器

要連接,您需要提供端點、用戶名和密碼。 確保 Redshift 數據庫上的安全組允許來自與 AWS Lambda function 關聯的安全組的訪問。

紅移數據 API

連接到 Redshift 的更新方法是通過數據 API ,這避免了對 SQL 客戶端的需要。

它使用 IAM 憑證,因此您實際上不需要存儲在 Secrets Manager 中的密碼。 此外,它不需要連接到與 Redshift 數據庫相同的 VPC。

坦率地說,這聽起來像是一種更好的聯系方式。 (我自己還沒有嘗試過。)

請參閱: 宣布 Amazon Redshift 的數據 API

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM