navigation

Serverless go microservices for AWS

Sparta is a framework that transforms a standard go application into a self-deploying AWS Lambda powered service. All configuration and infrastructure requirements are expressed as go types - no JSON or YAML needed!

Support Sparta

Help support continued Sparta development by becoming a Patreon patron!

Become a Patron!

Sample Application

1. Definition

// File: application.go
package main

import (
  sparta "github.com/mweagle/Sparta"
)

////////////////////////////////////////////////////////////////////////////////
// Hello world event handler
//
func helloWorld() (string, error) {
  return "Hello World 🌏", nil
}

////////////////////////////////////////////////////////////////////////////////
// Main
func main() {

  var lambdaFunctions []*sparta.LambdaAWSInfo
  lambdaFn, _ := sparta.NewAWSLambda("Hello world test",
    helloWorld,
    sparta.IAMRoleDefinition{})
  lambdaFunctions = append(lambdaFunctions, lambdaFn)

  // Delegate to Sparta
  sparta.Main("SpartaHelloWorld",
    "Simple Sparta application that creates a single AWS Lambda function",
    lambdaFunctions,
                nil,
                nil)
}

2. Deployment

$ go run main.go provision --s3Bucket $MY_S3_BUCKET
INFO[0000] ════════════════════════════════════════════════
INFO[0000] ╔═╗╔═╗╔═╗╦═╗╔╦╗╔═╗   Version : 1.8.0
INFO[0000] ╚═╗╠═╝╠═╣╠╦╝ ║ ╠═╣   SHA     : 597d3ba
INFO[0000] ╚═╝╩  ╩ ╩╩╚═ ╩ ╩ ╩   Go      : go1.11.1
INFO[0000] ════════════════════════════════════════════════
INFO[0000] Service: MyHelloWorldStack-mweagle            LinkFlags= Option=provision UTC="2018-12-22T15:23:56Z"
INFO[0000] ════════════════════════════════════════════════
INFO[0000] Using `git` SHA for StampedBuildID            Command="git rev-parse HEAD" SHA=b114e329ed37b532e1f7d2e727aa8211d9d5889c
INFO[0000] Provisioning service                          BuildID=b114e329ed37b532e1f7d2e727aa8211d9d5889c CodePipelineTrigger= InPlaceUpdates=false NOOP=false Tags=
INFO[0000] Verifying IAM Lambda execution roles
INFO[0000] IAM roles verified                            Count=1
INFO[0000] Checking S3 versioning                        Bucket=MY_S3_BUCKET VersioningEnabled=true
INFO[0000] Checking S3 region                            Bucket=MY_S3_BUCKET Region=us-west-2
INFO[0000] Running `go generate`
INFO[0000] Compiling binary                              Name=Sparta.lambda.amd64
INFO[0010] Creating code ZIP archive for upload          TempName=./.sparta/MyHelloWorldStack_mweagle-code.zip
INFO[0010] Lambda code archive size                      Size="18 MB"
INFO[0010] Uploading local file to S3                    Bucket=MY_S3_BUCKET Key=MyHelloWorldStack-mweagle/MyHelloWorldStack_mweagle-code.zip Path=./.sparta/MyHelloWorldStack_mweagle-code.zip Size="18 MB"
INFO[0016] Uploading local file to S3                    Bucket=MY_S3_BUCKET Key=MyHelloWorldStack-mweagle/MyHelloWorldStack_mweagle-cftemplate.json Path=./.sparta/MyHelloWorldStack_mweagle-cftemplate.json Size="2.2 kB"
INFO[0018] Creating stack                                StackID="arn:aws:cloudformation:us-west-2:123412341234:stack/MyHelloWorldStack-mweagle/a3671f60-05fd-11e9-b307-50a686be73f2"
INFO[0056] CloudFormation Metrics ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
INFO[0056]     Operation duration                        Duration=26.47s Resource=MyHelloWorldStack-mweagle Type="AWS::CloudFormation::Stack"
INFO[0056]     Operation duration                        Duration=17.33s Resource=IAMRolee41ad3d6e9c0dc2ca45a24ef8475a8fb6ad021e9 Type="AWS::IAM::Role"
INFO[0056]     Operation duration                        Duration=1.96s Resource=HelloWorldLambda80576f7b21690b0cb485a6b69c927aac972cd693 Type="AWS::Lambda::Function"
INFO[0056] Stack provisioned                             CreationTime="2018-12-22 15:24:13.845 +0000 UTC" StackId="arn:aws:cloudformation:us-west-2:123412341234:stack/MyHelloWorldStack-mweagle/a3671f60-05fd-11e9-b307-50a686be73f2" StackName=MyHelloWorldStack-mweagle
INFO[0056] ════════════════════════════════════════════════
INFO[0056] MyHelloWorldStack-mweagle Summary
INFO[0056] ════════════════════════════════════════════════
INFO[0056] Verifying IAM roles                           Duration (s)=0
INFO[0056] Verifying AWS preconditions                   Duration (s)=0
INFO[0056] Creating code bundle                          Duration (s)=10
INFO[0056] Uploading code                                Duration (s)=6
INFO[0056] Ensuring CloudFormation stack                 Duration (s)=40
INFO[0056] Total elapsed time                            Duration (s)=56

3. Invoke

Console GUI


Features

Unified

Use a go monorepo to define and your microservice's:

  • Application logic
  • AWS infrastructure
  • Operational metrics
  • Alert conditions
  • Security policies

Complete AWS Ecosystem

Sparta enables your lambda-based service to seamlessly integrate with the entire set of AWS lambda event sources such as:

  • DynamoDB
  • S3
  • Kinesis
  • SNS
  • SES
  • CloudWatch Events
  • CloudWatch Logs
Additionally, your service may provision any other CloudFormation supported resource and even your own CustomResources.

Security

Define IAM Roles with limited privileges to minimize your service's attack surface. Both string literal and ARN expressions are supported in order to reference dynamically created resources. Sparta treats POLA and #SecOps as first-class goals.

Discovery

A service may provision dynamic AWS infrastructure, and discover, at lambda execution time, the dependent resources' AWS-assigned outputs (Ref & Fn::Att). Eliminate hardcoded Magic ARNs from your codebase and move towards immutable infrastructure

API Gateway

Make your service HTTPS accessible by binding it to an API Gateway REST API during provisioning. As part of API Gateway creation, Sparta includes API Gateway Mapping Templates with all request data, including user-defined whitelisted parameters, so that you can focus on your core application logic.

Static Sites

Include a CORS-enabled S3-backed site with your service. S3-backed sites include API Gateway discovery information for turnkey deployment.


Sparta Overview

Sparta relies on CloudFormation to deploy and update your application. For resources that CloudFormation does not yet support, it uses Lambda-backed Custom Resources so that all service updates support both update and rollback semantics. Sparta’s automatically generated CloudFormation resources use content-based logical IDs whenever possible to preserve service availability and minimize resource churn during updates.

Getting Started

To get started using Sparta, begin with the Overview.

Administration

  • Problems? Please open an issue in GitHub.


Eveyone Welcome

Courtesy of gophers

Questions?

Get in touch via:

Other resources