AWS Lambda: Send a Message with Discord Webhooks
Tags: aws, python, cloud, serverless
Published: 2021-12-25 08:02:07

## Introduction

Learn how to send a message from AWS Lambda to a Discord channel using Discord's built-in Webhooks function. This tutorial will show you how to configure the AWS Lambda and a Discord webhook endpoint to send messages to a specific Discord channel.

What is AWS Lambda?

- Lambda is a compute service that lets you run code without provisioning or managing servers.
- Lambda runs your function only when needed and scales automatically, from a few requests per day to thousands per second.
- You can run code for virtually any type of application or backend service.
- Learn more about AWS Lambda [here](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html)

What is a Discord Webhook?

- Discord's built-in Webhooks function as an easy way to get automated messages and data updates sent to a text channel in your server. Learn more about webhook [here](https://discord.com/developers/docs/resources/webhook)
)
- Tip: Discord messages have "Rate Limits, meaning you can only send a specific number of messages, or requests to be exact, per second.  You can learn about it [here](https://discord.com/developers/docs/topics/rate-limits)

## Getting Started

This tutorial assumes you are familiar and comfortable utilizing the terminal, navigating the file system without a GUI, and navigating the AWS Console. To take full advantage of this tutorial, review the list of prerequisites below.

Prerequisites: 
- Access to AWS account
- Access to Discord account
- Python installed on your system
- Familiarity with Python programming

### Create a Discord Server

We will first create a Discord server and use the default Discord channel to prepare incoming messages.

> If you already have a discord server created and a channel ready, you can skip this step and jump to the "Prepare AWS Lambda Layer" section.

Navigate to the Discord website or open the Discord desktop app to access your account. Next, create a discord server by clicking on the green button with the big plus symbol.


![create now button](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/339ljhnktif0zhgqe0jy.png)

On the **Create a server** window, click the **Create My Own** button.



![create server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mgfvvmms8f46869mjnx8.png)


On the following **Tell us more about your server** window, click on the **For me and my friends** button.


![tell us more](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cjoc28l3cfhsmje8bbps.png)

Finally, on the **Customize your server** window, you can change the server's name in the **Server Name** field or click on the **Create** button to complete the server setup.

![customize your server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l2k5ec44as7hmf01ux8q.png)


### Create a Discord Webhook

In this example, I will use the default channel to create my webhook url, but you can create another channel if you desire.

Locate the **Edit Channel** button, which looks like a "Gear" icon on the left-hand side of the **Text Channels** pane. Click on the "Gear" icon to display the settings window.

![settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vj5vp6l8hvgt61kykjch.png)

Next, click on the **Integrations** button to display Integrations settings. Then click on the **Webhooks** button to open a new webhooks window.


![create webhook](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oytwxokd57e4pdn09a0v.png)

You can leave everything as is or if you have more than one channel, use the drop-down menu to select another channel. You can also take this time to change the title of the channel.


![change webhook settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/21gbaz1v6qd7vkz3f50a.png)

And finally, click on the **Copy Webhook URL** button, which copies the string to your clipboard. Next, close the window by clicking on the "X" button in the top right corner.


![copy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x50qpm1jaqqxj4lwqlcz.png)

And that's it!  Move on to the next section to configure your AWS Lambda.

## Prepare AWS Lambda Layer

To utilize Python packages in an AWS Lambda project, you must first upload a zip file containing all your Python packages to an **AWS Lambda Layer**. I will show you how to set that up in the following few sections.

Let's begin.

### Create local directory

We're only installing one package called **discord-webhook**to keep our project simple.  For more code examples and usage visit the link [here](https://pypi.org/project/discord-webhook/)

Next, create a directory/folder and give it a unique name or open a terminal and type the command below like so:

```bash
$  mkdir my-project
```

### Download Python packages

For this next step, you will need to access the Terminal window. If it's not already open, open the Terminal window and type the following command to change the directory and install the **discord-webhook** package in the current directory.

```bash
$ cd ~/Desktop/my-project && pip3 install -t . discord-webhook
```

The **-t** flag forces Python to install all your packages in the current folder by "targeting" the current working directory/folder. In our case, it's the current directory indicated by the period symbol **"."** located after the "-t" and "discord-webhook" is our Python package.

Next, remove unnecessary files by typing the command below like so:

```bash
$ rm -r *dist-info __pycache__
```

Suppose you see a warning like the one below. It just means that a specific folder doesn't exist. Some systems include this folder when downloading python packages.
```
rm: __pycache__: No such file or directory
```

> At the time of this writing, the current version of discord-webhook is 0.14.0.

<br>

### Compress and Zip directory

You can zip the folder manually or use this handy terminal command by typing the following like so:

```bash
$ zip -r -q ~/Desktop/my-project.zip .
```
<br>

## Configure AWS Lambda Layer in the cloud

Before launching and executing our main program from the AWS Lambda console, we need to set up our AWS Lambda Layer containing the Python packages you download earlier.


### Create AWS Lambda Layer

Open a web browser and sign in to your Amazon Web Services console. Next, locate the search bar, type "lambda," then click on the **Lambda** link to navigate the AWS Lambda Function web page. See the image below for guidance.


![aws lambda console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fxi6aouqby6njrfq0f9f.png)

On the sidebar on the left-hand side, locate and click on the **Layers** link to jump to the Layers page. Next, find and click on the **Create layer** button to initiate the "AWS Lambda Layer" creation process. See the image below for guidance.


![create layer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8wh63v8wut7ob41g8m6.png)

### Upload Zip File

Use the list and image below to help fill out the form.

1) Name: **Discord-Layer**
2) Description: **Write description for your layer**
3) Select: **Upload a .zip file**
4) Upload file: **my-project.zip**
5) Select architecture: **x86_64**
6) Select runtimes: **Python 3.9**
7) Click **Create** button

![layer form](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8zqnixfc35a0aci89hzj.png)

Excellent! Now you're ready to create your AWS Lambda Function!

## Create AWS Lambda Function

On the left-hand sidebar, locate and click on the **Functions** link, then on your right-hand side, locate and click on the **Create function** button; this will open a new window.

![create lambda function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9t06clbaxbj4qrv6f0bp.png)

By default, the **Author from scratch** is selected. Leave it as is and scroll down to fill out the form. You can also use the image below to guide you.

1) Function Name: **Discord-Webhook**
2) Runtime: **Python 3.9**
3) Architechture: **x86_64**
4) Click **Create function**


![lambda function form](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/710m8gqqxudmlshiymai.png)

You can leave the rest of the settings and wait a moment for the AWS Function configuration process to finish.

### Import Python modules

When you arrive at your AWS Function page, scroll to the bottom of the page, locate and click the **Add a layer** button.

![add layer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/59q54z4d5fcay750zb5l.png)

1) Layer Source: **Custom layers**
2) Custom layers: **Discord-Layer**
3) Version: **1**
4) Click **Add**


![import layer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ghzd8muaifcpxg0il3hl.png)

### Configure Test Event

Before we can execute our AWS Lambda Function, you need to configure a Test event.


![configure event](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/abzvd5tche4wds0sd6av.png)

1) Saved Test Events: **MyDiscordEvent**
2) Click **Create** button


![save event](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pe7zwg283qtxbusb3o7p.png)

### Import Python Modules

Now for the fun stuff!

Go ahead and copy-paste the code below into the cloud nine text editor. Make sure to replace this **<replace_with_your_hook>** with your Discord channel url webhook.

```python
webhook = DiscordWebhook(url="<replace_with_your_hook>")

embed = DiscordEmbed(
    title="Taco Love",
    description="You deserve some taco love today!",
    color="03b2f8")

embed.set_image(url="https://cdn.dribbble.com/users/545781/screenshots/3157610/happy-taco.jpg")
webhook.add_embed(embed)
webhook.execute()

def lambda_handler(event, context):

    return {
        "statusCode": 200,
        "body": {"message": "Hello World"}
    }
```

### Send Discord Message

Now, for the moment of truth!

Open your discord channel and click on the orange **Test** button.


![launch function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/linmx0luctp1gt94iinn.png)

If everything was done correctly, you should see a nice happy taco!



![happy taco](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z8taemlh13kkg63edv8k.png)

### conclusion

Recap:
- You created and configured a AWS Lambda Function
- You created and configured a Discord Webhook
- Implemented a Discord API using Python programming language

--
--

If you've gotten this far YOU. ARE. AWESOME! 

At times, working with AWS Console can be a bit discouraging and a bit scary. I hope this tutorial gives you a bit more confidence and more courage to continue pursuing code and any of your cloud certifications!

Until next time,

Stay AWSome!