There are two ways to create a Firehose Transform reactor that transforms a KinesisFirehoseEventRecord with a Lambda function:
NewKinesisFirehoseLambdaTransformer
NewKinesisFirehoseTransformer
import (
awsEvents "github.com/aws/aws-lambda-go/events"
spartaArchetype "github.com/mweagle/Sparta/v3/archetype"
)
// KinesisStream reactor function
func reactorFunc(ctx context.Context,
record *awsEvents.KinesisFirehoseEventRecord)
(*awsEvents.KinesisFirehoseResponseRecord, error) {
logger, _ := ctx.Value(sparta.ContextKeyRequestLogger).(*zerolog.Logger)
logger.Info().
Interface("Record", record).
Msg("Kinesis Firehose Event")
responseRecord = &awsEvents.KinesisFirehoseResponseRecord{
RecordID: record.RecordID,
Result: awsEvents.KinesisFirehoseTransformedStateOk,
Data: record.Data,
}
return responseRecord, nil
}
func main() {
// ...
handler := spartaArchetype.KinesisFirehoseReactorFunc(reactorFunc)
lambdaFn, lambdaFnErr := spartaArchetype.NewKinesisFirehoseLambdaTransformer(handler,
5*time.Minute /* Duration: recommended minimum of 1m */)
// ...
}
This is the lowest level transformation type supported and it enables the most flexibility.
Another option for creating Kinesis Firehose Transformers is to leverage the text/template package to define a transformation template. For instance:
{{/* file: transform.template */}}
{{if eq (.Record.Data.JMESPathAsString "sector") "TECHNOLOGY"}}
{
"region" : "{{ .Record.KinesisEventHeader.Region }}",
"ticker_symbol" : {{ .Record.Data.JMESPath "ticker_symbol"}}
}
{{else}}
{{ KinesisFirehoseDrop }}
{{end}}
A new *sparta.LambdaAWSInfo
instance can be created from transform.template as in:
func main() {
// ...
hooks := &sparta.WorkflowHooks{}
reactorFunc, reactorFuncErr := archetype.NewKinesisFirehoseTransformer("transform.template",
5*time.Minute,
hooks)
// ...
var lambdaFunctions []*sparta.LambdaAWSInfo
lambdaFunctions = append(lambdaFunctions, reactorFunc)
err := sparta.MainEx(awsName,
"Simple Sparta application that demonstrates core functionality",
lambdaFunctions,
nil,
nil,
hooks,
false)
}
The template execution context includes the the following:
string
)
JMESPath
, JMESPathAsString
, JMESPathAsFormattedString
) or regexp capture groups (RegExpGroup
, RegExpGroupAsString
, RegExpGroupAsJSON
).string
)
struct
)
awsEvents.MilliSecondsEpochTime
)
struct
)
Functions available in the template’s FuncMap include:
KinesisFirehoseDrop
: indicates that the record should be marked as KinesisFirehoseTransformedStateDropped