awsm-org / awsm

AWSM: Amazon Web Services Modules – Re-usable, standardized and optimized Lambda functions ready for deployment and easy installation into serverless projects
170 stars 12 forks source link

AWSM logo aws modules lambda api gateway JAWS

AWSM: Amazon Web Services Modules

Intro

Amazon Web Services Modules (aws-modules, awsm’s) contain pre-written, isolated functions ready to run on one or multiple AWS Lambda functions. Some examples are: functions that send out emails, register users or handle webhooks from other services.

The purpose of the aws-module format is to create an ecosystem of re-usable, standardized, optimized Lambda functions ready for deployment and easy installation into serverless projects.

aws-modules were designed to work with JAWS: The Serverless AWS Framework. The JAWS command line tool comes with commands to create and install aws-modules into your serverless projects. View the JAWS documentation for more information.

aws-modules will soon support all of the languages AWS Lambda supports. Currently, only javascript (node.js) is supported. Building a module system that supports multiple programming languages is challenging, but since the functions of serverless projects/applications are completely isolated, functions written in different programming languages can be combined within the same project. Given some languages are more efficient for specific tasks, this is a nice benefit.

awsm.json

awsm.json At Module Root

Example of an awsm.json at the root of an aws-module

The defining feature of an aws-module is an awsm.json file located at the root of the module. aws-modules' configuration settings, dependencies and authorship details are described in this awsm.json file. Below, are limited awsm.json examples. To view all available properties, see the awsm.json file in this repo.

Any required AWS resources (e.g., DynamoDB, S3) outside of the Lambda and API Gateway resources your module requires in the "resources" object:

"name": "usersCreate",
"description": "Creates a user",
"version": "0.0.1",
"location": "github.com/you/your_aws_modules_repo",
"resources": {
    "cloudFormation": {
        "LambdaIamPolicyDocumentStatements": [],
        "ApiGatewayIamPolicyDocumentStatements": [],
        "Resources": {}
    }
}

Please note that JAWS defines some standard AWS CloudFormation parameters that can be used as "Ref"'s

awsm.json At Lambda Root

Example of an awsm.json at the root of a lambda sub-folder

Within each resource/lambda directory is another awsm.json which describes either an AWS Lambda configuration, an API Gateway configuration, or both. awsm.json files within resource/action directories need only a "lambda" or "apiGateway" property (or both).

"lambda": {
    "enVars": [],
    "package": {},
    "excludePatterns": {},
    "cloudFormation": {
        "Description": "",
        "MemorySize": 1024,
        "Runtime": "nodejs",
        "Timeout": 6
    }
},
"apiGateway": {
    "cloudFormation": {}
}

Lambda Configuration Options

Note: All of the attrs below assume the lambda attribute key prefix.

Creating AWS-Modules

Whether you are writing aws-modules exclusively for your serverless project (every Lambda function in a JAWS project is an aws-module) or writing them for sharing publicly, the process to create them is the same.

Creating Re-Usable AWS-Modules

If you want to use your aws-module across multiple projects and optionally share it with the aws-module community, do the following:

AWSM + NPM-Modules

Architecture

awsm npm modules should have the following structure. Upon npm install yourmodule, JAWS will copy contents of the awsm folder into the serverless project's aws_modules folder on the npm postinstall hook.

module
    awsm.json
    package.json
    awsm
        lambda1
            awsm.json
            handler.js
            index.js
            event.json
    lib
        modularcode.js
/awsm.json

This contains module and authorship information for publishing your awsm on the upcoming aws-module registry.

/package.json

This contains module and authorship information for publishing your awsm as an npm module on their registry. Remember, awsm's use different package managers for delivery.

/awsm/

This contains starter scaffolding for lambda functions and endpoints. When someone installs your awsm via npm, the contents of this folder will be copied to their aws_modules folder, while the rest of your awsm will reside in their project's node_modules folder. As a best practice, don't put a lot of code in the scaffolding. Instead, focus on making your code modular and re-usable by putting the bulk of it in lib/yourcode.js.

/awsm/lambda1

Your awsm can have one or multiple lambda functions. Each lambda function resides in a sub-folder like this.

/awsm/lambda1/awsm.json

This contains configuration settings for this lambda and/or a URL endpoint that will be created via API Gateway. Lambda and API Gateway config information is stored in AWS CloudFormation syntax.

/awsm/lambda1/hander.js

This contains the handler which AWS Lambda will call. As a best practice, don't put your logic in here. Keep it separate so that it is re-usable, testable, and AWS independent.

/awsm/lambda1/index.js

This is your lambda's code, kept separate to be re-usable across modules, testable, and AWS independent. Please note that all of the dependencies in your awsm's package.json can only be required in code that is in /lib/. Do not require those dependencies in this index.js file or the handler.js file.

/awsm/lambda1/event.json

This is sample event information which you can use to test your Lambda function locally.

/lib/

When your awsm npm module is installed via JAWS, everything in the awsm folder will be copied to the project's aws_modules folder, but everything in this lib folder will be kept within the project's node_modules folder. Generally, code that should not be modified and code that needs to be required across all aws_modules in a serverless project should be put in this folder. Code that will be modified should be put in the awsm folder.

/lib/modularcode.js

This is code that should not be modified (like a normal npm module) and can be shared/required across all aws_modules in a serverless project. Please note that all dependencies that are in your awsm's package.json can only be required in code that is located in the lib folder.

Workflow

While you develop your module locally, it's useful to have a project to implement and test it in. With the following steps, you can install the module in your project, after which it can be tested.

Now your module is installed!

Please note that the module is not yet published to the NPM registry and therefore not yet added to your project's package.json. Read more about this in the NPM documentation on publishing NPM packages.

Ideas & Themes

So many things can be aws-modules. Here are a few popular themes for inspiration:

Registry

Want to tell the world about your aws module? Send a pull request against registry.json. This poor mans registry is just a temporary solution.