Logo AppDev24 Login / Sign Up
Sign Up
Have Login?
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Login
New Account?
Recovery
Go to Login
By continuing you indicate that you agree to Terms of Service and Privacy Policy of the site.
Python Script

Connect to Snowflake from Python Lambda Function

Updated on Jun 23, 2024

As data processing and analytics continue to play a crucial role in modern businesses, the need to integrate various data sources with cloud-based data warehouses has become increasingly important. In this article, we will explore how to connect to Snowflake, a popular cloud-based data warehouse, from an AWS Lambda function written in Python.

Lets consider that the access credentials in order to connect to Snowflake is available in AWS Secrets Manager.

Below is the sample Python code that demonstrates how to connect to Snowflake:

import os
import base64
import json
import boto3
from botocore.exceptions import ClientError
import snowflake.connector
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives.asymmetric import dsa
from cryptography.hazmat.primitives import serialization



# Snowflake Access Secrets
sf_access = os.environ['sf_access']
sm_client = boto3.client('secretsmanager', region_name = 'eu-central-1')
get_secret_value_response = sm_client.get_secret_value(
    SecretId = sf_access
)
if 'SecretString' in get_secret_value_response:
    secret = json.loads(get_secret_value_response['SecretString'])
else:
    secret = json.loads(base64.b64decode(get_secret_value_response['SecretBinary']))


# Snowflake Settings
p_key = serialization.load_pem_private_key(
  data = secret['sf_private_key'].encode(),
  password = secret['sf_passphrase'].encode(),
  backend = default_backend()
)

pkb = p_key.private_bytes(
  encoding = serialization.Encoding.DER,
  format = serialization.PrivateFormat.PKCS8,
  encryption_algorithm = serialization.NoEncryption()
)

conn = snowflake.connector.connect(
    account = secret['sf_account'],
    user = secret['sf_user'],
    role = secret['sf_role'],
    private_key = pkb,
    warehouse = secret['sf_warehouse'],
    database = secret['sf_database'],
    schema = secret['sf_schema']
)

print('Connection to Snowflake succeeded')
cur = conn.cursor()


def lambda_handler(event, context):
    cust_id = event['cust_id']
    cust_name = event['cust_name']
    cust_address = event['cust_address']

    query = f"""INSERT INTO mydatabase.myschema.mytable
            (CUST_ID, CUST_NAME, CUST_ADDRESS, CUST_STATUS)
            VALUES ('{cust_id}', '{cust_name}', '{cust_address}', 'JOINED')
        """
    cur.execute(query)

    return True

In this article, we have demonstrated how to connect to Snowflake from an AWS Lambda function written in Python. By following these steps, you can integrate your Snowflake data warehouse with your AWS-based applications and workflows.

PrimeChess

PrimeChess.org

PrimeChess.org makes elite chess training accessible and affordable for everyone. For the past 6 years, we have offered free chess camps for kids in Singapore and India, and during that time, we also observed many average-rated coaches charging far too much for their services.

To change that, we assembled a team of top-rated coaches including International Masters (IM) or coaches with multiple IM or GM norms, to provide online classes starting from $50 per month (8 classes each month + 4 tournaments)

This affordability is only possible if we get more students. This is why it will be very helpful if you could please pass-on this message to others.

Exclucively For Indian Residents: 
Basic - ₹1500
Intermediate- ₹2000
Advanced - ₹2500

Top 10 Articles