Posts tagged AWS

How to use GET Bucket location on Amazon S3 with Racket

:: IT, Racket, AWS

In Racket I want to iterate over my buckets in Amazon S3. They are located in different regions. So how do I get my bucket’s location/region? In the API Reference there is a call GET Bucket location. I use Greg’s AWS library for Racket and this library authenticates its calls with signature version V4. But V4 requires the user to know the region to correctly sign the request. So I need to know the region to ask Amazon S3 for the region where the bucket is located. Otherwise Amazon S3 responds with an error:

<?xml version="1.0" encoding="UTF-8"?>
<Error>
 <Code>AuthorizationHeaderMalformed</Code>
 <Message>The authorization header is malformed; the region 'us-east-1'
is wrong; expecting 'eu-central-1'</Message>
 <Region>eu-central-1</Region>
 <RequestId>XXXX</RequestId>
 <HostId>XXXX>
</Error>

After some search on the net I found a post on Stackoverflow that helped to solve that issue: If I use the URL format (instead of the normally used virtual host format) I could get the location of any bucket. Every region responds with a LocationConstraint answer.

Therefore a code snippet for Racket could be:

(define (get-bucket-location bucket)
  (parameterize
      ([s3-path-requests? #t])
    (define xpr (get/proc (string-append bucket "/?location") read-entity/xexpr))
    (and (list? xpr)
         (= (length xpr) 3)
         (third xpr))))

For example:

> (get-bucket-location "my-bucket-somewhere")
"eu-central-1"

PS: I think official Amazon S3 documentation could be a bit more verbose on the issues with GetBucketLocation and signature V4.

Update: Greg added a bucket-location function to his great library

Running Racket on AWS Lambda

:: IT, Racket, AWS

I started to use AWS for some projects recently. But I only use few of their services. From time to time I look into some of there services and wonder if they are useful for my tasks. I looked into AWS Lambda, "… a compute service that runs your code in response to events and automatically manages the compute resources for you, making it easy to build applications that respond quickly to new information." Nowadays these “lambda functions” could be written in NodeJS or Java. When I was looking for a roadmap of the supported languages I found an interesting blog post by Ruben Fonseca. He explaind how to run Go code on AWS Lambda.

I tried the same with Racket and wrote a short Racket programm test.rkt:

#lang racket/base

(display (format "Hello from Racket, args: ~a~%" (current-command-line-arguments)))

Then I used raco to create a binary test:

raco exe --orig-exe test.rkt

I took the NodeJS wrapper from Ruben’s blog post and put it in a file main.js:

var child_process = require('child_process');

exports.handler = function(event, context) {
  var proc = child_process.spawn('./test', [ JSON.stringify(event) ], { stdio: 'inherit' });

  proc.on('close', function(code) {
    if(code !== 0) {
      return context.done(new Error("Process exited with non-zero status code"));
    }

    context.done(null);
  });
}

Then I put both files in a zip archive, created a new AWS Lambda function, uploaded the zip file and invoked the function:

Invocation of AWS Lambda function

Invocation of AWS Lambda function

Fine!

PS: Only question is: When is AWS Lambda coming to the region eu-central-1, located in Frankfurt?

Upate (2016–03–15): AWS Lambda is now available in the EU (Frankfurt) region!