Skip to content

Instantly share code, notes, and snippets.

@baodinh
Created January 30, 2017 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baodinh/9ffa7f7fbcf7e89db36dfecc3a29c229 to your computer and use it in GitHub Desktop.
Save baodinh/9ffa7f7fbcf7e89db36dfecc3a29c229 to your computer and use it in GitHub Desktop.
<p>This topic describes how to Create AWS Lambda Node.js function that gets all images from S3, and deploy it into AWS API Gateway.</p>
<p>Scenario: </p>
<p>In S3 has the object contains the images arranged by movieid(folder) like:</p>
<div class="text-center"><img src="https://s3-us-west-2.amazonaws.com/baodinh.blog/gateway/15.png"/></div>
<p>I want to create api to access all of the images by movieid. So the result api will be like this:</p>
<pre>https://z5d4qclzwd.execute-api.usest2.amazonaws.com/movie/getimages?id=207932 </pre>
<div class="text-center"><img src="https://s3-us-west-2.amazonaws.com/baodinh.blog/gateway/1.png"/></div>
<p>This documentation guides you through our simple Node.js Lambda example function in detail.</p>
<p>Note: Strongly suggests you familiarize yourself with <a href="http://docs.aws.amazon.com/lambda/latest/dg/programming-model.html">Programming Model (Node.js)</a> in the AWS Lambda Developer Guide before continuing.</p>
<p>Example content:</p>
<p>1. Create NodeJS project to get data from S3:</p>
<p>a. Using the command to create node project:</p>
<pre>Npm init && npm install aws-sdk --save</pre>
<p>b. Add index.js: </p>
<script src="https://gist.github.com/baodinh/56fa4bf64a7cd38bdbaab9c298fcf6fb.js"></script>
<p>2. Upload lambda function into AWS Lambda</p>
<p>From AWS console -> AWS Lambda service -> Create a lambda function -> Select blueprint (Blank function) -> Next :</p>
<p>Zip your node project and upload package with .zip format:</p>
<div class="text-center"><img src="https://s3-us-west-2.amazonaws.com/baodinh.blog/gateway/2.png"/></div>
<p>Next -> Create Function. Now, we already have lambda function to use in AWS API Gateway. </p>
<p>3. Create AWS API Gateway to consume lambda function and deploy API:</p>
<p>AWS API Gateway -> Create API</p>
<div class="text-center"><img src="https://s3-us-west-2.amazonaws.com/baodinh.blog/gateway/3.png"/></div>
<p>Click Action -> Create Resource </p>
<div class="text-center"><img src="https://s3-us-west-2.amazonaws.com/baodinh.blog/gateway/4.png"/></div>
<p>Click Action -> Create Method -> Choose GET:</p>
<div class="text-center"><img src="https://s3-us-west-2.amazonaws.com/baodinh.blog/gateway/5.png"/></div>
<p>Next step is config the parameter for api:</p>
<div class="text-center"><img src="https://s3-us-west-2.amazonaws.com/baodinh.blog/gateway/6.png"/></div>
<p>Click Method Request -> In URL Query String Parameters -> Add query string:</p>
<div class="text-center"><img src="https://s3-us-west-2.amazonaws.com/baodinh.blog/gateway/7.png"/></div>
<p>Come back main screen Click Integration Request to set up body mapping for lambda function. In Body Mapping Templates -> Click Add mapping template:</p>
<div class="text-center"><img src="https://s3-us-west-2.amazonaws.com/baodinh.blog/gateway/8.png"/></div>
<p>Now it is the time for testing. Come back main screen and click Test and field the id of movie (folder), click Test. It will be like this if successful:</p>
<div class="text-center"><img src="https://s3-us-west-2.amazonaws.com/baodinh.blog/gateway/9.png"/></div>
<p>4. Deploy API to get URL for using in application:</p>
<p>Click on resource /getimages -> click Action -> Deploy API</p>
<div class="text-center"><img src="https://s3-us-west-2.amazonaws.com/baodinh.blog/gateway/10.png"/></div>
<p>After deploy successfully , it will show you url :</p>
<div class="text-center"><img src="https://s3-us-west-2.amazonaws.com/baodinh.blog/gateway/11.png"/></div>
<p>And this is the url you use in your app:</p>
<p>https://d2rvtmd8u4.execute-api.us-west-2.amazonaws.com/testapi/getimages?id=207932</p>
<p>Troubleshooting:</p>
<p>Here are some things I met:</p>
<p>1. When upload .zip file and test in AWS Lambda. </p>
<div class="text-center"><img src="https://s3-us-west-2.amazonaws.com/baodinh.blog/gateway/12.png"/></div>
<p>Solved: go inside the node folder and click all of files and zip. Don"t zip the folder. I don"t know why it happens.</p>
<p>2. It happens when using in MAC OS. </p>
<div class="text-center"><img src="https://s3-us-west-2.amazonaws.com/baodinh.blog/gateway/13.png"/></div>
<p>Solved: Right click folder and grant permission for it.</p>
<p>3. When test lambda function : </p>
<p>"errorMessage": "2016-12-25T19:53:55.373Z dd28198d-cadb-11e6-98b7-ffd818f37b47 Task timed out after 1.00 seconds"</p>
<p>Try to increase Timeout in AWS Lambda -> Configuration -> Advanced Settings :</p>
<div class="text-center"><img src="https://s3-us-west-2.amazonaws.com/baodinh.blog/gateway/14.png"/></div>
<p>These are the steps to create simple API by AWS Lambda, S3, AWS API Gateway. It also has many configurations inside like: monitoring, permission….</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment