My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
Serverless : AWS Lambda and Spring Cloud Function with Twitter API

Serverless : AWS Lambda and Spring Cloud Function with Twitter API

Shahul's photo
Shahul
·Oct 6, 2020·

6 min read

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 API:

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.

Screenshot from 2020-10-05 11-04-39.png Twitter profile name before update

Screenshot from 2020-10-05 11-03-32.png Updated profile name with live follower count

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.

carbon (1).png

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.

carbon (2).png

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.

carbon (4).png

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.

carbon (5).png

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.

carbon (6).png

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.

carbon (7).png

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.

Advantages:

  • 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.

Screenshot from 2020-10-05 12-35-47.png

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.

Screenshot from 2020-10-05 12-52-47.png

STEP 3: Next up upload the jar which we created earlier as shown below.

Screenshot from 2020-10-05 13-00-07.png

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.

Screenshot from 2020-10-05 13-01-52.png

STEP 5: Finally we click on Test and we have the below output and my Twitter profile also got updated.

blur.png

That’s it!! We have completed our first Serverless AWS lambda using Spring Cloud Function.

IMPROVING OUR FUNCTION:

  1. 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.
  2. 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.

CONCLUSION:

This is a getting started guide and there are still lots to explore and if you went ahead and did something cool leave it in the comments below.

GITHUB Linkhttps://github.com/shahulbasha/serverlessawslambda