Getting Started with AWS Lambda REST Services Part 1 of 3
This blog post will outline how to configure AWS Lambda functions to synchronously respond to HTTP requests through the API Gateway. Both Lambda and API Gateway are two very hot technologies that allow for serverless infrastructure. Lambda is an Amazon web service that allows you to define a series of functions in languages such as Node.js, Java, and Python. A developer can focus on writing functions for the business logic of the task at hand instead of thinking about application containers, frameworks, and other orchestration tools that are needed for full applications. AWS bills for each execution of the function by memory usage, length of time, and bandwidth.
The API Gateway from AWS allows developers to create web service apis that are capable of integrating with other web services or synchronously calling Lambda functions. The API Gateway taps into IAM so that developers can leverage granular security policies to secure the services.
Part 1 of this post will define how to configure the Lambda functions. Part 2 will define how to setup the API Gateway. Part 3 will describe how to configure security policies in IAM and deploy the API.
Configure the getContacts Lambda Functions
Go to the AWS Lambda console and select Create a Lambda Function. If this is your first time creating a function, select Get Started. AWS provides blueprints to help with defining basic project configuration. Search in the filter for
hello-world blueprint defines a basic Node Lambda function.
Set the name of the function to
getContacts, add a meaningful description, and set the runtime to Node.js 4.3.
Add the following code for this function. This function will receive an event object and context about the event.
The advanced setting should not require modification but should have the following values.
Select Next and confirm the function configuration.
Now try testing your new Lambda getContacts function. Select Test and set an empty json object as the payload for the event.
Save and test.
The response from the function call should be.
The resulting log output should be as follows.
Now test with an id set on the request object. Select Actions->Configure test event
The response should return one object with the id you specified.
Configure the addContact Lambda Function
Now create a second Lambda function called
addContact with the following code. All of the configuration should be the same as the last function.
addContact function with an empty event object to verify that the error handling works as expected.
Now test with an event object containing a new contact. (note: you will need to modify your test input through Actions->Configure test event)
If both tests work then congratulations on creating your first Lambda functions. Part 2 of this post will describe how to configure these functions to run synchronously on an API Gateway HTTP request.