In this article we will explore creating a simple AWS lambda function with Spring Cloud which will update our twitter profile name dynamically.
TABLE OF CONTENTS:
- Twitter API
- Spring Cloud Function with Spring Boot
- Serverless AWS Lambda Function
Twitter is one of the most popular API’s around where you can search and scrape for a lot of data analysing trends of a particular location among other things. To use the Twitter API head over to https://developer.twitter.com/apps and create a new app describing its purpose. Once you create it you will be given 4 keys – API key, API secret key , Access Token and Access Token Secret.
To keep it simple we will be updating the profile name of my Twitter account dynamically through a lambda function.
As you see above we change the name of the profile and also append the current follower count of the account.
SPRING CLOUD FUNCTION:
Spring Clound Function is a project which was developed to promote running business logic as functions. This is great because the same code can run as a web endpoint, a stream processor, or a task. Importantly this works well with Serverless providers . If you have developed a traditional Spring Boot REST API you would have an embedded server to launch the application on a particular port whereas on the other hand Spring Cloud Functions are serverless.
Create a Spring Boot starter project and add the following dependencies. We will use the Twitter4J client to interact with Twitter API.
Note that this will not provide you with an embedded server to test the application locally. In case you need to launch your application locally on a port and test it add the following dependency additionally.
The entire project is available on Github and i will leave a link at the end of the article. Next we create an instance of Twitter as shown below with the keys that we got earlier. The keys are stored in properties file and injected. Do not store these keys publicly or your twitter account is a goner.
Next up we create the service which will do the update of the profile name. Here we append the current follower count to the existing name. We also return the new profile name.
Now exposing it out to the web is the only thing that remains . Usually we will have a Controller which will do the job for us. In Spring Cloud Functions we make use of Functional Interfaces to expose the services. The Functional interfaces have to be one of the existing functional interfaces like Supplier , Function , Consumer and Predicate. If you are customizing the Functional interface you need to create a wrapper around it which is beyond the scope of this article.
Here we return a Supplier which returns a String which is the updated username of the Twitter profile. Now Spring Cloud will also expose this method at /username to be accessed publicly as a REST endpoint.
Finally for deploying as an AWS lambda function we need to add the below handler class inorder for AWS to recognize the application and its endpoints the inputs and outputs of the endpoint too. Here we have to extend the SpringBootRequestHandler class with types as String as i am returning a String. The input is also defined as a String although there are no inputs to this username endpoint in current use case.
Do a maven build and create the snapshot jar to be uploaded.
SERVERLESS AWS LAMBDA FUNCTION
Serverless doesn’t mean there is no server it is just the fact that as a developer we don’t need to care about setting it up. We only write the business logic as we have done so far and then hand it over to a serverless provider to do the rest.
- No Server Management
- Pay only for the duration that the function runs.
AWS Lambda is the name of the service that Amazon provides for executing and managing these serverless functions.
STEP 1: Create an account with AWS by providing your credit card details and then log in to the dashboard. In your console search for service Lambda and click Create. You will be landing on the below screen. I am using a Java 8 application and hence i select that . You can even submit a Node or Python script in place of Java . Once you give these details click Create Function.
STEP 2: In the next page edit basic settings point to the Twitter Handler class which we created earlier and give a timeout of 30 seconds and click Save.
STEP 3: Next up upload the jar which we created earlier as shown below.
STEP 4: Finally we have to configure the test event which is on the top right corner of the same screen. Here if our endpoint needs any input parameters we can give it. But in our case we don’t need it and hence i leave it as an empty string.
STEP 5: Finally we click on Test and we have the below output and my Twitter profile also got updated.
That’s it!! We have completed our first Serverless AWS lambda using Spring Cloud Function.
IMPROVING OUR FUNCTION:
- Next up we need to expose this out so that it can be called as a REST API from the outside world. We can use AWS API gateway to trigger this lambda function but be careful as you might be billed on certain services.
- Scheduling our application as a CRON Job would trigger this function let’s say every 30 minutes so that our follower count is automatically updated on our Twitter profile. Also we could change our profile pictures and keep a different image for each day of the week as a fun experiment. The possibilities are endless and you can do a lot of data analysis too.
This is a getting started guide and there are still lot to explore and if you went ahead and did something cool leave it in the comments below.
GITHUB Link – https://github.com/shahulbasha/serverlessawslambda