Implements: Container, ILambdaFunctionOverrides
Description
The LambdaFunction class allows you to create an abstract AWS Lambda function that acts as a container to instantiate and run components, and expose them via an external entry point.
Important points
-
When handling calls the “cmd” parameter determines what action shall be called, while the other parameters are passed to the action itself.
-
Container configuration for this Lambda function is stored in "./config/config.yml" file. But this path can be overriden by CONFIG_PATH environment variable.
Configuration parameters
- dependencies:
- controller: override for Controller dependency
- connections:
- discovery_key: (optional) key to retrieve the connection from IDiscovery
- region: (optional) AWS region
- credentials:
- store_key: (optional) key to retrieve the credentials from ICredentialStore
- access_id: AWS access/client id
- access_key: AWS access/client key
References
- *:logger:*:*:1.0: (optional) ILogger components to pass log messages.
- *:counters:*:*:1.0: (optional) ICounters components to pass collected measurements.
- *:service:lambda:*:1.0: (optional) ILambdaService services to handle action requests
- *:service:commandable-lambda:*:1.0: (optional) ILambdaService services to handle action requests
Constructors
InheriteLambdaFunction
Creates a new instance of this lambda function.
InheriteLambdaFunction(overrides ILambdaFunctionOverrides, name string, description string) *LambdaFunction
- overrides: ILambdaFunctionOverrides - overrides.
- name: string - (optional) container name (accessible via ContextInfo).
- description: string - (optional) container description (accessible via ContextInfo).
Fields
Methods
Act
Calls registered action in this lambda function. The “cmd” parameter in the action parameters determines what action shall be called.
- This method shall only be used in testing.
(c *LambdaFunction) Act(params map[string]any) (string, error)
- params: map[string]any - action parameters.
- returns: (string, error) - result
Handler
Executes this AWS Lambda function and returns the result. This method can be overloaded in child classes if it is necessary to change the default behavior
(c *LambdaFunction) Handler(ctx context.Context, event map[string]any) (string, error)
- ctx: context.Context - operation context.
- event: map[string]any - event parameters (or function arguments)
- returns: (string, error) - result of the function execution.
GetHandler
Gets an entry point into this lambda function.
(c *LambdaFunction) GetHandler() func(ctx context.Context, event map[string]any) (string, error)
- returns: func(ctx context.Context, event map[string]any) (string, error) - incoming event object with invocation parameters.
- context: context.Context - context object with local references.
- event: map[string]any - incoming event object with invocation parameters.
Instrument
Gets entry point into this lambda function.
(c *LambdaFunction) Instrument(ctx context.Context, correlationId string, name string) *InstrumentTiming
- ctx: context.Context - operation context.
- correlationId: string - (optional) transaction id used to trace execution through the call chain.
- name: string - method name.
- returns: *InstrumentTiming - object to end the time measurement.
Open
Opens the component.
(c *LambdaFunction) Open(ctx context.Context, correlationId string) error
- ctx: context.Context - operation context.
- correlationId: string - (optional) transaction id used to trace execution through the call chain.
- returns: error - error or nil if no errors occurred.
RegisterAction
Registers an action in this lambda function.
- Note: This method has been deprecated. Use LambdaService instead.
(c *LambdaFunction) RegisterAction(cmd string, schema *Schema, action func(ctx context.Context, params map[string]any) (result any, err error)) error
- cmd: string - action/command name.
- schema: *Schema - validation schema used to validate received parameters.
- action: func(ctx context.Context, params map[string]any) - action function that is called when the action is invoked.
- returns: error - error or nil if no errors occured.
RegisterServices
Registers all actions in this lambda function.
- Note: Overloading of this method has been deprecated. Use LambdaService instead.
(c *LambdaFunction) RegisterServices()
Run
Runs this lambda function, loads container configuration, instantiates components and manages their lifecycle. Makes this function ready to access action calls.
(c *LambdaFunction) Run(ctx context.Context) error
- ctx: context.Context - operation context.
- returns: error - error or nil no errors occured.
SetReferences
Sets references to dependent components.
(c *LambdaFunction) SetReferences(ctx context.Context, references IReferences)
- сеч: context.Context - operation context.
- references: IReferences - references to locate the component’s dependencies.
Examples
type MyLambdaFunction struct {
*awscont.LambdaFunction
controller awstest.IMyController
}
func NewMyLambdaFunction() *MyLambdaFunction {
c := &MyLambdaFunction{}
c.LambdaFunction = awscont.InheriteLambdaFunction(c, "mygroup", "MyGroup lambda function")
c.DependencyResolver.Put(context.Background(), "controller", cref.NewDescriptor("mygroup", "controller", "*", "*", "1.0"))
return c
}
func (c *MyLambdaFunction) SetReferences(ctx context.Context, references cref.IReferences) {
c.LambdaFunction.SetReferences(ctx, references)
depRes, depErr := c.DependencyResolver.GetOneRequired("controller")
if depErr == nil && depRes != nil {
c.controller = depRes.(awstest.IMyController)
}
}
func (c *MyLambdaFunction) getOneById(ctx context.Context, params map[string]any) (any, error) {
correlationId, _ := params["correlation_id"].(string)
return c.controller.GetOneById(
ctx,
correlationId,
params["mydata_id"].(string),
)
}
func (c *MyLambdaFunction) Register() {
c.RegisterAction(
"get_mydata_by_id",
cvalid.NewObjectSchema().
WithOptionalProperty("mydata_id", cconv.String).Schema,
c.getOneById)
}
lambda := NewMyLambdaFunction()
lambda.Run(context.Context())