Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

lambda to redshift python

import json
import urllib.parse
import boto3
import botocore.session as bc

print('Loading function')

s3 = boto3.client('s3')


def lambda_handler(event, context):
    print("Entered lambda_handler")

    secret_name='redshift' ## HERE add the secret name created.
    session = boto3.session.Session()
    region = session.region_name
    
    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']
    
    
    bc_session = bc.get_session()
    
    session = boto3.Session(
            botocore_session=bc_session,
            region_name=region,
        )
    
    # Setup the client
    client_redshift = session.client("redshift-data")
    print("Data API client successfully loaded")
    
    query_str = "create table public.lambda_func (id int);"
                      
    print(query_str)
    
    res = client_redshift.execute_statement(Database= 'dev', SecretArn= secret_arn, Sql= query_str, ClusterIdentifier= cluster_id)
    id=res["Id"]
Source by aws.amazon.com #
 
PREVIOUS NEXT
Tagged: #lambda #redshift #python
ADD COMMENT
Topic
Name
7+5 =