Make a chatbot — Manage AWS Servers

Start a simple ChatOps for AWS OpsWorks with Telegram Chatbot

Jeongsoo Park
Sep 10, 2017 · 5 min read
AWS OpsWorks

BotHub.Studio uses OpsWorks which is an AWS flavored Chef for server management. We can add or remove an instance for layers, deploy new codes of Git repository to servers.

When we adopted OpsWorks first, we use it through AWS web dashboard, but it was too cumbersome to log in, navigate along with lots of clicks. So we made a simple CLI script for frequent tasks such like add or remove an instance. But it also has a downside when moving across laptop and desktop, or a colleague wants to access. So we made a chatbot that helps such tasks.

Requirements

  • Store access credentials to request API call to OpsWorks
  • Choose a stack to manage
  • Get layer list
  • Inform instance status in a layer
  • Execute a deployment command

Although more features can be added, I’ll narrow and focus to features above in this article.

Implementation

Slack can be another option if your company already uses it. BotHub.Studio will support Slack soon.

Credentials

On-boarding and get credentials

Credentials consist of access token and secret access token. BotHub.Studio SDK have a useful feature helps construct an intent made of several entities.

Add an intents entry to bothub.yml, which had made when you created a project.

You can add several intent entires under intents entry. An intent key in this file will be used as an argument name in bot.py code. We added an intent with a name credentials. Each intent has a slots, on_complete entry. (and on_complete can be omitted, and set_<intent_name> will be used for its name.) slots is a list of slots which consists of a slot ID and a question to ask.

Now when a user enters a /intent credentials command, chatbot asks a question of each slot step by step and keep its answers. on_complete functions will be executed when got all slot answers. Slot IDs defined at bothub.yml are function arguments name.

A user enters credentials and stores it in user storage with a key credentials.

When a user talks to our chatbot first time, it explains about itself, ask credentials to the user.

Telegram gives chatbot /start message when a conversation begins. So I implemented on_start methodto handle it. on_help method explains about commands supported.

Get a Stack list and choose a Stack

Choose a stack

We will use boto3 library to access OpsWorks. Add boto3 to requirements.txt file which is created in root directory when creating a new project.

bothub
boto3

I added two methods to manage stack and one utility method like below.

get_boto_client method creates and returns boto3 client object using credentials we input before.

/stack command returns a stack list the account has. Get stack list using boto3, put the result into a Message object and send to a messaging channel.

Show each stack as a button to response with ease, run stack select command (/use_stack) when clicked.

/use_stack command stores a selected stack ID to user storage and tells about commands user can use.

Get a list and inspect a Layer

Layer list and instance status

The repeated pattern continues:

/layers command shows layer list, layer name as a button and send /layer <layer_id> when clicking the button. /layer command prints an instance list and status of each instance of the layer.

Deployment

Execute a deployment

As same with before, when a button clicked, the user send a command to the chatbot, chatbot sends a request to OpsWorks using boto3 and show the result with text and buttons.

When a user clicks Deploy button, chatbot shows apps of the stack and the user chooses an app, then chatbot shows deployment command list, the user chooses a command, chatbot starts a deployment.

I used a small trick to use an abbreviation for command argument in on_deploy_app() method to reduce a payload. Telegram limits payload of InlineKeyboardButton to 64bytes maximum.

You can find a complete code at GitHub repository.

Conclusion

We explored a simple usage here, but you can add more useful features for server management like to monitor server status, add an instance, change values for auto-scaling. You can also bind it to AWS CloudWatch to get notified when a situation occurs.

Register to BotHub.Studio and make your chatbot for free now.

BotHub.Studio

News, tutorials about BotHub.Studio chatbot hosting service

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store