5 min read

In part 1 of this series, we installed the AWS-CLI tool, created an AWS EC2 instance, listed one, terminated one, and downloaded an AWS S3 content via AWS CLI instead of UI.

Now that we know AWS CLI is very useful and that it supports an extensive API, let’s use it more for daily development and operation works through Slack, which is a very popular chat tool. ChatOps, which is used in the title, means doing operations via a chat tool.

So, before explaining the full process, I assume you are using Slack.

Let’s see how we can control EC2 instances using Slack.

Integrate Slack with Hubot

At first, you need to setup Hubot project with Slack Adapter on your machine. I assume that your machine is MacOSX, but the way to setup is not so different.

# Install redis
$ brew install redis

# Install node
$ brew install node

# Install npm packages
npm install -g hubot coffee-script yo generator-hubot

# Make your project directory
mkdir -p /path/to/hubot
cd /path/to/hubot

# Generate your project basement
yohubot
? Owner: yoheimuta<[email protected]>
? Bot name: hubot
? Description: A simple helpful robot for your Company
? Bot adapter: (campfire) slack
? Bot adapter: slack

Then, you have to register an integration with Hubot on a Slack configuration page. This page issues an API Token that is necessary to work the integration.

# Run the hubot
$ HUBOT_SLACK_TOKEN=YOUR-API-TOKEN ./bin/hubot --adapter slack

If all works as expected, Slack should respond PONG when you type hubot-name ping in Slack UI.

Install hubot-aws module

Ok, you are ready to introduce hubot-aws which makes Slack to be your team’s AWS CLI environment.

# Install and add hubot-aws to your package.json file:
$ npm install --save hubot-aws

# Addhubot-aws to your external-scripts.json:
$ vi external-scripts.json

# Set an AWS credential if your machine has no ~./aws/credentials
$ export HUBOT_AWS_ACCESS_KEY_ID="ACCESS_KEY"
$ export HUBOT_AWS_SECRET_ACCESS_KEY="SECRET_ACCESS_KEY"

# Set an AWS region no matter whether your machine has ~/.aws/config or not
$ export HUBOT_AWS_REGION="us-west-2"

# Set a DEBUG flag until you use in production
$ export HUBOT_AWS_DEBUG="1"

Run an EC2 instance

We are going to run an EC2 instance, which is provisioned mongodb, nodejs and Let’s Chat — Self-hosted chat for small teams app. In order to run it, we first need to create config files.

$ mkdiraws_config
$ cdaws_config/

# Prepare option parameters
$ viapp.cson
$ catapp.cson
MinCount: 1

MaxCount: 1

ImageId: "ami-936d9d93"

KeyName: "my-key"

InstanceType: "t2.micro"

Placement:
    AvailabilityZone: "us-west-2" 

NetworkInterfaces: [
    {
      Groups: [
        "sg-***"
      ]
SubnetId                 : "subnet-***"
DeviceIndex              : 0
AssociatePublicIpAddress : true
    }
]
# Prepare a provisioning shell script
$ viinitfile
$ catinitfile
#!/bin/bash

# Install mongodb
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
sudo echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen" | sudo tee -a /etc/apt/sources.list.d/10gen.list

# Install puppet
sudowget -P /tmp https://apt.puppetlabs.com/puppetlabs-release-precise.deb
sudodpkg -i /tmp/puppetlabs-release-precise.deb
sudo apt-get -y update
sudo apt-get -y install puppet 2>&1 | tee /tmp/initfile.log

# Install a puppet module
sudo puppet module install jay-letschat 2>&1 | tee /tmp/initfile1.log

# Create a puppet manifest
sudosh -c "cat >> /etc/puppet/manifests/letschat.pp<<'EOF';
class { 'letschat::db':
user          => 'lcadmin',
pass          => 'unsafepassword',
bind_ip       => '0.0.0.0',
database_name => 'letschat',
database_port => '27017',
} ->
class { 'letschat::app':
dbuser          => 'lcadmin',
dbpass          => 'unsafepassword',
dbname          => 'letschat',
dbhost          => 'localhost',
dbport          => '27017',
deploy_dir      => '/etc/letschat',
http_enabled    => true,
lc_bind_address => '0.0.0.0',
http_port       => '5000',
ssl_enabled     => false,
cookie          => 'secret',
authproviders   => 'local',
registration    => true,
}
EOF"

# Apply a puppet manifest
sudo puppet apply /etc/puppet/manifests/letschat.pp 2>&1 | tee /tmp/initfile2.log
$ export HUBOT_AWS_EC2_RUN_CONFIG="aws_config/app.cson"
$ export HUBOT_AWS_EC2_RUN_USERDATA_PATH="aws_config/initfile"
$ HUBOT_SLACK_TOKEN=YOUR-API-TOKEN ./bin/hubot --adapter slack

Let’s type hubot ec2 run –dry-run to validate the config and then hubot ec2 run to start running an EC2 instance.

Enter public-ipaddr:5000 into a browser to enjoy Let’s Chat app after initialization of the instance (maybe it takes about 5 minutes). You will find public-ipaddr from AWS UI or hubot ec2 ls –instance_id=*** (this command is described below).

List running EC2 instances

You now have created an EC2 instance. The detail of running EC2 instances is displayed whenever your colleague and you type hubot ec2 ls. It’s cool.

Terminate an EC2 instance

Well, it’s time to terminate an EC2 instance to save money. Type hubot ec2 terminate –instance_id=***. That’s all.

Conclusion

ChatOps is very useful, especially for your team’s routine ops work. For our team’s example, we ran a temporal app instance to test a development feature before deploying it in production, terminating a problematic EC2 instance logging errors, and creating settings about Auto Scaling that is relatively difficult to automate completely yet like using Terraform for our team. Hubot-AWS works for dev&ops engineers who want to use AWS CLI while sharing ops with their colleagues but have no time to completely automate.

About the author

YoheiYoshimuta is a software engineer with a proven record of delivering high quality software in both game and advertising industries. He has extensive experience in building products from scratch in small and large team. His primary focuses are Perl, Go and AWS technologies. You can reach him at @yoheimutaonGitHubandTwitter.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here