Trigger AWS lambda function through streamlit

Hi, how can I trigger my lambda function through streamlit app?

Hi @Jasnoor_Kaur_US,

Thanks for posting!

You can use the invoke function of boto3 if you haven’t explored that option yet.

Detailed examples are listed on the official docs from AWS but essentially you would need to;

  1. Configure AWS credentials
    session = boto3.Session(
        aws_access_key_id='YOUR_ACCESS_KEY',
        aws_secret_access_key='YOUR_SECRET_KEY',
        region_name='YOUR_REGION'
    )
    
  2. Create Lambda client: lambda_client = session.client('lambda')
  3. Trigger the Lambda function

Example trigger with st.button:

    if st.button("Trigger"):
        response = lambda_client.invoke(
            FunctionName='YOUR_FUNCTION_NAME',
            Payload='{}'
        )

Hi @Jasnoor_Kaur_US, and welcome to our forums! :raised_hands:

Once your AWS Lambda and AWS API Gateway are set up, you should be able to send a request from your Streamlit app as follows:

import streamlit as st
import requests

if st.button('Trigger Lambda'):
    api_url = 'https://your-api-url.amazonaws.com/your-endpoint'
    response = requests.post(api_url, json={'key':'value'})  # replace with your actual payload
    if response.status_code == 200:
        st.write("AWS Lambda function triggered successfully!")
    else:
        st.write(f"Failed to trigger AWS Lambda function. Status code: {response.status_code}")

Remember to replace 'https://your-api-url.amazonaws.com/your-endpoint' with your actual API URL, and {'key':'value'} with the actual payload you want to send to your Lambda function.

Let me know how it goes. I’d be happy to help with further questions :slight_smile:

Best,
Charly

Seems like we posted our answers pretty much at the same time, @tonykip! :smiley:

Nice, 2 is better than 1 :joy: