I have also been exploring AWS Lambda - Amazon's serverless offering, I thought I would attempt to build my own serverless function. I thought one cool way to test this would be to attempt to automate my Twitter and develop some simple Twitter Bot that would Like and Retweet random tweets with Hashtags.
What is Serverless computing
Serverless computing is a type of event-driven architecture - functions are reacting to specific type of trigger events. Most of the large Cloud vendors i.e. AWS, Azure, Google Cloud are investing heavily in providing this functionality.
The term Serverless, initially may seem a little bit misleading, I know when I first heard the term I misconstrued it to infer that somehow we are now able to deploy applications without the need for a server! Of course this is not possible, however it does mean we can deploy our code without having to concern ourselves with the configuration and management of the server.
Serverless means being able to deploy your code without worrying about a server
I have previously discussed how to create a simple lightweight node.js webserver and how to create a RESTful Web Api with node.js and swagger providing examples of how simple and fairly trivial a task of creating these features can be using node. However, what is hidden behind these features is that the developer still has to concern themselves with how these features are deployed and managed.
In order for your functions and web Api's to be accessible they will still need to be deployed to a server somewhere. Those servers will need to be managed and have the ability to elastically scale depending on use and load.
The developer also has to concern themselves with a number of different plumbing libraries like Express and swagger to enable features.
Functions as a service (FaaS)
In a microservice environment you may just want to break your services up into small isolated units which call each other, you often don't want or even desire the additional administrative overhead and infrastructure burden.
Breaking this down a little is that typically end up building – ad hoc functions. Separating your code into functions is, of course, nothing new and as developers we know this is just best practice. However, where serverless adds to additional spins:
- Everything you develop is a function.
All your code is standalone and composable - Polyglot programming
Enabling you to develop your functions in any language of your choice. So if something is better off suited to being developed in Java, C#, JavaScript Python or any other programming language you can develop your function in that language and consume it your application
What is the Serverless Framework
The Serverless Framework is an open-source, MIT -licensed command line tool developed in node.js to help quickly and easily develop functions to run on AWS lambda.
Build applications comprised of microservices that run in response to events, auto-scale for you, and only charge you when they run. This lowers the total cost of maintaining your apps, enabling you to build more logic, faster.
Serverless framework
Serverless Framework - Github Repository
Install Serverless Framework
To install the Serverless framework simply npm install -g serverless
Create project with serverless framework
To create a project using the serverless framework simply create a folder and change into it : mkdir tweetpress && cd tweetpress
.
we create our project using serverless create -t aws-nodejs
Well notice that the directory is now populated with .gitignore , handler.js, serverless.yml
.gitignore
The git ignore file should be pretty self explanatory. Git uses it to determine which files and directories to ignore, before you make a commit.
handler.js
This declares your Lambda function.
serverless.yml
Do not underestimate the power of the serverless.yml
file. It enables the configuration and description of your service functions. Take a little time to understand the functionality available here. For the sake of brevity I will not explain all, however I will highlight some important options throughout this post.
You are easily able to schedule intervals when specific functions of your service execute using serverless.yml
Configure Credentials
Go ahead and create an IAM user you want your Service to execute under then Grab the Access Key ID
and the Secret Access Key
then register them using the command line
serverless config credentials --provider aws --key [Your Key Here] --secret [your secret here]
Deploy the initial application
We can now at this stage, deploy our initial application stub to test that it all works as expected. While we're at it we'll create 2 environments which we'll use for Development and Production at this stage.
dev
To deploy the dev environment we can just run serverless deploy
prod
We can create a production environment really simply by serverless deploy --stage prod
You should now be easily able to test a basic stub services by clicking on the urls.
npm init
We are going to be adding some additional node modules dependencies to our project, so in order to manage this we need to add a package.json file. The easiest way to do this is npm init
which will call up the customary questions.
Once complete we can add the dependency we'll be using : Twitter Client for nodejs
To install the dependencies :
npm install twitter --save
Register application on Twitter
You will need to register an application on Twitter and get you API Credentials.
Add Config.js
Add an additional JS file to your project and call it config.js
Add your API keys & secrets to this file.
Create another additional JavaScript file and call it twitter_controller.js
, which for the most part will be used to isolate most of the interactivity of the application with Twitter making use of the Twitter Client
.
If you read Brandon Morelli's article you'll no doubt notice that this code is more or less just a copy and paste of his code. For the purpose of this article it really doesn't need to be all that more complicated.
The main point being is that you can have whatever code you want in any number of files you may need. This area of the code is really isn't part of the serverless aspect, this primarily just want your Function to do.
Handler
We can now edit our handler.js
file to include our twiiter controller file and logic to interact with it.
There a few basic points work discussing here. You'll notice that we actually export a basic module which creates a function. You can add as many functions to the file as you may need.
In this example we are targeting AWS Lambda as te deployment target, and because of that the Serverless framework has generated us a stub with 3 arguments, this can be provider and language specific, so you may see some differences. However in our case because we are targeting AWS and Node, we have 3 parameters, but really we only have to concern ourselves with the first one events
.
We will use the events
parameter to pass arguments into our function as a service. This is typically just a JSON array.
You'll notice that all we do here really is extract values from the array passed in then pass it down to our controller method to action. While building up a response object.
Serverless.yml
We now just need to edit the serverless.yml
, this will help describe our function to the AWS services and you'll notice you describe the region we want our function deployed to, the runtime to use.
We also provide some basic features of our functiosn like the Name i.e. favourite and the handler to use. In our instance we want our function accessible over http
and the path with be url/path i.e. /favourite
Thats it, our code for this very simple application is now complete and we can now deploy it to AWS lambda to do so we simply serverless deploy
.
Once complete we can now test our function using Postman or any other API testing tool of choice.
Summary
Despite our application being a rather contrived and simple application, the steps of developing and deploying our function are exactly the same and you will no doubt observe that it is extremely easy to build lightweight frictionless services and deploy them AWS without the need extensive infrastructure management.
AWS Lambda is exremely cheap and robust solution if you want to create API services for your Website of mobile apps to consume.
References
For a great whistle stop tour of the features discussed above I recommend watching the following video
- What is this Directory.Packages.props file all about? - January 25, 2024
- How to add Tailwind CSS to Blazor website - November 20, 2023
- How to deploy a Blazor site to Netlify - November 17, 2023