The problems development teams at large companies like MasterCard or Goldman Sachs face are largely problems of scale. For example, how should an authentication system work when clients randomly connect to thousands, even hundreds of thousands of servers in unpredictable ways? How are sessions coordinated? How is a UI connected to one server notified about changes on another server? These difficulties faced by distributed system engineers have led to new strategies on how to structure large scale architectures.
The strategy of composing large systems out of small, focused, independent units has been successful for many teams migrating away from monolithic architectures. These composed architectures are commonly described as “microservice” architectures. One next step in this progression is moving distinct functionality into the cloud absent any server maintenance at all: “serverless” technology, sometimes described as “FaaS” (Functions as a Service).
Amazon Web Services (AWS) has pioneered this revolutionary idea with their “Lambda” offering.
In this article we’re going to use the claudia package for NodeJS, which is a well documented, maintained, and designed toolkit for deploying serverless products.
Scaling with Claudia and API Gateway
To alleviate for developers the hassle of writing tricky boilerplate code to connect with AWS Lambda, the NodeJS community has delivered several excellent “serverless toolkits” as npm packages. We will be using one named claudia.js (get it, cloud architects?), which you can learn more about here: https://claudiajs.com/.
To begin, you will need to create a free developer account with AWS at https://aws.amazon.com. Most AWS services have very generous free usage tiers, which you can use AWS while learning and developing without incurring any cost (within limit). With Lambda, for example, the first one million requests per month are free.
Now log in to your dashboard. Time to identify claudia as having access to your account and managing rights for the claudia user.
From the Services tab, select IAM. Claudia needs permission to communicate with your AWS account. You generally do not want to use your root account privileges in applications, which should be understood as “sub-users” of your account. AWS provides an Identity and Access Management (IAM) service to help with that. Let’s create an AWS profile with IAM full access, Lambda full access, and API.
1. Click Add User. You can use any name, but we’re going to use claudia to keep things consistent. You’re creating a new user claudia, affording this user programmatic access.
2. Next, the Permissions button. We need to attach the IAM account claudia just created to the Lambda and API Gateway services and give it administrative privileges.
3. Select Attach existing policies directly, and add the following permissions your claudia user will need to work with Lambda and API Gateway:
- AdministratorAccess
- AmazonAPIGatewayAdministrator
- AWSLambdaFullAccess
Hit Review, where you should see a claudia user with the correct permissions.
Great. Click on Create User. You will be presented with your shiny new IAM credentials. Copy the provided Access key ID and Secret access key (you’ll need these later). You’re now ready to start deploying Lambda functions using claudia.
Installing claudia
To begin installing the claudia module, type the following command:
npm install claudia -g
Remember the credentials you just created for the claudia user? Time to store them in a place claudia can find. A good pattern here is to store an AWS configuration file in your home directory (on OSX, this would be /Users/<yoursystemusername>). Visit your home directory, and create a .aws/credentials file with the following contents (remember to replace with your real credentials):
[claudia]
aws_access_key_id = <YOUR_ACCESS_KEY>
aws_secret_access_key = <YOUR_ACCESS_SECRET>
Here we are indicating that claudia is the AWS profile name, using these IAM credentials. claudia will automatically grab and use these credentials to authenticate with AWS and do its magic.
Now let’s create a web-accessible HTTP endpoint that returns the string “Hello from InRhythm!”.
Creating a serverless API
Create a new directory and initialize an npm package with npm init, using any name that you’d like for the package. To work with AWS API Gateway, we’ll also need to install an extension package for claudia:
npm install claudia-api-builder
Next, add the following app.js file to this package:
const ApiBuilder = require('claudia-api-builder');
const api = new ApiBuilder();
module.exports = api;
api.get('/hello', function () {
return 'Hello from AWS!';
});
Here we attach a Lambda function to handle a GET on the /hello route. Note how natural this feels, especially to any Express users out there (anyone?). Surprisingly, we’re done! To deploy, enter the following into your terminal:
AWS_PROFILE=claudia claudia create --region us-east-1 --api-module app
The AWS_PROFILE environment variable provides the [claudia] profile identifier in the credentials file you created earlier, with a deployment region flag, –region. This starts the deployment process.
If everything goes well, your endpoint will be deployed, and information similar to the following will be returned:
{
"lambda": {
"role": "claudiaapi-executor",
"name": "claudiaapi",
"region": "us-east-1"
},
"api": {
"id": "s8r80rsu22",
"module": "app",
"url": "https://s8r80rsu22.execute-api.us-east-1.amazonaws.com/latest"
}
}
The returned URL points to our API gateway. But it’s not complete—now we need to add the additional route /hello, added earlier, resulting in the full URL:
https://s8r80rsu22.execute-api.us-east-1.amazonaws.com/latest/hello
You will see the following message:
Hello from InRhythm!
That was easy. Updating the function is just as easy. Return to the code and change the string message your function returns to something else, then run:
AWS_PROFILE=claudia claudia update
Go ahead and reload the returned endpoint in your browser and you will see the updated message.
We have satisfied the key goal of creating independent, stateless, services that can be replaced or upgraded (or downgraded) independently.
Remember that you can gain full access to the Lambda functions you create by simply returning to the AWS dashboard and visiting your Lambda services, where you have a complete “IDE” for managing your lambdas.
Be sure to read the docs at https://claudiajs.com/documentation.html. They are filled with examples to get you started building the future of software with NodeJS and claudia!
Leave a Reply