Skip to content

Instantly share code, notes, and snippets.

@fabianbaechli
Last active May 18, 2017 07:09
Show Gist options
  • Save fabianbaechli/825356cf43ea5f78747d0b10c1cdcc71 to your computer and use it in GitHub Desktop.
Save fabianbaechli/825356cf43ea5f78747d0b10c1cdcc71 to your computer and use it in GitHub Desktop.

Setting up an API

Prerequisites

  • AWS-Account
  • Basic knowledge of either JS, Java or Python

How to

We are going to create a program which takes a min and max value and generates a random value in the range resulting from these two parameters

  1. Creating the Lambda function

    1. Click the Create lambda function in your console
    2. Select the hello-world blueprint in the language of your choice (I'm gona be creating mine in nodejs6.10)
    3. We are not goingt to define the triggers just now
    4. Configure function
      • Name = "random number generator"
      • Runtime = Node . js 6.10
      • Code (Written in JS)
        'use strict';
        
        console.log('Loading function');
        
        exports.handler = (event, context, callback) => {
            // Minimum value of the random number
            let min = event.min;
            // Maximum value of the random number
            let max = event.max;
            // Calculating the number
            let generatedNumber = Math.floor((Math.random() * max) + min);
        
            callback(/*error message*/null, generatedNumber);
        };
        
      • Role = Create new role from template(s)
      • Role name = "basic-lambda-execute-role"
      • Policy templates: Simple Microservice permissions
  2. Creating the API Gateway

    1. Click Create API in your API Gateway Console
    2. API name = "random-generator"
    3. In the Actions dropdown click Create Resource
      • Resource Name = "number"
    4. On the newly created number Resource, click the Actions dropdown again and select "Create Method"
    5. Set a GET method
      • Integration type = Lambda Function
      • Lambda Region = value of parameter "region" in link (.../home?region=us-east-1)
      • Lambda function = "random-number-generator"
    6. In the actions dropdown select Deploy API
      • Deployment stage = [New Stage]
      • Stage name = prod
  3. Body Mapping

    1. Select the created "GET" Method under "Ressources" on your new API
    2. Click on "Integration Request"
    3. Expand "Body Mapping Template"
    4. "Add Mapping Template"
      • Content Type = "application/json"
    5. Write your template
      • Example
      {
      "_comment": "Takes a link in this form: '...?min=1&max=10'",
      "min" : "$input.params('min')",
      "max" : "$input.params('max')"
      }
      
    6. Deploy that shit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment