Skip to content

Single repository

While developing a serverless solutions, one of the most painful aspects is to manage dependencies.

The most obvious solution, is to have your own NPM dependency, but every change on the logic will require publishing your dependency, update your lambda to a newest version, and then deploy. This process is slow and error prone, and is specially painful when you are debuggin an error.

Our proposal is to have a single repository with all your lambdas and dependencies on a single repo with a single deployment process, while keeping a convenient separation of concepts. For this, we use a lambda layer that is shared among all lambdas, and is automatically linked on deployment time.

This lambda layer will serve all lambdas access to database, storage, queues and any other AWS services involved, leaving a lambda the sole function to interact with them an orchestrate the logic, while API gateway is able to interact with lambdas when necessary, or directly with AWS services when a lambda is not required, offering a wide range of scalability.

Single repository diagram

Code Structure

This is the basic structure of a Noback backend:

  • Directory/
    • Directorylambda-layer/
      • Reusable code available to any lambda within the project
    • Directorysrc/
      • Lambda functions for the application code.
    • Directorytemplates/
      • Modular SAM templates, extending template.yaml
    • config.env Config file with all the environment variables to be consumed by the Cloudformation stack
    • deploy.sh Script that automates building and deployment to your AWS account
    • template.yaml Main SAM template, that defines resources and permissions
    • template-samconfig.toml Template to build the SAM configuration file (samconfig.toml) using ‘config.env’ parameters

Lambda layer

It’s the place for all shared dependencies, and it provides helper methods to interact with AWS services, like Cognito, S3 or DynamoDB. It’s assigned to Lambdas using Layers flag inside Lambda definitions on templates/api-gateway.yaml.

It’s built with Typescript, and within this folder there are the required structure to build the layer and make it available to any lambda using it automatically.

This filetree exposes all the important files:

  • Directoryresource/
    • Directorynodejs/ expected route by the lambda with the built code for the layer
  • Directorysrc
    • Directorysrc/
      • Directorydynamodb
        • Directorycollections
          • Directoryentities You can use any entity in this folder as a template
            • DirectoryentityName
              • entityNameModel.ts
              • entityNameSchema.ts
              • interfaces.ts
          • index.ts Any new entity should be exported here
          • interfaces.ts Any new entity should be updated here
      • Directoryhelper Helper methods for any AWS service
        • cloudwatch
        • cognito
        • costexplore
        • data This stablish and initialize DynamoDB
        • eventbridge
        • queues
        • rekognition
        • s3
        • security Custom security methods
        • index.ts Generic helper methods
      • index.ts general exports to be consumed by lambdas