# Trigger AWS lambda function through streamlit

**URL:** <https://discuss.streamlit.io/t/trigger-aws-lambda-function-through-streamlit/44922>\
**Category:** Community Cloud\
**Tags:** aws, streamlit-cloud\
**Created:** [June 9, 2023, 9:26am UTC](https://discuss.streamlit.io/t/trigger-aws-lambda-function-through-streamlit/44922 "2023-06-09T09:26:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jasnoor\_Kaur\_US](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/jasnoor_kaur_us/32/27674_2.png) [@Jasnoor\_Kaur\_US](https://discuss.streamlit.io/u/Jasnoor_Kaur_US)\
**Post date:** [June 9, 2023, 9:26am UTC](https://discuss.streamlit.io/t/trigger-aws-lambda-function-through-streamlit/44922/1 "2023-06-09T09:26:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tonykip](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/tonykip/32/45167_2.png) [@tonykip](https://discuss.streamlit.io/u/tonykip)\
**Post date:** [June 9, 2023, 2:00pm UTC](https://discuss.streamlit.io/t/trigger-aws-lambda-function-through-streamlit/44922/2 "2023-06-09T14:00:02Z")

</div>

Hi @Jasnoor_Kaur_US,

Thanks for posting!

You can use the `invoke` function of [boto3](https://pypi.org/project/boto3/) if you haven’t explored that option yet.

Detailed examples are listed on the [official docs from AWS](https://docs.aws.amazon.com/code-library/latest/ug/python_3_lambda_code_examples.html) but essentially you would need to;

1. Configure AWS credentials

```bash
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`:

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

```

---

<div class="post-metadata">

**Author:** ![Charly\_Wargnier](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/charly_wargnier/32/1504_2.png) [@Charly\_Wargnier](https://discuss.streamlit.io/u/Charly_Wargnier)\
**Post date:** [June 9, 2023, 2:03pm UTC](https://discuss.streamlit.io/t/trigger-aws-lambda-function-through-streamlit/44922/3 "2023-06-09T14:03:53Z")

</div>

Hi @Jasnoor_Kaur_US, and welcome to our forums! 🙌

Once your **AWS Lambda** and **[AWS API Gateway](https://aws.amazon.com/api-gateway/)** are set up, you should be able to send a request from your Streamlit app as follows:

```python
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 🙂

Best,  
Charly

---

<div class="post-metadata">

**Author:** ![Charly\_Wargnier](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/charly_wargnier/32/1504_2.png) [@Charly\_Wargnier](https://discuss.streamlit.io/u/Charly_Wargnier)\
**Post date:** [June 10, 2023, 6:52am UTC](https://discuss.streamlit.io/t/trigger-aws-lambda-function-through-streamlit/44922/4 "2023-06-10T06:52:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tonykip](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/tonykip/32/45167_2.png) [@tonykip](https://discuss.streamlit.io/u/tonykip)\
**Post date:** [June 12, 2023, 2:22pm UTC](https://discuss.streamlit.io/t/trigger-aws-lambda-function-through-streamlit/44922/5 "2023-06-12T14:22:10Z")

</div>

Nice, 2 is better than 1 😂

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/streamlit/original/3X/c/f/cf4c2ee0d938358f82448c5fe4b9c3c91d7ce19c.svg) [@system](https://discuss.streamlit.io/u/system)\
**Post date:** [June 19, 2023, 12:31pm UTC](https://discuss.streamlit.io/t/trigger-aws-lambda-function-through-streamlit/44922/6 "2023-06-19T12:31:06Z")

</div>

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