AWS lambda: the basics

For a API Gate / Lambda combo, there is a bit of a gotch when setting those two services together and following their hello world example.

Instead of the default in their example:

# print(“value2 = ” + event[‘key2’])



I wish it was more evident in their documentation what “event” means, but basically, after you set the above, you need to also set the query params (spell out what they will be) under: Amazon API Gateway / Resources / Method execution

Also, in that section, Integration Request, specify:

When there are no templates defined (recommended)”

and add a new template for: “application/json”

In the “Generate Template” section, choose: “Method Request Pass through”

Leave the default code in there, and now, when you pass your parameters as:


you will see those values in your python script as:


Docker: the basics

Dockerfile: series of commands used to create an image. Below is an explanation of some of the basic commands you can use inside:

# usually a operative system, where we are going to build the docker image:

FROM some_img_name

MAINTAINER your name <>

# any commands you need to run as part of your image build

RUN apt-get update

RUN some other command

# notice how we are passing -y to avoid the y/n question at install time:

RUN apt-get install -y some_package

# example of creating a config file via echo, also, this command will make your docker image available to any external connections:

RUN echo “bind_ip =” >> /etc/mongodb.conf

# Including files from our local host into the image:

ADD some_local_file_path some_path_inside_docker_img

# important! this is how you expose ports from inside the image:

EXPOSE 27017

# this command run after the image start

CMD some_command_here

Once your Dockerfile is ready, you are ready to build your image:

docker build -t your_docker_namespace/some_tag:latest .

The . indicates you want to use the local folder to run your build. This will execute each command in that Dockerfile at build time.

Once you build successfully, you are ready to push to the docker hub repo, so you can download and use this new image from anywhere:

docker tag your_new_img_tag your_dockerhub_namespace/your_img_name

docker push your_new_img_tag your_dockerhub_namespace/your_img_name


# pulling images from the docker hub to your local env:

docker pull postgres:latest

you will notice how several “things” are downloaded. This is because images are comprised of several sets of layers, some of those shareable between images. The idea is to be able to cache and reuse better.

By default, you are pulling from the dockerhub repo.

# running docker images:

docker run docker_img_name /path/to/command command_args


docker run –name dockerimgname -it -v /src:/somedirinsideimg/src -p 9000:9000



# running the ubuntu image locally, and then interact with it (-it) by opening a bash session to it:

docker run -it ubuntu /bin/bash

# exposing ports in a running docker container:

docker run -d -p 8000:80 –name some_name atbaker/nginx-example

Notes: option -d is so we run in detach mode (in the background). For the ports, it takes port 80 in the docker container, and makes it accessible in port 8000 in the host machine. The –name option is to avoid the default name docker gives to the running images (you can pass any string to it). To get the actual ip address you need to hit on your machine (in the browser, for example), you need to run:

docker-machine ls

So the actual url you will be looking at (for the example above) would be something like:

# tailing logs on a running docker container:

docker logs -f some_name

# see what has changed on a docker container since we started it:

docker diff some_name

# check the history of commands run to produce a docker image:

docker history docker_img_name

# inspect low level information about our container:

docker inspect some_name

# get the top command applied to our docker image:

docker stats some_name

# remove all docker running images:

docker rm –force `docker ps -qa`

# creating new docker images:

pull and run a base docker image as instructed above, and then go ahead and go inside the image:

docker run -it image_name_here bash

inside the image, do whatever modifications you need to do for the base image, then you can commit your changes as follows:

docker commit -m “Some description of the changes here” docker_id_here docker_tag_here

the docker tag at the end is just any descriptor of your new image version. To push the changes to dockerhub you need to login first, and then push:

docker login

docker tag docker_tag_here your_dockerhub_namespace/name_of_docker_repository

docker push your_dockerhub_namespace/name_of_docker_repository

Mounting external volumes inside docker images

-v [hostpath]:[containerpath]


docker run \
    -ti -v `pwd`/testdir:/root \
    ubuntu /bin/bash

we are running an image, and attaching whatever folder we are at the moment (via pwd), plus /testdir, to go inside the root folder in the docker image

so whatever files we create inside that image, they will also be created in the root directory.

Example of how to persist data between stops and starts of a docker image:

docker run -it --name somedockerimgname -v /root ubuntu /bin/bash

so now, when you stop and restart somedockerimgname, the files you created inside the /root folder will still be there. Destroying the container will still remove the data though!

Differences between docker and Vagrant

Vagrant is meant to spawn and manage entire Virtual Machines. Docker is more a series of files and executables packed in the image, so when programs run, they are directed to that set of files. When initialized, we are not booting a full fledge VM, just the set of files needed to run as one.

Docker’s goal is to run the fewest services per image, so you may need multiple to run your app.

The advantage of docker is that it gives you more flexibility, as you can swap services as modules easier. Also, it require less resources than running full blown Virtual Machines.

Docker also has its own internal network service. You can control the ports that the outside world uses to communicate with your image.




Rails 4.1: deploying to Heroku

download the toolbelt kit:

$ wget -O- | sh


$ heroku login

create your app:

$ heroku apps:create [your app name]

or, if you already have an app in Heroku:

heroku git:remote -a ticketee-demo

Add the rails_12factor gem, to take care of the standard points a web app should cover. In your gemfile:

gem “rails_12factor”, group: :production

Also, in the same gemfile, to make sure you are using the same Ruby version as in development, add the following line (right under the “source” first line):

source ''
ruby "2.2.1"

Also, to specify a better production server (than the redbrick default), add the following line at the bottom of your gemfile:

gem "puma", group: :production

If you are going to use the Puma server in production, in Heroku, you will also need to add a Procfile in your root folder, with the following content:

web: bundle exec puma -t 5:5 -p ${PORT:-3000}
  -e ${RACK_ENV:-development}

Then, just rebuild your gemfile.lock by running:

bundle install --without=production

And finally, to push to Heroku, checking your code, and push it:

$ git add .
$ git commit -am "setup heroku ruby version and server"
$ git push heroku master

Your code is now there, but you will also need to get the database ready and run all the needed migrations:

heroku run rake db:migrate

If you have any seed files for your db, this is also a good time to run it:

heroku run rake db:seed

If anything goes wrong, you can always see what is happening in the logs:

heroku logs

rails 4.1: styling an app with bootstrap and font-awesome

Install the bootstrap gem:

gem "bootstrap-sass", "~> 3.3"
gem "font-awesome-rails", "~> 4.3"

Rename app/assets/stylesheets/application.css to application.css.scss, so you an add @import statements to it

Replace the entire content of the file with:

@import "bootstrap-sprockets";
@import "bootstrap";
@import "font-awesome";

Once you do this, you will need to also import the particular stylesheets your app is compose of, example (importing projects.scss:

@import "projects";

At this point you have the bootstrap css already applied to your pages. It is recommended you apply at least this div wrapper to your app/views/layouts/application.html.erb file:


  <div class="container">
  <% flash.each do |key, message| %>
    <div><%= message %></div>
  <% end %>

  <%= yield %>


An example of a styled link with bootstrap:

<%= link_to “New Project”, new_project_path, class: “new” %>

And the CSS (inside application.css.scss): {
  @extend .btn;
  @extend .btn-success;

  &:before {
    font-family: "FontAwesome";
    @extend .fa-plus;
    padding-right: 0.5em;

This is an example on how to create a decent looking action buttons:

<ul class="actions">
  <li><%= link_to "Edit Project", edit_project_path(@project),
    class: "edit" %></li>

  <li><%= link_to "Delete Project", project_path(@project),
     method: :delete,
     data: { confirm: "Are you sure you want to delete this project?" },
     class: "delete" %></li>

And the associated css:

a.edit {
  @extend .btn;
  @extend .btn-primary;

  &:before {
    font-family: "FontAwesome";
    @extend .fa-pencil;
    padding-right: 0.5em;

a.delete {
  @extend .btn;
  @extend .btn-danger;

  &:before {
    font-family: "FontAwesome";
    @extend .fa-trash;
    padding-right: 0.5em;

This is an example of how to style your alert messages (in application.html.erb):

<% flash.each do |key, message| %>
  <div class="alert alert-<%= key %>">
    <%= message %>
<% end %>

And the CSS associated with it:

.alert-notice {
  @extend .alert-success;

.alert-alert {
  @extend .alert-danger;

Example of how to add a top nav bar:

<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <%= link_to "Ticketee", root_path, class: "navbar-brand" %>
      <button type="button" class="navbar-toggle collapsed"
        data-toggle="collapse" data-target="#collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>

    <div class="collapse navbar-collapse" id="collapse">
      <ul class="nav navbar-nav">
        <li class="<%= "active" if current_page?("/") %>">
          <%= link_to "Home", root_path %>

And the CSS associated with it:

body {
  padding-top: 70px;

If you end up using the nav bar code above, you are going to need a bit of javascript to make the menu work on mobile platforms (otherwise the menu won’t open). Make sure to include this line in your app/assets/javascripts/application.js file:

//= require bootstrap-sprockets

Making your app responsive

Include the following inside your application.html.erb <head> tags:

<meta name="viewport" content="width=device-width, initial-scale=1">

Also, create a dedicated stylesheet file to include your responsive styles (assets/stylesheets/responsive.scss), and import it:

@import "responsive";

Inside there, you can place all your scss related to media queries:

@media(max-width: 500px) {
  p { color: red }

rails 4.1 helpers

helpers are methods inside your app/helpers folder, and they are available to your views usage. Every controller gets one (if the controller gets created via a generator).

Example of an application helper:

module ApplicationHelper
  def title(*parts)
    unless parts.empty?
      content_for :title do
        (parts << "Ticketee").join(" - ")

The * means any argument passed will be available as an array

This particular helper can be called via this example code (in your application.html.erb template):

  <% if content_for?(:title) %>
    <%= yield(:title) %>
  <% else %>
  <% end %>

And, the particular pages or sub-templates that call this:

<% title(, "Projects") %>

rails 4.1: creating a CRUD resource feature

Create the test first, inside your specs/feature/ folder:

Sample content for that file:

require "rails_helper"

RSpec.feature "Users can create a project" do
  scenario "with valid attributes" do
    visit "/"

    click_link "New Project"

    fill_in "Name", with: "Some Project name"
    fill_in "Description", with: "Some project description"
    click_button "Create"

    expect(page).to have_content "Project has been created."

The test will fail when you run:

bundle exec rspec

If you want to run only one:

bundle exec rspec spec/features/creating_projects_spec.rb

You need to create the db in order for them to pass

bundle exec rake db:create

To make the test pass, you need to build a route from the “/” request

Inside config/routes.rb, add the following line:

root "projects#index"

Create your controller (use plurals! singular names are for models)

rails g controller projects

And, your models (by default, the columns will be string)

rails g model project name description
bundle exec rake db:migrate

Now, inside your src/app/controllers/projects_controller.rb, create your controller methods:

class ProjectsController < ApplicationController
  def index

  def new
    @project =

  def create
    @project =
      flash[:notice] = "Project has been created."
      redirect_to @project
    else[:alert] = "Project has not been created."
      render "new"
  def show
    @project = Project.find(params[:id])

  def edit
     @project = Project.find(params[:id])

  def update
     @project = Project.find(params[:id])
     if @project.update(project_params)
      flash[:notice] = "Project has been updated."
      redirect_to @project
     else[:alert] = "Project has not been updated."
      render "edit"

  def destroy
     @project = Project.find(params[:id])
     flash[:notice] = "Project has been deleted."
     redirect_to projects_path

  def project_params

Also, build your view templates:




Example of the way you display model information inside any of the ERBs above:

<h1><%= %></h1>
<p><%= @project.description %></p>

Create the form, usually inside a partial, so you can reuse the code for Create and Edit views (inside app/views/projects/_form.html.erb):

<%= form_for(project) do |f| %>
  <% if project.errors.any? %>
   <div id="error_explanation">
     <h2><%= pluralize(project.errors.count, "error") %>
     prohibited this project from being saved:</h2>

       <% project.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
       <% end %>
  <% end %>

    <%= f.label :name %><br>
    <%= f.text_field :name %>

    <%= f.label :description %><br>
    <%= f.text_field :description %>

  <%= f.submit %>
<% end %>

Inside your view, since you were looking for a link in this particular test, you can put the following content to make the test pass:

<%= link_to “New Project”, new_project_path %>

But now we need to define a route for that link (inside your config/routes.rb file:

resources :projects

By adding that resource, you are basically accessing seven potential actions on your page:


If you want Flash messages to display in the page, you need to include this on your views/layout/application.hml.erb, or in the page where you need them:

<% flash.each do |key, message| %>
  <div><%= message %></div>
<% end %>

Basic validation

Add the following to your model:

class Project < ActiveRecord::Base
  validates :name, presence: true

Index page

The rspec (spec/features/viewing_projects_spec.rb) will look like this:

require "rails_helper"

RSpec.feature "Users can view projects" do
  scenario "with the project details" do
    project = FactoryGirl.create(:project, name: "Sublime Text 3")

    visit "/"
    click_link "Sublime Text 3"
    expect(page.current_url).to eq project_url(project)

Before you run you new test, make sure you add the project factory, inside spec/factories/project_factory.rb:

FactoryGirl.define do
  factory :project do
    name "Example project"

A typical index.html.erb view page will look like this:


<%= link_to “New Project”, new_project_path %>

<div id=“projects”>

  <% @projects.each do |project| %>

    <h2><%= link_to, project %></h2>

    <p><%= project.description %></p>

  <% end %>


Updating section

Add the following test in spec/features/editing_projects_spec.rb

require "rails_helper"

RSpec.feature "Users can edit existing projects" do
  before do
    FactoryGirl.create(:project, name: "Sublime Text 3")

    visit "/"
    click_link "Sublime Text 3"
    click_link "Edit Project"

  scenario "with valid attributes" do
    fill_in "Name", with: "Sublime Text 4 beta"
    click_button "Update Project"

    expect(page).to have_content "Project has been updated."
    expect(page).to have_content "Sublime Text 4 beta"
  scenario "when providing invalid attributes" do
    fill_in "Name", with: ""
    click_button "Update Project"
    expect(page).to have_content "Project has not been updated."

Then, add a link to the edit page on your show.html.erb page:
<%= link_to “Edit Project”, edit_project_path(@project) %>

And the actual template (app/views/projects/edit.html.erb):

<h1>Edit Project</h1>
<%= render "form", project: @project %>

Delete section

Create your test at: spec/features/deleting_projects_spec.rb

require "rails_helper"

RSpec.feature "Users can delete projects" do
  scenario "successfully" do
    FactoryGirl.create(:project, name: "Sublime Text 3")

    visit "/"
    click_link "Sublime Text 3"
    click_link "Delete Project"

    expect(page).to have_content "Project has been deleted."
    expect(page.current_url).to eq projects_url
    expect(page).to have_no_content "Sublime Text 3"

In your show.html.erb template, you need to add the delete action:

<%= link_to "Delete Project",
    method: :delete,
    data: { confirm: "Are you sure you want to delete this project?" }

rails projects workflow

setup your rails vagrant env and add git, by following the instructions in this page

setup your IDE to be connected to your vagrant instance (inside Vagrantfile):

config.vm.synced_folder "./src", "/home/vagrant/your_project"

setup the initial rails app:

rails new myapp

setup the bitbucket repo

config and initialize your git repo:

git config --global "your name"
git config --global
git init
git add .
git commit -am "initial commit"
git remote add origin
git push origin master -u 

setup your testing env:

put the following gems in your Gemfile, in the development:, test: section:

gem "rspec-rails", "~> 3.2.1"

also, add capybara and factory girl:

group :test do
  gem "capybara", "~> 2.4"
  gem "factory_girl_rails", "~>4.5"

Update your gems with the additions by running:

bundle update

And install rspec afterwards:

rails g rspec:install

If you get an error message about uglifier being missing, you will have to install nodejs on your system, and run the command again afterwards (there is a js dependency on nodejs)

sudo apt-get install nodejs

make sure you commit your changes at this point:

git add .
git commit -am "setup tests"
git push

database configuration

Install postgresql in your vagrant box:

sudo apt-get update
sudo apt-get install postgresql postgresql-contrib
sudo apt-get install libpq-dev
sudo -i -u postgres psql ALTER USER postgres WITH PASSWORD 'somepassword'

Modify your Gemfile to include the pg gem:

gem ‘pg’

bundle install

Change your config/database.yml file from sqlite3 to postgresql:

default: &default

  adapter: postgresql

  pool: 5

  timeout: 5000


  <<: *default

  database: ticketee_development

  username: vagrant

Also, make sure the test database gets a different name from the development one:

< database: ticketee_test

username: vagrant

Get the vagrant user to be able to run the rails app:

sudo su - postgres
CREATE ROLE vagrant superuser;
alter ROLE vagrant WITH LOGIN;

make sure you tests are running:

setup an rspec test (see the beginning of this post for details)

bundle exec rake db:create
bundle exec rspec

basic SPA (single page app) boilerplate for fast prototyping in angular

Creating this inside a Vagrant image (meaning there are extra steps to install the needed basic packages):

Creating the Vagrant workspace:

vagrant init ubuntu/trusty64;
vagrant up; vagrant ssh;
vi Vangrant # and then port forward 8080 to 8080 on the host

Creating the boilerplate:

mkdir myapp; cd myapp;
sudo apt-get update;
sudo apt-get install git;
sudo apt-get install npm;
sudo ln -s /usr/bin/nodejs /usr/bin/node;
npm init;
sudo npm install -g bower;
bower init;
touch main.js
touch index.html
git init
vi .gitignore
# include the following inside your .gitignore file:
# back to the command line:
bower install --save angular
bower install --save ui-router

HTML / JS basic bootstrap


angular.module(“MyAppName”, [‘ui-router’])

.config(function ($stateProvider, $urlRouterProvider) {



My App Title
<body ng-app=MyAppName>
<h1>My App</h1>
<div ui-view></div>
<script src=bower_components/angular/angular.js></script>
<script src=bower_components/angular-ui-router/release/angular-ui-router.js></script>
<script src=main.js></script>

Quickly testing the page without setting up apache:

sudo npm install -g http-server