AWS serverless in 2024 : SAM, Lambda, API gateway, DynamoDB Cognito and OpenAPI standard

Introduction:

In 2024, an AWS serverless architecture continues to be a popular choice for building scalable and cost-effective applications. With a service stack using SAM (AWS Serverless Application Model), AWS Lambda,  API Gateway, DynamoDB, Cognito, and OpenAPI standard; developers can create serverless solutions with ease. This guide will walk you through the process of setting up a project with these AWS serverless components. dotnet 6 was the chosen Lambda runtime.

This example was developed as an example game back-end, however this architecture can apply to a number of real-world scenarios including web applications.

To summarise the chosen the role of the services in the chosen stack:

  1. SAM (AWS Serverless Application Model): A framework to simplify serverless application development that serves as an extension of CloudFormation.
  2. AWS Lambda: Implement business logic with serverless functions.
  3. API Gateway: Create APIs to enable back-end communication.
  4. DynamoDB: Store application/game data.
  5. Cognito: Managed service for authentication and authorisation.
  6. OpenAPI standard: Declarative syntax for creating clear and consistent APIs to be shared amongst developers.

Part 1 – Initial Setup:

  1. Install AWS CLI and Setup with AWS SSO:
  •    Install the AWS CLI, which allows you to interact with AWS services from the command line.
  •    Set up AWS Single Sign-On (SSO) and use the aws sso login command to refresh your credentials.
  1. Install SAM and Docker Desktop:
  •    Install the AWS Serverless Application Model (SAM) CLI, which provides a simplified way to build, test, and deploy serverless applications.
  •    Install Docker Desktop, as it is required for running functions locally.
  1. Create a SAM Project:
  •    Use the command sam init to create a new SAM project.
  •    Choose the AWS Quick Start Templates and select the Hello World Example.
  •    When prompted, I selected dotnet6 as the desired runtime.
  •    When prompted, select the desired package type (I used zip in this example).
  •    Change to the project directory using cd proj-name and launch VS Code with code ..
  1. Build and Test:
  •    Execute the command sam build to build the project.
  •    Test the project locally using sam local invoke HelloWorldFunction.
  1. Deploy:
  •    Deploy the project using sam deploy –guided.
  •    Review the changes to be deployed and confirm by typing ‘y‘.
  •    You can review the deployment in the AWS Management Console under CloudFormation.

 

Part 2 – Modifying the Example SAM Template Project:

  1. Add OpenAPI to the SAM Project:
  •    If you already have an API definition, copy the YAML file into the SAM project.
  •    In the example project, delete the Events block in the HelloWorldFunction section.
  1. Add OpenAPI Definitions:
  •    Add the OpenAPI definitions for the /hello route to the SAM template.
  •    Include the necessary information such as the operationId, responses, and integration details.
  •    Pay attention to the x-amazon-apigateway-integration block.
  1. Add API Resource:
  •    Define the HttpAPI resource in the SAM template.
  •    Specify the AccessLogSettings and DefinitionBody using the Fn::Transform and AWS::Include.
  1. Build and Test the API:
  •    Start the API locally using sam local start-api.
  •    Verify that the HelloWorldFunction is mounted at http://127.0.0.1:3000/hello [GET].
  1. Add Permissions:
  •    Grant the necessary permissions to the HelloWorldFunction using the AWS::Lambda::Permission resource in the SAM template.
  1. Rebuild and Deploy:
  • Rebuild the project using sam build.

Deploy the changes using sam deploy –guided.

  1. Test with Curl or Postman:
  •    Test the API by sending requests to the deployed endpoint using tools like Curl or Postman.

 

Adding New Lambda Functions:

  1. Create Empty Constructor:
  •   To add additional functions to the same class file, create an empty constructor in the class.
  1. Add New Function Block to the Template:
  •    Add a new function block to the SAM template for each additional function.
  •    Specify the CodeUri, Handler, Runtime, MemorySize, Environment variables, and any required policies.

 

Adding Authentication with Cognito:

  1. Create Cognito User in the Template:
  •    Add a parameter for the email address of the created user in the SAM template.
  •    Include a UserPoolUser resource with the desired properties.
  1. Deploy with Parameter Override:
  •    Deploy the template with the CognitoUserEmail parameter override using sam deploy –parameter-overrides CognitoUserEmail=email@example.com.
  •    This will email a password to the user specified in the parameter.
  1. Initiate Auth Response and Obtain IdToken:
  •    Use the AWS CLI command aws cognito-idp admin-initiate-auth to get the session token.
  •    Respond to the challenge to change the password and obtain the IdToken.
  1. Test Authentication:
  •    Use the obtained IdToken to make authenticated requests to the API using tools like Curl.

 

Adding DynamoDB Integration to Lambda:

  1. Install Required Packages:
  •    Install the AWSSDK.SSO, AWSSDK.SSOOIDC, and AWSSDK.SecurityToken packages (required for local development).
  1. Batch Writing Data to DynamoDB:
  •    Convert JSON data to DynamoDB format using the aws dynamodb batch-write-item command.

 

Conclusion:

By leveraging a serverless stack of SAM, Lambda, API Gateway, DynamoDB, Cognito, and the OpenAPI standard you can enjoy the benefits of serverless computing in reduced operational overhead, automatic scaling and pay-as-you-go pricing, allowing you to focus more on innovation and less on infrastructure management.

 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *