Application Customization

Sparta-based applications use the Cobra package to expose a rich set of command line options. This section describes:

  • Custom Commands
  • In addition to custom flags, an application may register completely new commands. For example, to support alternative topologies or integrated automated acceptance tests as part of a CI/CD pipeline. To register a custom command, define a new cobra.Command and add it to the sparta.CommandLineOptions.Root command value. Ensure you use the xxxxE Cobra functions so that errors can be properly propagated. httpServerCommand := &cobra.Command{ Use: "httpServer", Short: "Sample HelloWorld HTTP server", Long: `Sample HelloWorld HTTP server that binds to port: ` + HTTPServerPort, RunE: func(cmd *cobra.

  • Custom Flags
  • Some commands (eg: provision) may require additional options. For instance, your application’s provision logic may require VPC subnets or EC2 SSH Key Names. The default Sparta command line option flags may be extended and validated by building on the exposed Cobra command objects. Adding Flags To add a flag, use one of the pflag functions to register your custom flag with one of the standard CommandLineOption values. For example: // SSHKeyName is the SSH KeyName to use when provisioning new EC2 instance var SSHKeyName string func main() { // And add the SSHKeyName option to the provision step sparta.

  • Managing Environments
  • It’s common for a single Sparta application to target multiple environments. For example: Development Staging Production Each environment is largely similar, but the application may need slightly different configuration in each context. To support this, Sparta uses Go’s conditional compilation support to ensure that configuration information is validated at build time. Conditional compilation is supported via the --tags/-t command line argument. This example will work through the SpartaConfig sample.

  • CloudFormation Resources
  • In addition to per-lambda custom resources, a service may benefit from the ability to include a service-scoped Lambda backed CustomResource. Including a custom service scoped resource is a multi-step process. The code excerpts below are from the SpartaCustomResource sample application. 1. Resource Type The first step is to define a custom CloudFormation Resource Type //////////////////////////////////////////////////////////////////////////////// // 1 - Define the custom type const spartaHelloWorldResourceType = "Custom::sparta::HelloWorldResource" 2. Request Parameters The next step is to define the parameters that are supplied to the custom resource invocation.

  • Custom Resources
  • In some circumstances your service may need to provision or access resources that fall outside the standard workflow. In this case you can use CloudFormation Lambda-backed CustomResources to create or access resources during your CloudFormation stack’s lifecycle. Sparta provides unchecked access to the CloudFormation resource lifecycle via the RequireCustomResource function. This function registers an AWS Lambda Function as an CloudFormation custom resource lifecycle. In this section we’ll walk through a sample user-defined custom resource and discuss how a custom resource’s outputs can be propagated to an application-level Sparta lambda function.

Adding custom flags or commands is typically a prerequisite to supporting alternative topologies.