Make a chatbot — Manage AWS Servers

Start a simple ChatOps for AWS OpsWorks with Telegram Chatbot

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

Let me see what the chatbot should let users do:

  • 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

I’m going to use Telegram in this article. Please read an article below to know how to create a new Telegram chatbot, install an SDK and CLI tool of BotHub.Studio.

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

Credentials

First of all, our chatbot needs an access credential enables API request to OpsWorks.

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

Now, let’s get OpsWorks stack list an account has. Select one stack among them, then it will be used to get layer list, to deploy new codes to instances.

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

Now, let’s show a layer list for a stack and instance list, status for each 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

Now we will send a deployment command to a stack. We cover only deploy and update_custom_cookbooks commands here among Opsworks commands.

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

Chatbot in the aspect of ChatOps is similar to a CLI tool based on terminal but different because you can access it from anywhere you can use a messenger.

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.