To build a Sparta application, follow these steps:
go get -u -v https://github.com/mweagle/Sparta/...
Configure your AWS Credentials according to the go SDK docs. The most reliable approach is to use environment variables as in:
$ env | grep AWS
AWS_DEFAULT_REGION=us-xxxx-x
AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
AWS_REGION=us-xxxx-x
AWS_ACCESS_KEY_ID=xxxxxxxxxxxxxxxxxxxx
Create a sample main.go file as in:
package main
import (
"context"
"fmt"
"os"
"github.com/aws/aws-sdk-go-v2/aws/session"
sparta "github.com/mweagle/Sparta/v3"
spartaCF "github.com/mweagle/Sparta/v3/aws/cloudformation"
"github.com/rs/zerolog"
)
// Standard AWS λ function
func helloWorld(ctx context.Context) (string, error) {
logger, loggerOk := ctx.Value(sparta.ContextKeyLogger).(*zerolog.Logger)
if loggerOk {
logger.Info("Accessing structured logger 🙌")
}
return "Hello World 👋. Welcome to AWS Lambda! 🙌🎉🍾", nil
}
////////////////////////////////////////////////////////////////////////////////
// Main
func main() {
lambdaFn, _ := sparta.NewAWSLambda("Hello World",
helloWorld,
sparta.IAMRoleDefinition{})
sess := session.Must(session.NewSession())
awsName, awsNameErr := spartaCF.UserAccountScopedStackName("MyHelloWorldStack",
sess)
if awsNameErr != nil {
fmt.Print("Failed to create stack name\n")
os.Exit(1)
}
var lambdaFunctions []*sparta.LambdaAWSInfo
lambdaFunctions = append(lambdaFunctions, lambdaFn)
err := sparta.Main(awsName,
"Simple Sparta HelloWorld application",
lambdaFunctions,
nil,
nil)
if err != nil {
os.Exit(1)
}
}
Build with go run main.go provision --s3Bucket YOUR_S3_BUCKET_NAME
where YOUR_S3_BUCKET_NAME
is an S3 bucket to which your account has write privileges.
The following Example Service section provides more details regarding how Sparta transforms your application into a self-deploying service.