aws-lambda-billing
This example shows simplified billing system in serverless architecture.
Comprehensive guide describing exactly the architecture, applied design patterns and technologies can be found on our blog in article Simplified billing system with AWS Lambda. Comparision: Kotlin + Micronaut vs Kotlin + Dagger vs vanilla Kotlin.
We encourage you to read, because in this README there is only a substitute for all information.
User uploads CSV file (with name structure
CLIENTCODE_YEAR_MONTH_activeList.txt) with Beneficiaries (the sample file is located in thedata-examplesfolder) to a specific data storage -asc-lab-serverless-inputS3 Bucket.The above action triggers a function (
GenerateBillingItemsFunc) that is responsible for:- generating billing items (using prices from an external database -
PriceListtable inDynamoDB) and saving them in the tableBillingItem; - sending message about the need to create a new invoice to
invoice-generation-request-queue;
- generating billing items (using prices from an external database -
When a new message appears on the
invoice-generation-request-queue, next function is triggered (GenerateInvoiceFunc). This function creates domain objectInvoiceand save this object in database (DynamoDB Invoicestable) and send message to queues:invoice-print-request-queueandinvoice-notification-request-queue.When a new message appears on the
invoice-print-request-queue, functionPrintInvoiceFuncis triggered. This function uses external engine to PDF generation -JsReportand saves PDF file inasc-lab-serverless-printoutS3 Bucket.When a new message appears on the
invoice-notification-request-queue, functionNotifyInvoiceFuncis triggered. This function uses two external systems -SendGridto email sending andTwilioto SMS sending.
Preparation
Deployment requires AWS CLI and Terraform
Before first deployment you need to initialize Terraform with
init.sh
Also you need to create some external accounts and informations
- jsreportonline (https://jsreportonline.net/) - just username and password
- SendGrid (https://sendgrid.com/) - API key
- Twilio (https://www.twilio.com/) - Account SID and Auth Token
As a last step, you need to create variables file to give all the above information to Terraform. Create file billing.tfvars in main folder insides should looks like this:
jsreport_url = "https://some_user.jsreportonline.net/api/report"
jsreport_username = "some_user"
jsreport_password = "some_password"
sendgrid_api_key = "SG.rePNAA_AAAAaAA1aAAAi6w.r5AAAUZhyaaaUo7w0030MFAauAaTWvXw-n11mpAaqe0"
twilio_account_sid = "ACd111feb1c11f11111111cd1dd1111c11"
twilio_auth_token = "1ed1a11111b1af1111b111d1111dd11d"
Building and Deployment
Terraform should create all necessary infrastructure (S3 bucket, SQS Queues, DynamoDB Tables (with data)) and deploy lambdas code.
buildAndDeploy.sh
Testing
You should see S3 Bucket called asc-lab-serverless-input. Put there sample file and enjoy logs in AWS CloudWatch.
Disabling
SQS triggers can actively check queues for new messages. If you don't want to exceed your free tier, you can disable them using our Terraform configuration. Just run:
terraform apply -auto-approve -var-file="billing.tfvars" -var "active=false" terraform
To re-enable triggers run:
terraform apply -auto-approve -var-file="billing.tfvars" -var "active=true" terraform
