Trigger AWS lambda function through streamlit

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

2 Likes

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='{}'
        )
1 Like

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

2 Likes

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

3 Likes

Nice, 2 is better than 1 :joy:

3 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.