Coder Social home page Coder Social logo

aidenpearce001 / aws-lambda-domain-model-sample Goto Github PK

View Code? Open in Web Editor NEW

This project forked from aws-samples/aws-lambda-domain-model-sample

1.0 0.0 0.0 448 KB

This is a sample project for implementing domain model objects on AWS Lambda function.

License: MIT No Attribution

Shell 3.71% Python 96.29%

aws-lambda-domain-model-sample's Introduction

Domain Model objects on AWS Lambda with Hexagonal Architecture Sample

[-Readme in Japanese-]

What is this project?

This project contains a Lambda function with domain model objects. By using Hexagonal Architecture (Ports and Adapters pattern), it separates domain model from other layer code.

The Hexagonal Architecture, or ports and adapters architecture, is an architectural pattern used in software design. The hexagonal architecture was invented by Dr. Alistair Cockburn.

Hexaglnal Architecture

The repository shows you how to implement your classes on the function. It includes sample domain models regarding a simple vaccination reservation system. With ports and adapters classes, domain model objects are loosely coupled from infrastructure code such as accessing to DynamoDB table.

This application also uses inversion of control concept to inject ports and adapters classes. It enables you to execute unit testing more easily because you can inject dummy instances into target classes. For more details, see sample unit testing code in this project (./tests/unit folder).

Class Diagram

Domain Models

Sequence diagram

Sequence diagram

Serverless Application Model

This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders.

  • src - Code for the application's Lambda function.
  • events - Invocation events that you can use to invoke the function.
  • tests/unit - Unit tests for the application code.
  • template.yaml - A template that defines the application's AWS resources.

Deploy the sample application

The Serverless Application Model Command-Line Interface (SAM CLI) extends the AWS CLI that adds functionality to building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API.

To use the SAM CLI, you need the following tools.

To build and deploy your application for the first time, run the following in your shell:

sam build --use-container
sam deploy --guided

The first command will build the source of your application. The second command will package and deploy your application to AWS, with a series of prompts:

You can find your API Gateway Endpoint URL in the output values displayed after deployment.

Prepare DynamoDB data before you execute this function

When you execute this function you need to execute data prepare script.

$ chmod +x setup/add_ddb_data.sh
$ setup/add_ddb_data.sh

Invoke a Lambda function throw Amazon API Gateway

You need to replace [Your api endpoint address] to SAM deploy output value.

$ curl -X POST -H "Content-Type:application/json" -d "{\"recipient_id\":\"1\", \"slot_id\":\"1\"}" [Your api endpoint address]

{"message": "The recipient's reservation is added."}

Use the SAM CLI to build and test locally

Build your application with the sam build --use-container command.

$ sam build --use-container

The SAM CLI installs dependencies defined in src/requirements.txt, creates a deployment package, and saves it in the .aws-sam/build folder.

Test a single function by invoking it directly with a test event. An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the events folder in this project.

Run functions locally and invoke them with the sam local invoke command.

$ sam local invoke ReservationFunction --event events/event.json

Tests

Tests are defined in the tests folder in this project. Use PIP to install the test dependencies and run tests.

$ pip install -r tests/requirements.txt --user
# unit test
$ python -m pytest tests/unit -v

Cleanup

To delete the sample application that you created, use the SAM CLI. Assuming you used your project name for the stack name, you can run the following:

sam delete --stack-name [Stack Name]

Resources

See the AWS SAM developer guide for an introduction to SAM specification, the SAM CLI, and serverless application concepts.

aws-lambda-domain-model-sample's People

Contributors

amazon-auto avatar fatsushi avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.