Skip to content

Instantly share code, notes, and snippets.

@jtsaito
jtsaito / dynamodb_lambda.MD
Last active January 31, 2018 18:33
AWS DyndamoDB and Lambda

This code example briefly describes how to store data to and retrieve data from DynamoDB using two AWS Lambdas. In this example we assume a DynamoDB tale test-can-learn with String key user_uuid in region eu-west-1. As a bonus, we show how to make the read access available as RESTful resource with AWS' API Gateway.

1. Setup Lambdas

Skip Lambda templates and craete the Lambdas custom. Then assign the Basic with DynamoDB role to the Lambda.

2. Lambda writing to DynamoDB

var AWS = require('aws-sdk');
var dynamodb = new AWS.DynamoDB({
  region: 'eu-west-1'
@jtsaito
jtsaito / dynamo_with_ruby_sdk.MD
Last active December 7, 2015 21:23
Put and query Dynamo DB with Ruby's AWS SDK v2

Prerequisites

  1. Created a dynamo db table. We use "travis-ci-example-with-filter" with a composite primary key (string partition key "id", integer range key "created_at").
  2. Made aws-sdk gem available.

Prepare AWS Dynamo DB client and database name

require 'aws-sdk'

Aws.config.update({
@jtsaito
jtsaito / aws_dynmamodb_aws_cli.md
Last active December 2, 2015 08:27
Get DynamoDB item with aws cli

Prerequisites

  1. Created a dynamo db table. We use "travis-ci-deloy-test" with number key "created_at".
  2. Setup aws cli command line tool.
  3. Stored an item with with number key and attribute (in this example, foo: "finger_print").
  4. Set credentials for the table for aws client config (e.g. by setting values in ~/.aws/credentials).

Querying items

First, prepare the query key in a file query_key.json describing the key as json.

@jtsaito
jtsaito / aws_api_gateway.md
Created August 28, 2015 10:36
AWS API Gateway Example

AWS API Gateway allows developers to put a uniform API between your services and clients. A servcie could for instance be your custom app or just an AWS Labmda.

The official walkthrough for making Lambda calls from the AWS Gateway works out of the box. It describes how to make a GET and a POST request and map them to self-defined Lambdas.

When I was testing the walkthrough the example was only slightly outdated as follows. When making stages (something akin to Rails environements for the API Gateway), methods must be registered in a sperate effort as follows.

(1) In the dashboard, select your API.

@jtsaito
jtsaito / aws_api_gateway.md
Created August 28, 2015 10:36
AWS API Gateway Example

AWS API Gateway allows developers to put a uniform API between your services and clients. A servcie could for instance be your custom app or just an AWS Labmda.

The official walkthrough for making Lambda calls from the AWS Gateway works out of the box. It describes how to make a GET and a POST request and map them to self-defined Lambdas.

When I was testing the walkthrough the example was only slightly outdated as follows. When making stages (something akin to Rails environements for the API Gateway), methods must be registered in a sperate effort as follows.

(1) In the dashboard, select your API.

How to get to Alt-Lichtenrade by public transport

There are two ways to get to Alt-Lichtenrade by public transport, by U-Bahn or by S-Bahn.

U6

  • travel by U6 to terminal Alt-Mariendorf
  • hop on bus, either
    • Bus M76 (-> Lichtenrade), get out at Grimmstrasse, then 4 minute walk
    • or Bus X76 (-> Lichtenrade), Goethestr./Potsdamer Str.
@jtsaito
jtsaito / gist:29b34ecd19fef276ca73
Created April 16, 2015 10:49
Fix for race conditions in controllers

The following invalidates the ActiveRecord::QueryCache.

In an initializer for patching ActiveRecord:

ActiveRecord::Base.class_eval do
 
  def self.race_condition_safe(&block)
    begin
 yield
@jtsaito
jtsaito / gist:1d572909a6555ba85855
Last active August 29, 2015 14:13
Note on jQuery's autocomplete plugin

jQuery's Autocompletion plugin comes with jQuery UI. The plugin autocompletes "any field that receives an input." Below is an example from the malt-mate app in coffee script. It fetches the autocomplete values from remote. Moreover, the code does away with the default behaviour of displaying values for labels in the <input type="text"> . (By default, labels are displayed on select but values are displayed on focus.)

<input id="distillery-autocomplete">
<input hidden=true id="distillery-autocomplete-value" type="text" name="distillery-id">
    $("#distillery-autocomplete").autocomplete(
      delay: 500                        # delay between attempts to fetch data
@jtsaito
jtsaito / rails_backport.md
Created January 9, 2015 14:44
Note on backporting an ActiveRecord method from Rails 4.1 to Rails 2.3

This is a simple example for a backport of the ActiveRecord::Persistence#update_columns method which does not exists in Rails 2.3 but in Rails.

Create a ruby file config/iniitilizers/active_record_backport.rb and run a simple class_eval on the extended class, e.g. ActiveRecord::Persistence.

  ActiveRecord::Persistence.class_eval do

    # backport from Rails 4.1.0
    # see https://github.com/rails/rails/blob/4-1-stable/activerecord/lib/active_record/persistence.rb#L271
    def update_columns(attributes)
@jtsaito
jtsaito / gist:6d20c9bea528452168a0
Last active August 29, 2015 14:10
Inserting into an ActiveRecord table without instantiating the model

There are several reasons why one would avoid creating records through ActiveRecord::Base models. One reason is speed for batch inserts, another one is avoiding callbacks or overwritten method calls.

In such cases ActiveRecord::Base#connection.execute or similary methods can be used. They can be combined with Areal for generating SQL code (or other representations) as follows.

Suppose we have an ActiveRecord model User with attributes name (String) and zipcode (integer). The following snippet adds a new record.

insert_manager = Arel::InsertManager.new(ActiveRecord::Base)
table = User.arel_table
arel_attributes = [[table[:name], "John Doe"], [table[:zipcode], "55555"]]