- AWS-Account
- Basic knowledge of either JS, Java or Python
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
-
Creating the Lambda function
- Click the Create lambda function in your console
- Select the hello-world blueprint in the language of your choice (I'm gona be creating mine in nodejs6.10)
- We are not goingt to define the triggers just now
- 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
-
Creating the API Gateway
- Click Create API in your API Gateway Console
- API name = "random-generator"
- In the Actions dropdown click Create Resource
- Resource Name = "number"
- On the newly created number Resource, click the Actions dropdown again and select "Create Method"
- 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"
- In the actions dropdown select Deploy API
- Deployment stage = [New Stage]
- Stage name = prod
-
Body Mapping
- Select the created "GET" Method under "Ressources" on your new API
- Click on "Integration Request"
- Expand "Body Mapping Template"
- "Add Mapping Template"
- Content Type = "application/json"
- Write your template
- Example
{ "_comment": "Takes a link in this form: '...?min=1&max=10'", "min" : "$input.params('min')", "max" : "$input.params('max')" }
- Deploy that shit