This is a basic demo guide on how to get started with AWS Lambda using Node. Last update: June 7, 2019
- Visit the AWS Lambda console.
- Select Create Function
- Give your function a name
- Create a role
- i. Create new role from template(s)
- ii. Provide a role name
- iii. From Policy Templates select the Simple Microservice permissions
- Create function! Huzzah!
You've now created an AWS Lambda function and should be looking at the details of your new function.
- In the upper-right corner select Test
- Give your test/event a name
- Create
- NOTE: You can change the data in the JSON now or later, the default data is placed their for convenience.
- Run the Test!
- NOTE: Make sure to expand the Details section of the green output window at the top of the page to see the response data.
- NOTE: Your console logs are under Log output of the Details section
- Copy the code from main.js - Example One - into the editor.
- NOTE: This code will replace ALL of the code currently in the editor that was provided by default.
- NOTE: REMEMBER TO HIT SAVE!
- Run the Test you created previously.
At this point your response body should be exactly the same as the JSON data in your test. Why?
The event object holds the data passed into our function, and the callback is passing the event object into the JSON response returned by our function.
- In the Designer under Add Trigger click on API Gateway.
- RANT: To me their interface looks as though you should be able to drag and drop, but you cannot. So, if you are like me, it is important to emphasis that you need to click the Trigger, not drag and drop.
- Scroll down to the Configure trigger section.
- Select Create a new API.
- Security select Open.
- NOTE: This means if someone got the link to your API they could pass data in, and get whatever data is coming out. So, be careful!
- Expand Additional Settings and change the name and/or deployment stage if you'd like, but not needed.
- Click Add then click SAVE! Super easier to forget to save (speaking from experience).
- There should now be a link at the bottom of your screen, visit it!
Oh...internal server error. Why do you think we are getting this error?
I know! We are not responding with a proper HTTP response! Now that we've attached an API Gateway trigger to our Lambda function we need to send a proper HTTP response with a status, body, headers, etc.!
Jump back into the main.js file and grab the code under EXAMPLE TWO. Replace ALL of the code in your Lambda editor with the example code.
Alright, try that link again!
You've now deployed an AWS Lambda function as an API endpoint using AWS API Gateway.
Paste your API Gateway url into main.js of this project. Fire up live-server and your Lambda response should now appear in your browser.
Check out this tutorial to learn more about S3 triggers for Lambda
Check out this tutorial to learn more about DynamoDB triggers for Lambda.
Check out this tutorial to learn more about creating log files of actions taken against your Lambda function using CloudTrail.