Sample Service - Details

The Overview walked through a simple “Hello World” example. In this section we’ll cover how Sparta works in preparation for moving on to more advanced usages. Most development will use the provision command line argument, so this section will outline exactly what that entails.

Provisioning Flow

The provisioning workflow is defined in provision.go, with a singular goal of encapsulating all AWS mutations into a CloudFormation template. Where CloudFormation does not support a given service, Sparta injects Lambda-backed Custom Resources into the template definition.

At a high level, provisioning uses the flow below. We’ll dive a bit deeper into each stage in the following sections.

graph TD iam[Verify Static IAM Roles] preBuild[WorkflowHook - PreBuild] style preBuild fill:#61B6EB,stroke:#000,stroke-width:4px; compile[Cross Compile for AWS AMI] cgoCompile[CGO Docker Compile for AWS AMI] postBuild[WorkflowHook - PostBuild] style postBuild fill:#61B6EB,stroke:#000,stroke-width:4px; nodeJSProxyShims[Create NodeJS-Go HTTP Proxies] pythonJSProxyShims[Create Python-CGO HTTP Proxies] package[ZIP archive] userArchive[WorkflowHook - Archive] style userArchive fill:#61B6EB,stroke:#000,stroke-width:4px; upload[Upload Archive to S3] packageAssets[Conditionally ZIP S3 Site Assets] uploadAssets[Upload S3 Assets] preMarshall[WorkflowHook - PreMarshall] style preMarshall fill:#61B6EB,stroke:#000,stroke-width:4px; generate[Marshal to CloudFormation] decorate[Call Lambda Decorators - Dynamic AWS Resources] serviceDecorator[Service Decorator] style serviceDecorator fill:#61B6EB,stroke:#000,stroke-width:4px; postMarshall[WorkflowHook - PostMarshall] style postMarshall fill:#61B6EB,stroke:#000,stroke-width:4px; uploadTemplate[Upload Template to S3] updateStack[Create/Update Stack] inplaceUpdates[In-place λ code updates] wait[Wait for Complete/Failure Result] iam-->preBuild preBuild-->|go|compile preBuild-->|cgo|cgoCompile compile-->postBuild cgoCompile-->postBuild postBuild-->|go|nodeJSProxyShims nodeJSProxyShims-->package nodeJSProxyShims-->packageAssets postBuild-->|cgo|pythonJSProxyShims pythonJSProxyShims-->package pythonJSProxyShims-->packageAssets package-->userArchive userArchive-->upload packageAssets-->uploadAssets uploadAssets-->generate upload-->generate generate-->preMarshall preMarshall-->decorate decorate-->serviceDecorator serviceDecorator-->postMarshall postMarshall-->uploadTemplate uploadTemplate-->|standard|updateStack uploadTemplate-->|inplace|inplaceUpdates updateStack-->wait
This diagram is rendered with Mermaid. Please open an issue if it doesn't render properly.

Verify Static IAM Roles

The NewLambda function accepts either a string or a sparta.IAMRoleDefinition value type. In the event that a string is passed, this function verifies that the IAM role exists and builds up a cache of IAM role information that can be shared and referenced during template generation. Specifically, a pre-existing IAM Role ARN is cached to minimize AWS calls during template generation.


The next step is to cross compile the application to a binary that can be executed on an AWS Lambda instance. The compile flags are:

  • TAGS: -tags lambdabinary

The binary is built in the current directory with a .lambda.amd64 suffix.


The end result of the package phase is a ZIP archive containing everything needed to deploy your service. This includes the NodeJS proxy entries that forward AWS Lambda requests to your Go binary HTTP-based handler. The packaging step also includes NodeJS code to support CloudFormation custom resources for services such as API Gateway configuration and S3 push source configuration. Finally, a known version of the AWS SDK is included (that’s the node-modules copying you’ll see if you enable debug verbosity) that’s likely newer than the one included in the AWS Lambda runtime.

Over time it’s expected that CloudFormation grows to support additional services and capabilities, at which point much of the NodeJS code can be eliminated.

Everything is ZIP’d up and ready for S3 upload.

Upload Archive To S3

Uploads the archive to S3. There’s not much else to see here.

Generate CloudFormation Template

Once the archive is uploaded and the S3 Item key is available, the CloudFormation template is generated by marshaling the sparta.LambdaAWSInfo objects into CloudFormation JSON representations.

The AWS Lambda marshaling is automatically handled. This is also the point at which the optional TemplateDecorator functions are called to annotate the automatically generated template with additional resources.

Upload CloudFormation Template to S3

Uploads the template to S3 to maximize template size. There’s not much else to see here.

Create/Update Stack

Finally, the provisioning workflow determines whether the Sparta serviceName exists and either creates or updates as appropriate.

Next Steps

Now that we’ve covered how Sparta handles provisioning your stack, we’re ready to expand functionality to leverge more of the AWS ecosystem in the next section.