Giter VIP home page Giter VIP logo

aws-saas-factory-billing-and-metering-reference-implementation's Introduction

Reference Billing/Metering Service

This project contains a reference implementation for a serverless billing/metering service.

Requirements

Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

Building

Build the SAM application by running the following command:

$ sam build

You can ignore the warnings about the JVM using a major version higher than 11 if you are using a Java compiler version higher than 11. The pom file tells the compiler to build for a version of Java that is compatible with Lambda.

Testing

There is Maven lifecycle that defines tests of major functionality locally. In order to test the OnboardNewTenant, ProcessBillingEvent, BillingEventAggregation and StripeBillingPublish, the DynamoDB Local container needs to be running in Docker and listening on port 8000:

$ docker run -p 8000:8000 amazon/dynamodb-local:latest

For the StripeBillingPublish test, the stripe-mock container needs to be run according to the Docker usage instructions here

The tests for TenantConfiguration and Constants do not require any external dependencies.

Deploying

In order to deploy the application, run the following command:

$ sam deploy --guided

Initial Configuration

Tenant Configuration

Tenants are configured in two places: within Stripe and within this application. Tenant configuration in Stripe must happen before configuring a tenant in this application. There are several components that need to be created within Stripe:

The connection between the String Customer and Stripe Subscription creates the subscription item ID, an identifier prefixed with "si_". Please refer to the Stripe documentation for more information about how to create these resources.

Once the subscription item ID exists in Stripe, the tenant can be onboarded into this application. This is done through an event on the associated EventBridge. See the "Usage and Function" section below.

Stripe API Key

Do not use a production API key.

The Stripe API key is stored within Secrets Manager. The Cloudformation stack creates an empty secret. Access the secret through the Secrets Manager console and paste in a restricted testing API key with the following permissions:

  • Invoices - Read
  • Subscriptions - Read
  • Usage Records - Write

More information about how to create a restricted key can be found here.

Do not paste it in JSON format; the application expects to find a single string with the API key.

Do not use a production API key.

Usage and Function

First, tenants need to be onboarded to this system. This is done by placing an event onto the EventBridge. The event is in the following format:

{
  "TenantID": "Tenant0",
  "ExternalSubscriptionIdentifier": "si_00000000"
}

Where "TenantID" is some way to identify a tenant, and where "ExternalSubscriptionIdentifier" is the identifier associated with the subscription in the billing provider. In the case of Stripe Billing, this identifier will be the subscription ID, prefixed with "si_".

This can be placed onto the EventBridge with the AWS CLI or with one of the AWS SDKs. Here is an example using the AWS CLI:

$ aws events put-events --entries file://exampleOnboardingEvent.json

The contents of exampleOnboardingEvent.json should be similar to the following:

[
  {
    "Detail": "{ \"TenantID\": \"Tenant0\", \"ExternalSubscriptionIdentifier\": \"si_00000000\" }",
    "DetailType": "ONBOARD",
    "EventBusName": "BillingEventBridge",
    "Source": "command-line-test"
  }
]

Where the value of the EventBusName key is the name of the EventBridge associated with the SAM application. This is BillingEventBridge by default.

Once a tenant is onboarded, place billing events onto the same EventBridge. The event is in the following format:

{ 
  "TenantID": "Tenant0",
  "Quantity": 5
}

The billing event is in the following format:

  • TenantID: the ID of the tenant that owns the billing event
  • Quantity: the number of billing events that occurred

This can be placed onto the EventBridge with the AWS CLI or with one of the AWS SDKs. Here is an example using the AWS CLI:

$ aws events put-events --entries file://exampleBillingEvents.json

The contents of exampleBillingEvents.json should be similar to the following:

[
  {
    "Detail": "{ \"TenantID\": \"Tenant0\", \"Quantity\": 5 }",
    "DetailType": "BILLING",
    "EventBusName": "BillingEventBridge",
    "Source": "command-line-test"
  },
  {
    "Detail": "{ \"TenantID\": \"Tenant0\", \"Quantity\": 10 }",
    "DetailType": "BILLING",
    "EventBusName": "BillingEventBridge",
    "Source": "command-line-test"
  }
]

After placing the event onto the EventBridge, a Lambda function processes the event and places it into a DynamoDB table.

Where the value of the EventBusName key is the name of the EventBridge associated with the SAM application. This is BillingEventBridge by default.

At a rate set by the user of the application, a Cloudwatch Scheduled Event runs a Step Function State Machine that aggregates the events in the DynamoDB table and publishes them to Stripe Billing.

aws-saas-factory-billing-and-metering-reference-implementation's People

Contributors

amazon-auto avatar keithaws avatar

Watchers

 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.