sls

package module
v0.4.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 30, 2023 License: MIT Imports: 21 Imported by: 0

README

sls

Golang package to handle all things AWS with a focus on serverless computing.

Documentation

Overview

Package sls exposes behavior and types that are used to manage day to day development, deployment, inspecting and testing of serverless applications running is AWS

Index

Constants

View Source
const (
	GoBinaryName   = "go"
	GoBuildCmdName = "build"
	DefaultLDFlags = `-ldflags="-s -w -X main.Version=$(git rev-parse HEAD)"`
)
View Source
const (
	UsEast1      = Region("us-east-1")
	UsEast2      = Region("us-east-2")
	UsWest1      = Region("us-west-1")
	UsWest2      = Region("us-west-2")
	AfSouth1     = Region("af-south-1")
	ApEast1      = Region("ap-east-1")
	ApSouth1     = Region("ap-south-1")
	ApNortheast1 = Region("ap-northeast-1")
	ApNortheast2 = Region("ap-northeast-2")
	ApNortheast3 = Region("ap-northeast-3")
	ApSoutheast1 = Region("ap-southeast-1")
	ApSoutheast2 = Region("ap-southeast-2")
	CaCentral1   = Region("ca-central-1")
	EuCentral1   = Region("eu-central-1")
	EuWest1      = Region("eu-west-1")
	EuWest2      = Region("eu-west-2")
	EuWest3      = Region("eu-west-3")
	EuSouth1     = Region("eu-south-1")
	EuNorth1     = Region("eu-north-1")
	MeSouth1     = Region("me-south-1")
	SaEast1      = Region("sa-east-1")
	UsGovEast1   = Region("us-gov-east-1")
	UsGovWest1   = Region("us-gov-west-1")
)
View Source
const (
	APIGWProxyTrigger      = InvokeTrigger("apigw")
	APIGWCustomAuthTrigger = InvokeTrigger("apigw-auth")
	AppSyncTrigger         = InvokeTrigger("appsync")
	CloudWatchEventTrigger = InvokeTrigger("cw-event")
	CloudWatchLogsTrigger  = InvokeTrigger("cw-log")
	CognitoTrigger         = InvokeTrigger("cognito")
	DDBTrigger             = InvokeTrigger("ddb")
	DDBStreamTrigger       = InvokeTrigger("ddb-stream")
	DirectTrigger          = InvokeTrigger("direct")
	KinesisStreamTrigger   = InvokeTrigger("kinesis-stream")
	SNSTrigger             = InvokeTrigger("sns")
	SQSTrigger             = InvokeTrigger("sqs")
	S3Trigger              = InvokeTrigger("s3")
	StepTrigger            = InvokeTrigger("sfn")
)
View Source
const (
	DefaultOutputName          = "bootstrap"
	DefaultBinaryZipName       = "deployment.zip"
	DefaultInfraDir            = "infra/local"
	DefaultTerraform           = "terraform"
	DefaultLambdasDir          = "app/lambdas"
	DefaultBuildDir            = "/tmp"
	DefaultRepoRefName         = "main"
	DefaultLambdaInvokeType    = "RequestResponse"
	DefaultLambdaInvokeLogType = "Tail"
	DefaultLambdaGoFile        = "main.go"
	DefaultAppDirName          = "app"
	DefaultLambdaDirName       = "lambdas"
	DefaultInfraDirName        = "infra"
	DefaultBuildDirName        = "build"
	DefaultTerraformDirName    = "terraform"

	LocalTerraformRepoName = "local-terraform"
	GithubURL              = "github.com"
	TerraformName          = "terraform"
	SSHProtocol            = Protocol("ssh")
	HTTPSProtocol          = Protocol("https")
	CLIProtocol            = Protocol("cmds")
	CLITemplate            = "%s/%s"
	SSHTemplate            = "git@%s:%s/%s.git"
	HTTPSTemplate          = "https://%s/%s/%s.git"

	RemoteStateTF  = "remote-state"
	LambdaDeployTF = "lambda-deploy-bucket"
	KeyPairTF      = "key-pair"
	MessagingTF    = "messaging"
	CognitoTF      = "cognito"
	NetworkingTF   = "networking"
)
View Source
const (
	DefaultFeatureTimeout = 100
)

Variables

View Source
var (
	APIGWProxyEvent      = reflect.TypeOf(events.APIGatewayProxyRequest{})
	APIGWCustomAuthEvent = reflect.TypeOf(events.APIGatewayCustomAuthorizerRequest{})
	CloudWatchEvent      = reflect.TypeOf(events.CloudWatchEvent{})
	CloudWatchLogsEvent  = reflect.TypeOf(events.CloudwatchLogsEvent{})
	DDBEvent             = reflect.TypeOf(events.DynamoDBEvent{})
	DirectEvent          = reflect.TypeOf([]byte{})
	SNSEvent             = reflect.TypeOf(events.SNSEvent{})
	SQSEvent             = reflect.TypeOf(events.SQSEvent{})
	S3Event              = reflect.TypeOf(events.S3Event{})
)
View Source
var DefaultRegion = Region("us-east-1")

Functions

func BoolPtr added in v0.4.8

func BoolPtr(v bool) *bool

func GetTraceID added in v0.2.0

func GetTraceID(ctx context.Context) string

GetTraceID gets the trace id from the context. The value is in the format of "Root=1-5abc5ca4-f07ab5d0a2c2b2f0730acb08;Parent=200406d9510e71a3;Sampled=0" and the root trace id is parsed out and returned.

func InvocationLogger added in v0.2.0

func InvocationLogger(ctx context.Context, l *zap.SugaredLogger, invType InvokeTrigger) *zap.SugaredLogger

InvocationLogger gets a logger with fields initialized for a particular invocation.

func NewDefaultConfig added in v0.2.0

func NewDefaultConfig(opts ...context.Context) (aws.Config, error)

NewDefaultConfig will return an aws.Config struct that can be used to configure most sdk clients. If no context is given this will create one

func NewDefaultConfigMust added in v0.2.0

func NewDefaultConfigMust(opts ...context.Context) aws.Config

NewDefaultConfigMust is the same as its cousin accept it will panic if it fails. This is useful in setup scenarios where you can not return any errors

func NewDefaultConfigWithConf added in v0.2.0

func NewDefaultConfigWithConf(c AWSConfig, opts ...context.Context) (aws.Config, error)

NewDefaultConfigWithConf will use the config.LoadDefaultConfig with various different options set by the conf package. An Empty struct will give you the default config.

func NewDefaultConfigWithProfile added in v0.2.0

func NewDefaultConfigWithProfile(profile string, opts ...context.Context) (aws.Config, error)

NewDefaultConfigWithProfile just adds an aws profile to the configuration.

func NewDefaultConfigWithRegion added in v0.2.0

func NewDefaultConfigWithRegion(reg string, opts ...context.Context) (aws.Config, error)

NewDefaultConfigWithRegion just adds a aws region to the configuration. NOTE: the region is validated and will fail if not a proper aws region

func NewGoBuildCmd

func NewGoBuildCmd(buildDir, binaryName, targetDir string) (*exec.Cmd, error)

NewGoBuildCmd is designed to compile golang source code used for AWS Lambdas, for us these are features in our microservices.

	buildDir   - directory the binary will be compiled to
 binaryName - is the name of the binary, this is used in the -o flag
 targetDir  - is the directory which contains the source code to build

func RegionCode

func RegionCode(region string) string

RegionCode takes an AWS region like us-east-1 and compresses into a smaller code like (us-east-1) -> (use1) which is used in resource naming

func Zip

func Zip(zf, binary string) error

Types

type AWSAccount added in v0.2.0

type AWSAccount struct {
	Region  Region
	Profile string
}

AWSAccount hold the profile used and the default region. We can use this to set ENV vars before running our code.

type AWSConfig added in v0.2.0

type AWSConfig struct {
	Profile string `conf:"no-prefix, global-flag, 									 env:AWS_PROFILE,  cli:aws-profile, cli-u: aws profile"`
	Region  string `conf:"no-prefix, global-flag, default:us-east-1, env:AWS_REGION,	 cli:aws-region,  cli-u: aws region (default us-east-1)"`
}

AWSConfig is a configuration struct using the conf annotated tags which allows you to use it to configure cli systems or lambdas from env or cli inputs

func (AWSConfig) ToRegion added in v0.2.0

func (c AWSConfig) ToRegion() (Region, error)

type BuildResult added in v0.2.0

type BuildResult struct {
	Settings BuildSettings
	ZipName  string
	ZipData  []byte
}

type BuildSettings added in v0.2.0

type BuildSettings struct {
	CodeDir     string
	BuildDir    string
	BinName     string
	BinPath     string
	SkipZipping bool
	ZipName     string
}

type CodeLayout added in v0.2.0

type CodeLayout struct {
	Root      string
	Lambdas   string
	CLI       string
	Infra     string
	Build     string
	Terraform string
}

func DefaultCodeLayout added in v0.2.0

func DefaultCodeLayout(root, cliPath string) CodeLayout

func (CodeLayout) BuildDir added in v0.2.0

func (cl CodeLayout) BuildDir() string

func (CodeLayout) CLIDir added in v0.2.0

func (cl CodeLayout) CLIDir() string

func (CodeLayout) InfraDir added in v0.2.0

func (cl CodeLayout) InfraDir() string

func (CodeLayout) LambdasDir added in v0.2.0

func (cl CodeLayout) LambdasDir() string

func (CodeLayout) RootDir added in v0.2.0

func (cl CodeLayout) RootDir() string

func (CodeLayout) TerraformDir added in v0.2.0

func (cl CodeLayout) TerraformDir() string

func (CodeLayout) TriggerDir added in v0.2.0

func (cl CodeLayout) TriggerDir(et InvokeTrigger) string

type CompileResult added in v0.2.0

type CompileResult struct {
	BuildDir   string
	BinaryName string
	BinaryPath string
	CodeDir    string
}

type ConcreteHandlerFn added in v0.2.0

type ConcreteHandlerFn func() (out interface{}, err error)

type Configurable added in v0.2.0

type Configurable interface {
	ProcessEnv() error
	ProcessCLI(cmd *cobra.Command, v *viper.Viper) error
	CollectParamsFromEnv(appTitle string) (map[string]string, error)
	ParamNames(appTitle string) ([]string, error)
	EnvNames() ([]string, error)
	EnvToMap() (map[string]string, error)
	SetPrefix(prefix string)
	GetPrefix() string
	IsPrefixEnabled() bool
	MarkDefaultsAsExcluded()
	MarkDefaultsAsIncluded()
	SetExcludeDefaults(value bool)
	IsDefaultsExcluded() bool
}

type Feature added in v0.2.0

type Feature struct {
	Name          string
	QualifiedName string
	Trigger       InvokeTrigger
	BinaryName    string
	BinaryZipName string
	Conf          Configurable
	Env           map[string]string
}

func (Feature) AddEnv added in v0.2.0

func (l Feature) AddEnv(name, value string)

func (Feature) CodeDir added in v0.2.0

func (l Feature) CodeDir() string

func (Feature) NameWithTrigger added in v0.2.0

func (l Feature) NameWithTrigger() string

func (Feature) String added in v0.2.0

func (l Feature) String() string

func (Feature) TriggerDir added in v0.2.0

func (l Feature) TriggerDir() string

type FeatureDeployment added in v0.2.0

type FeatureDeployment struct {
	Name        string
	ServiceName string
	CodeDir     *string
	BuildDir    *string
	BinName     *string
	ZipName     *string
	IsEnvOnly   bool
	Lambda      Feature
}

type Features added in v0.2.0

type Features map[string]Feature

type GlobalResources added in v0.2.0

type GlobalResources struct {
	RemoteState TFResource
	Repo        Repo
	RootDir     string
	Config      map[string]TFResource
}

func (GlobalResources) Cognito added in v0.2.0

func (gr GlobalResources) Cognito() (TFResource, bool)

func (GlobalResources) KeyPair added in v0.2.0

func (gr GlobalResources) KeyPair() (TFResource, bool)

func (GlobalResources) LambdaDeployBucket added in v0.2.0

func (gr GlobalResources) LambdaDeployBucket() (TFResource, bool)

func (GlobalResources) Messaging added in v0.2.0

func (gr GlobalResources) Messaging() (TFResource, bool)

func (GlobalResources) Networking added in v0.2.0

func (gr GlobalResources) Networking() (TFResource, bool)

type InvokeTrigger added in v0.2.0

type InvokeTrigger string

func InvokeTriggerFromEvent added in v0.2.0

func InvokeTriggerFromEvent(t reflect.Type) (InvokeTrigger, error)

func InvokeTriggerFromString added in v0.2.0

func InvokeTriggerFromString(s string) (InvokeTrigger, error)

func (InvokeTrigger) IsEmpty added in v0.2.0

func (lt InvokeTrigger) IsEmpty() bool

func (InvokeTrigger) String added in v0.2.0

func (lt InvokeTrigger) String() string

type KeyPair added in v0.2.0

type KeyPair struct {
	Name      string
	PublicKey string
}

type MicroService added in v0.2.0

type MicroService struct {
	CodeLayout
	Resource TFResource
	Account  AWSAccount
	Name     ServiceName
	Repo     Repo
	Features map[string]Feature
}

Inputs for microservices

1) root directory - absolute path to the microservice codebase 2) repo - GitHub repository information used to checkout the code base 3) app title - the base name in our AWS resource naming convention for microservices 4) cli title - the name of microservice's cli binary used to manage this cli 5) env - name of the aws environment the microservice will run in 6) region - default aws region when managing aws resources through the sdk 7) profile - the aws profile used by system managing the environment. used for creds

func NewMicroService added in v0.2.0

func NewMicroService(in MicroServiceIn) (*MicroService, error)

func (*MicroService) AddByTrigger added in v0.2.0

func (s *MicroService) AddByTrigger(et InvokeTrigger) error

func (*MicroService) AddFeature added in v0.2.0

func (s *MicroService) AddFeature(et InvokeTrigger, title string) error

func (*MicroService) BuildFeature added in v0.2.0

func (s *MicroService) BuildFeature(feature Feature, codeDir ...string) (*CompileResult, error)

func (*MicroService) BuildFeatureCode added in v0.2.0

func (s *MicroService) BuildFeatureCode(buildDir, binaryName, sourceDir string) (*CompileResult, error)

func (*MicroService) Feature added in v0.2.0

func (s *MicroService) Feature(title string) (Feature, error)

func (*MicroService) GoPath added in v0.2.0

func (s *MicroService) GoPath() (string, error)

func (*MicroService) LoadFeaturesFromFilesystem added in v0.2.0

func (s *MicroService) LoadFeaturesFromFilesystem() error

func (*MicroService) NewBuildSettings added in v0.2.0

func (s *MicroService) NewBuildSettings(feature Feature) BuildSettings

func (*MicroService) String added in v0.2.0

func (s *MicroService) String() string

func (*MicroService) UpdateEnvWithPStoreCmd added in v0.2.0

func (s *MicroService) UpdateEnvWithPStoreCmd(feature string) (*exec.Cmd, error)

type MicroServiceIn added in v0.2.0

type MicroServiceIn struct {
	RootDir      string
	Region       string
	Env          string
	RepoOwner    string
	Repo         string
	RepoRef      string
	IsRepoBranch bool
	App          string
	CLI          string
}

type MockTimeout added in v0.2.0

type MockTimeout struct {
	ReturnFromFn bool
	Out          interface{}
	Err          error
}

func (*MockTimeout) WithTimeConstraint added in v0.2.0

func (m *MockTimeout) WithTimeConstraint(_ context.Context, fn ConcreteHandlerFn) (out interface{}, err error)

type Prefix

type Prefix struct {
	Region  Region
	EnvName string
}

Prefix represents our naming prefix, used when creating resources with terraform. Every fender resource has a prefix to encode information about it. The fender prefix is laid out as follows: <region_code>-<env> region_code - this is the aws region code so `use1 [us-east-1]` env - the environment this resource is provisioned in. `[prod,qa,active,<developer_code>]`

func DefaultPrefix added in v0.2.0

func DefaultPrefix(env string) (Prefix, error)

func NewPrefix

func NewPrefix(reg, env string) (Prefix, error)

func (Prefix) AWSRegion added in v0.2.0

func (p Prefix) AWSRegion() string

func (Prefix) Env

func (p Prefix) Env() string

func (Prefix) IsValid added in v0.2.0

func (p Prefix) IsValid() bool

func (Prefix) RegionCode

func (p Prefix) RegionCode() string

func (Prefix) String

func (p Prefix) String() string

type Protocol added in v0.2.0

type Protocol string

func (Protocol) IsEmpty added in v0.2.0

func (p Protocol) IsEmpty() bool

func (Protocol) String added in v0.2.0

func (p Protocol) String() string

type Region

type Region string

Region represents the AWS region identifier like us-east-1

func ToRegion

func ToRegion(region string) (Region, error)

func (Region) Code

func (r Region) Code() string

func (Region) IsEmpty added in v0.2.0

func (r Region) IsEmpty() bool

func (Region) String

func (r Region) String() string

type RegionConfig added in v0.2.0

type RegionConfig Region

type Repo added in v0.2.0

type Repo struct {
	Name            string
	Owner           string
	IsBranch        bool
	RefName         string
	DefaultProtocol Protocol
}

func NewRepo added in v0.2.0

func NewRepo(owner, name, refName string, isBranch bool) Repo

func (Repo) URI added in v0.2.0

func (r Repo) URI(p ...Protocol) string

type ServiceName added in v0.2.0

type ServiceName struct {
	Prefix
	Title string
}

ServiceName is used for fender microservices. These are a repository of lambdas and as such the Microservice is not a physical aws resource but rather a collection of resources which include lambdas, apigw, sns queues, dynamodb etc... This name would act as the Prefix for those physical resources

func NewServiceName added in v0.2.0

func NewServiceName(env string, title string) (ServiceName, error)

func (ServiceName) AppTitle added in v0.2.0

func (sn ServiceName) AppTitle() string

func (ServiceName) QualifiedName added in v0.2.0

func (sn ServiceName) QualifiedName() string

func (ServiceName) String added in v0.2.0

func (sn ServiceName) String() string

type TFBackend added in v0.2.0

type TFBackend struct {
	Bucket      string
	Key         string
	Region      Region
	DynamoTable string
}

func NewTFBackend added in v0.2.0

func NewTFBackend(prefix Prefix, label string) TFBackend

type TFResource added in v0.2.0

type TFResource struct {
	Dir       string
	StateFile string
	PlanFile  string
	Name      string
	Backend   TFBackend
	Vars      map[string]string
}

func NewTFResource added in v0.2.0

func NewTFResource(rootDir string, prefix Prefix, label string) TFResource

func (TFResource) IsBackend added in v0.2.0

func (tr TFResource) IsBackend() bool

type Terraform added in v0.2.0

type Terraform struct {
	BinaryDir       string
	BinaryName      string
	Version         string
	GlobalResources GlobalResources
}

Terraform stands for Terraform it holds the path to terraform binary as well as the Repo for global resources to be provisioned into the developer local aws account. Dir is where we will clone our global resource's repo.

type Timeout added in v0.2.0

type Timeout struct {
	// contains filtered or unexported fields
}

func NewTimeout added in v0.2.0

func NewTimeout(config TimeoutConfig) *Timeout

func (*Timeout) WithTimeConstraint added in v0.2.0

func (t *Timeout) WithTimeConstraint(ctx context.Context, fn ConcreteHandlerFn) (out interface{}, err error)

type TimeoutCapturing added in v0.2.0

type TimeoutCapturing interface {
	WithTimeConstraint(ctx context.Context, fn ConcreteHandlerFn) (out interface{}, err error)
}

type TimeoutConfig added in v0.2.0

type TimeoutConfig struct {
	Period time.Duration `conf:"env:SLS_FEATURE_HANDLER_TIMEOUT, default:100ms"`
}

type VCS added in v0.2.0

type VCS struct {
	URL   string
	Owner string
}

VCS hold info about our version control system. In our case this is git/GitHub

Directories

Path Synopsis
Package cog is responsible for providing an aws cognito client as well as lambda handling front controller pattern for all microservice lambda features that need logging, timeout and panic handling.
Package cog is responsible for providing an aws cognito client as well as lambda handling front controller pattern for all microservice lambda features that need logging, timeout and panic handling.
Package dynamo implements various design patterns for dynamodb and looks to simplify the aws api for aws-sdk-go-v2
Package dynamo implements various design patterns for dynamodb and looks to simplify the aws api for aws-sdk-go-v2
Package infra is responsible for infrastructure related concerns of the app like deploying and configuration
Package infra is responsible for infrastructure related concerns of the app like deploying and configuration
Package pstore implements a parameter store client used specifically to managing configuration data for microservices
Package pstore implements a parameter store client used specifically to managing configuration data for microservices
Package security is used to create key-pairs and tunnel database connections in the local aws env for development
Package security is used to create key-pairs and tunnel database connections in the local aws env for development

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL