# Restarting  inactive EC2 instance via AWS Lambda , Boto, and notifying an instace outage to Slack

Amazon Web Services (AWS) has become one of the most popular cloud computing services, providing a wide range of tools and services to help businesses run their operations smoothly and efficiently. Among its many offerings, the Elastic Compute Cloud (EC2) is a powerful and flexible service that allows users to easily deploy and manage virtual servers in the cloud.

However, even the most reliable systems can experience failures, and when an EC2 instance fails a status check, it can cause serious disruptions to your business. That’s where AWS Lambda comes in. With its ability to execute code automatically in response to events, Lambda provides an easy way to reboot a failed EC2 instance and send notifications to Slack, a popular team communication platform.

> Slack is a popular team communication platform that allows teams to collaborate and communicate effectively in real time. It provides a range of features such as chat rooms, direct messaging, file sharing, and integrations with other services, making it a powerful tool for remote and distributed teams.

Slack is widely used by companies of all sizes, from small startups to large enterprises, to improve team productivity, streamline workflows, and enhance communication. With its powerful API, Slack can also be integrated with other services, allowing users to automate workflows and notifications. In this blog, we will show you how to integrate Slack with AWS Lambda to send notifications when an EC2 instance fails a status check.

**Step 1**: Create an incoming webhook for the channel where you want notifications by going to **slack.com**. Sign up for Slack and do that. If you don’t have a channel, create one.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052178574/3e56ec75-f7b2-49a6-b5fd-457d50d149d4.png align="left")

After creating the channel, select **Browse Slack** and then **select Apps.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052180111/66cd696e-26d9-43be-ac20-68378fcc185f.png align="left")

**Step 2:** Search for incoming webhook and click on **add**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052181708/f1da8c8f-b9f9-4448-b22b-66ef28b2a718.png align="left")

**Step 3:** Click on **add to slack.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052183132/1ce3c565-abd9-4c3a-aa4c-0c9a7bb69fa7.png align="left")

**Step 4:** Decide which channel you want notifications on. Next, select **Add**. You’ll then receive all the information regarding the incoming webhook.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052184773/7660c15a-15be-42b3-bdf0-9033d34f7293.png align="left")

**Now let's move on to AWS;**

AWS EC2 instances are one of the most widely used services in the cloud computing world. They provide a scalable and flexible infrastructure to run various applications and services. However, like any other system, EC2 instances can encounter issues that may cause them to fail the status check. This can lead to unexpected downtime and impact your application’s or service’s overall performance. To ensure the high availability and reliability of your EC2 instances, it’s crucial to have a mechanism that can automatically detect and remediate such issues.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052186137/2daf3b0c-1507-43d9-8e2c-6c875ace7c9f.png align="left")

One of the ways to achieve this is by using AWS Lambda, a serverless computing service that allows you to run code without provisioning or managing servers.

> **In this part of the blog, I’ll walk you through the steps to create a Lambda function that can detect the failed status check of an EC2 instance and automatically reboot it to restore its health. So let’s get started!**

**Step 1:** We must first create a role and grant the required permission:

Click on **create new role** under **AWS IAM** Roles. Keep the other setting set to its default value. Choose the **Lambda use case, a**fter which select **next**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052187089/58e077af-74ce-4190-820b-496bdb61a8ea.png align="left")

**Step 2:** Add the following 2 permission policies: **AWSLambdaBasicExecutionRole** and **AmazonEC2FullAccess**. Choose **next.** Give your role a suitable name and click the “**create role**” button on the following screen.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052188433/7f263396-a7f0-48c9-98d9-0cf8661426c3.png align="left")

We have now successfully created a position and allocated the necessary permissions.

**Step 3:** We must now create a lambda function. Click “**Create function**” on the AWS Lambda page.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052189617/f54f3b4d-74b6-4f37-aa9c-f9b8147ea307.png align="left")

Complete the basic details

**Step 4**: Click **Change default execution role**, which is located beneath the permissions section.

The role that we previously created can be chosen by selecting **Use an Existing role**. (If you’d like, you may also create a new role similar to the one we just made.) then select the create function button.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052191242/ea4cd6d1-a979-4d62-8ef4-c4a7964ce476.png align="left")

Now, our function is created. The above code will detect whether the EC2 instances are impaired or not and send a message to slack if found.

**Step 5:** Choose **Test**. As you can see, that results in a module error. We will now look at how to import an external Python module into Amazon Lambda.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052192495/ce2bda2a-8e46-47ba-aacf-df85390b4bb5.png align="left")

**Step 6:** As quickly as possible, launch the S3 console, create an S3 bucket, and give it a unique name. The generated zip package will be saved here. Save the name somewhere safe.  
**Launch the AWS Cloudshell.** It’s at the top right corner of the console.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052193980/78fce172-931a-47a2-909d-ae2025c4f83b.png align="left")

Represents the picture.

**Step 7:** Run the following commands in the CloudShell:

`mkdir packages   cd packages   python3 -m venv venv   source venv/bin/activate   mkdir python   cd python`

Now, install the package that you require using the pip command. Consider this as an example:

`pip install requests -t`

We will be utilizing those packages for lambda. Prior to zipping the Python folder in which they are stored, let’s free up some space by removing any items with the “**.dis-info” extension**. They are unnecessary.

The following command is the best approach to do this.

`rm -rf *dist-info`

**Step 8:** With the **cd.. command**, we can now exit the Python directory.

The Python directory will now be zip-filed and give a suitable name. To do this, use the command below.

`zip -r lambda-package-name.zip python`

**Step 9:** Next, upload the packaged zip into the s3 bucket you created earlier. The command should look like the below:

`aws s3 cp python-lambda-package.zip s3://your-s3-bucket-name/`

Now, if you go to that bucket, you can find the zip uploaded on the bucket:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052195574/78c10517-a991-41a5-8dc4-8a27bd3bb040.png align="left")

**Step 10: Create a layer** now, and then upload the packages. Click on **add a new layer** under layers as seen below:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052196985/7af79615-1773-4a81-b668-61cd242a14fa.png align="left")

**Step 11:** Name the layer and select the option to upload a file from Amazon S3. Copy the URL from the zip file and paste it where indicated. Lastly, choose to **Create layer.** Ensure to choose the appropriate Compatible runtimes.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052198309/4a88a3bc-117d-4779-8551-fd11507fdece.png align="left")

**Step 12:** Let’s now link the layer with the function. Go down to the **Layers section** of the Lambda function page. To **add a layer**, select it.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052199499/f4cd6948-e849-4abf-b695-3b02dd76b8b4.png align="left")

Choose **custom layers**, choose the layer we made, and then click **Add**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052200998/7394f89b-dd33-4243-a408-a1bb4b2f9c68.png align="left")

In this way, we may add an external module to the AWS lambda function. Your code should now run smoothly and without issues. You’ll also receive the appropriate notification on Slack.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052202281/3ce22874-9208-4dab-8c93-c13092ec82e8.png align="left")

Please see the example image.

**Reference:**

[**EC2 - Boto3 Docs 1.26.74 documentation**  
\*A low-level client representing Amazon Elastic Compute Cloud (EC2) Amazon Elastic Compute Cloud (Amazon EC2) provides…\*boto3.amazonaws.com](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html)

[**Add External Python Libraries to AWS Lambda using Lambda Layers**  
\*If you are a Cloud Engineer or Developer who loves to code in python like myself, 7 out of 10 times, you might find…\*www.linkedin.com](https://www.linkedin.com/pulse/add-external-python-libraries-aws-lambda-using-layers-gabe-olokun)

> I hope this article was informative and provided you with the details you required. If you have any questions related while reading the blog, message me on [Instagram](https://www.instagram.com/acanubhav94/) or [LinkedIn](https://www.linkedin.com/in/anubhav-chaturvedi-a7465a72/). For any kind of work related to DevOps, Site Reliability you can contact me at [helpmeanubhav@gmail.com](mailto:helpmeanubhav@gmail.com)

> Special credits to my team members: [**Gaurav Kachariya**](https://www.linkedin.com/in/gaurav-kachariya/) **and** [**Krisha Amlani**](https://www.linkedin.com/in/krisha-amlani/)**.**

> Thank You…
