serverless On Aws

serverless On Aws

Building Scalable and Efficient HTTP APIs with the Serverless Framework

Table of contents

No heading

No headings in the article.

In this blog post, we will take a deep dive into building detailed HTTP APIs on AWS using the Serverless framework. We will explore the key components and steps involved in designing, implementing, and deploying highly customizable and efficient APIs on the AWS cloud.

Why Choose AWS and Serverless for HTTP APIs?

AWS provides a comprehensive suite of cloud services that are ideal for building HTTP APIs. Leveraging the power of the Serverless framework on AWS allows developers to focus on writing code and delivering features without the need to worry about infrastructure management. The combination of AWS and Serverless offers benefits such as:

  • Scalability and High Availability: AWS automatically scales resources to handle any amount of traffic, ensuring your API performs optimally even during peak loads.

  • Cost Optimization: The pay-as-you-go model of AWS ensures cost efficiency, as you only pay for the resources consumed by your API. There are no upfront costs or idle resources.

  • Seamless Integration: AWS provides a wide range of services that seamlessly integrate with the Serverless framework, such as AWS Lambda, Amazon API Gateway, AWS DynamoDB, AWS S3, and more. This enables you to leverage a powerful ecosystem of services to enhance the functionality of your HTTP APIs.

  • Security and Compliance: AWS offers robust security features and compliance certifications, allowing you to implement secure authentication, authorization, and data protection mechanisms for your APIs.

Use Cases for Serverless Architecture:

  1. Small Web Applications:

    • Serverless architecture is particularly suitable for small-scale web applications, such as a To-Do app.

    • The predictable and intermittent nature of user activity makes it a perfect fit for the pay-as-you-go model.

  2. Event-Driven Workloads:

    • Serverless architectures excel in event-driven workloads where functions are triggered by events like HTTP requests, database changes, or file uploads.

    • These characteristics make serverless ideal for building webhooks,

      real-time data processing, and IoT applications.

Designing Detailed HTTP APIs on AWS.

API Gateway Configuration: AWS API Gateway acts as the front door for your HTTP APIs. HTTP APIs play a crucial role in modern application development as they enable communication and interaction between different components of a software system. They allow applications to send requests and receive responses over the internet, facilitating seamless integration between various services, platforms, and devices.

Using AWS and the Serverless framework for building HTTP APIs offers several benefits.

  1. AWS provides a comprehensive suite of cloud services that can be easily integrated with the Serverless framework, allowing developers to leverage powerful tools like AWS Lambda, Amazon API Gateway, DynamoDB, and S3.

  2. the Serverless framework eliminates the need for infrastructure management, allowing developers to focus on writing code and delivering features. With automatic scalability and high availability provided by AWS, developers can ensure optimal performance even during peak loads.

  3. the pay-as-you-go model of AWS ensures cost optimization, as you only pay for the resources consumed by your API. This eliminates the need for upfront costs or idle resources.

Additionally, AWS offers robust security features and compliance certifications, enabling developers to implement secure authentication, authorization, and data protection mechanisms for their APIs.

Overall, using AWS and the Serverless framework for building HTTP APIs provides scalability, cost efficiency, seamless integration with other AWS services, and robust security features, making it an ideal choice for modern application development.

Implementing and Deploying HTTP APIs with Serverless:

  • Function Implementation: Write serverless functions using AWS Lambda and your preferred programming languages, such as Node.js, Python, or Java. Each function should handle a specific API endpoint or business logic.

  • Serverless Configuration: Define your API's infrastructure and configuration using the Serverless framework's declarative configuration file, often named serverless.yml. Specify function handlers, event triggers, environment variables, and AWS service integrations.

  • Packaging and Deployment: Use the Serverless framework's CLI commands to package your code, resolve dependencies, and deploy your API. The framework handles the provisioning of necessary AWS resources, such as Lambda functions, IAM roles, and API Gateway.

  • Continuous Integration and Deployment (CI/CD): Integrate your Serverless API deployment with a CI/CD pipeline to automate the build, test, and deployment processes. This ensures efficient and consistent delivery of updates to your HTTP APIs.

AWS Serverless Architecture and Services

  1. AWS Lambda: Run code in response to events, e.g., processing uploaded files, and handling API requests.

  2. Amazon API Gateway: Create and manage APIs for serverless functions, e.g., building RESTful APIs for your application.

  3. AWS DynamoDB: Fully managed NoSQL database, e.g., storing user profiles and preferences.

  4. AWS S3: Object storage service, e.g., hosting static website assets like images and CSS.

  5. AWS Step Functions: Coordinate complex workflows, e.g., orchestrating microservices for order processing.

  6. AWS SNS: Simple Notification Service for push-based messaging, e.g., sending alerts to subscribers.

  7. AWS SQS: Simple Queue Service for message queues, e.g., decoupling components for asynchronous processing.

  8. AWS Cognito: User authentication and authorization, e.g., managing user sign-up and login.

  9. AWS CloudFront: Content delivery network, e.g., caching and delivering website content globally.

  10. AWS AppSync: Real-time API development, e.g., building chat applications with live data synchronization.

  11. AWS CloudWatch: Monitoring service, e.g., tracking serverless function performance and resource usage.

These services, when used together, form a powerful serverless architecture that enables developers to build scalable, cost-efficient, and flexible applications with reduced infrastructure management overhead.

Hands-on

Step 1: To install Node on your machine, follow these steps:

  1. Begin by updating the package repository contents database on your Ubuntu Linux system. Open the terminal and execute the following command:

     sudo apt update
    

    This command will fetch the latest package information from the repositories and update the local package database.

  2. After the system update, install Node.js by first installing the required repository.

     curl -sL deb.nodesource.com/setup_14.x | sudo bash -
    

    Once the repository is added, you can begin the installation of Node.js on Ubuntu

  3. After updating the package database, you can proceed with installing Node. Node.js provides an installation package called nodejs in the default Ubuntu repositories. Run the following command:

     sudo apt -y install nodejs
    

    This command will install Node.js along with its package manager, npm, which allows you to install additional modules and libraries.

    Verify that Node.js has been successfully installed by checking its version. Execute the following command:

     node -v
     npm -v
    

    This command will display the version of npm installed on your system.

    With Node.js installed on your machine, you are now ready to develop and run applications that utilize the Node.js runtime and its vast ecosystem of libraries and frameworks

Step 2-Install Serverless Framework — click on the Documentation page and follow the instructions:

  • Install the serverless CLI via NPM

      sudo npm install -g serverless
    

    We have a serverless installed host. Start building your project now.

  • Type serverless and enter.

  • Using the down arrow select ‘Node.js — HTTP API’ and give a name to the project as shown below.

    Do not deploy the application for now as we need to set the platform, region, etc.

    Type the command ‘ls’ and you will see your ‘aws-node-http-api-project’ folder. Change the directory by command ‘cd aws-node-http-api-project’ and again ‘ls’ to look into the folder. You will get ‘README.md’, ‘index.js’ and ‘serverless.yml’ files.

Step 3: Now create a user and give full access.

serverless.yml is a configuration file for connecting AWS accounts. This means you will require ‘access keys’ and ‘IAM user’. And give access to all the essential functionalities to IAM users and add the respective user keys through AWS CLI to the host where serverless is installed.

  • Open IAM Dashboard on the AWS account and create a user ‘serverless-admin’ by clicking ‘Add users’. Click Next.

  • Now give permissions for all the functionalities we are going to use as mentioned above.

  • Click on ‘Create user’.

  • Click on User name

  • In the Security Credential column, click on ‘Create Access Keys'

  • Select CLI

  • Click create an access key.

  • Download the Key by clicking Download .CVS file.

Step 4: Configure aws on Ubuntu.

  • Install Aws CLI using the command 'sudo apt install awscli'

  • On Ubuntu local server type the ‘aws configure’ command and enter the access secret, access keys and Default region name: us-east-1.

Step 5 :

  • Type the command ‘vim serverless.yml’ and start to understand the serverless file as shown below:

    Here ‘provider’ is creating an AWS lambda function which will be used by Node.js.

    And the ‘functions’ will have the name of the lambda function, the path, event, etc

Step 6-

  • Type command serverless deploy

  • Copy this URL and run it on the browser:

    https://k1g6c7mj44.execute-api.us-east-1.amazonaws.com/

  • It runs, Let's do some updates and check again

    delete the event portion and deploy again,

  • Browse the URL

    It runs successfully

  • To check serverless is running without a server, stop the EC2 server and again browse the URL.

    Again it will display the same.

Step-7: To set up the Serverless dashboard and display your application. This command will initiate the login process to the Serverless dashboard

  • Run the command: serverless login and Select ‘Serverless Framework Dashboard

  • By running the serverless login command and logging into the Serverless dashboard, you can leverage the platform's powerful capabilities to monitor and manage your serverless application effectively.

  • Copy the generated URL and paste it into the browser.

  • click ‘continue to dashboard’.

  • On the Ubuntu host type ‘sls deploy’. If not showing any changes on the dashboard try to redeploy.

AWS Dynamo db and S3

Amazon S3

  • Amazon S3 (Simple Storage Service) is a scalable and secure cloud storage service provided by AWS.

  • S3 allows storing and retrieving any amount of data from anywhere on the web.

  • It offers high durability, scalability, and multiple copies of data across facilities within a region.

  • Access control and security features include ACLs, bucket policies, and IAM.

  • Versioning enables tracking changes and recovering from accidental deletions.

  • Lifecycle policies automatically transition objects between storage classes.

  • S3 supports data encryption at rest and in transit.

  • Integration with other AWS services, such as Athena and CloudFront, is seamless.

  • Logging, monitoring, and event notifications enhance visibility and auditing.

  • S3 offers flexible pricing based on storage, data transfer, and additional features.

Step-8 : Let's check with AWS S3. It's simple, whatever code package we have, is stored in S3.

  • Go to Amazon S3

DynamoDB

  • DynamoDB is a fully managed NoSQL database service provided by AWS.

  • It offers a flexible schema design and supports key-value and document data models.

  • AWS manages all aspects of database administration, including scaling and replication.

  • DynamoDB automatically scales to handle high throughput with low latency.

  • It follows a serverless and pay-as-you-go pricing model.

  • The database provides high availability and data durability through replication across multiple Availability Zones.

  • Global tables allow data replication across multiple regions for low-latency access and disaster recovery.

  • Advanced features include data encryption, in-memory caching, and transaction support.

  • DynamoDB integrates seamlessly with other AWS services, expanding its capabilities.

  • It is commonly used for e-commerce, gaming, real-time analytics, and content management systems.

Step-9: Fork the repository and switch your branch todev’.

github.com/LondheShubham153/aws-node-http-a..

Here, the source code repository of simple HTTP API with Node.js running on AWS Lambda and API Gateway using the Serverless Framework.

Paste below command to deploy clone repository in CLI

Step-10

For confirmation, we can test these APIs using the Postman application.

key aspects and roles involved in the development of a web application:

  • Application backend focuses on server-side logic and data processing.
  • Front-end skills are essential for creating an impressive and visually appealing user interface.

  • Combining both back-end and front-end skills allows the development of a "Web-Application."

  • Back-end handles server-side functionalities and data management.

  • Front-end skills are responsible for designing and implementing the user interface.

  • A "Web-Application" is the result of integrating back-end and front-end capabilities effectively.

    Go to CloudFormation:

  • Conclusion: serverless computing has revolutionized the way we develop and deploy applications. Its ability to allow developers to focus on application logic, coupled with the cost-effectiveness of the pay-per-use model, has significantly improved agility and feature delivery. By eliminating the need to manage infrastructure, serverless has reduced deployment and maintenance time. However, it is essential to acknowledge its limitations, such as cold start times and lack of infrastructure control. To make the most of serverless, evaluating specific use cases and finding the right balance between serverless and server-based architectures are key factors in achieving optimal DevOps outcomes.

Serverless is a complex and extensive topic that cannot be fully explored in a single blog post. For a more thorough understanding of serverless, it's best to explore various resources, tutorials, and documentation.

Thanks for reading!!