Let's stay in contact

Welcome! and thank you for visiting my little home on the web. I would love to tell you more about my journeys in tech and running. I mostly write about what I learn from running long distances and building large scale web applications. I also enjoy reading your email responses.

Thank you, Chris

Click Here to Join Me

Lambda Function to return a 1x1 Pixel. It took me a while to figure out how to return binary data from Lambda through the AWS API Gateway. Here’s my Lambda function for returning a GIF image pixel.

Lambda Function to return 1x1 pixel

'use strict';
exports.handler = function(event, context) {

  var imageHex = "\x42\x4d\x3c\x00\x00\x00\x00\x00\x00\x00\x36\x00\x00\x00\x28\x00"+ 
  "\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x18\x00\x00\x00"+ 
  "\x00\x00\x06\x00\x00\x00\x27\x00\x00\x00\x27\x00\x00\x00\x00\x00"+
  "\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\x00\x00"; 
  context.done(null, { "body":imageHex });

};

Then in the Method Response for the API Gateway I added Content-Type to the Response Header.

Then in the Integration Response I map Content-Type to the constant string, ‘image/gif’ (single quotes). In the Integration Response -> Body Mapping Templates I added a template for image/gif.

image/gif Body Mapping Template

#set($inputRoot = $input.path('$'))
$inputRoot.body

Now I can call the pixel from my webapges.

<img src="https://api-gateway-url/stage/pixel.gif" />

Chris Larson

I fell in love with running after I committed to running a 5k everyday during the month of September. Now I'm training for my first Marathon on October 1st, 2017.

chrylarson chrylarson


Published