lambda

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

README

aws-lowcode-lambda-go

Creating a config file

# config.yaml
TemplateFormatVersion: 2024-01-31
Description: lowcode-lambda configuration

Resources:
  Request:
    AllowedMethods:
    - GET
    - POST
    - PUT

  Database:
    TableName: users
    Keys:
      email:
        Operator: "="
      order:
        Operator: "="
    Filter: "#age > :age"
    FilterValues:
      age: "5"
    ProjectionCols:
    - email
    - username
    - age
  
  Response:
    DataStruct: '[{"username": "", "age": ""}]'

Building a lowcode lambda function

// main.go
package main

import (
  "os"
  "fmt"

  "github.com/raywall/aws-lowcode-lambda-go/config"
	"github.com/raywall/aws-lowcode-lambda-go/server/clients/dynamodb"
	"github.com/raywall/aws-lowcode-lambda-go/server/handlers"
)

func init() {
  conf := &config.Global

	data, err := os.ReadFile(os.Getenv("CONFIG_SAMPLE"))
	if err != nil {
		log.Fatalf("failed reading lowcode role file: %v", err)
	}

	err = conf.Load(data)
	if err != nil {
		log.Fatalf("failed loading settings: %v", err)
	}

  // create a handler for integration between an api gateway and a dynamodb table
	handlers.Client, err = dynamodb.NewDynamoDBClient(conf)
	if err != nil {
		log.Fatalf("failed starting a dynamodb client: %v", err)
	}
}

func main() {
    // make the handler available for remote procedure call by aws lambda
    lambda.Start(handlers.HandleLambdaEvent)
}

Testing your function locally with SAM


# building your project
sam build

# running your function
sam local start-api

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Global = Config{}

Global is the object that contains your configuration

Functions

This section is empty.

Types

type ComposedData

type ComposedData struct {
	Data     string `yaml:"Data,omitempty"`
	Operator string `yaml:"Operator,omitempty"`
}

ComposedData is a struct representing an AttributeValue. You will use this struct to inform parameters of an key.

Example:

  • let's consider that you have a PK and a SK, and your PK needs to be exactly, but your SK is a number and needs to be better than 5

In this case you will indicate: > Keys:

	email:
  		Operator: "="
	id:
  		Operator: "="

You don't need to inform the data, because the client will get then of your request.

type Config

type Config struct {
	TemplateFormatVersion string    `yaml:"TemplateFormatVersion,omitempty"`
	Description           string    `yaml:"Description,omitempty"`
	Resources             Resources `yaml:"Resources"`
}

Config is a struct representing the Configuration file, containing all the necessary information about the request, database, and response parameters that the client uses to orchestrate requests

func (*Config) IsMethodAllowed

func (config *Config) IsMethodAllowed() bool

IsMethodAllowed checks if the received method is allowed to be processed by the client.

func (*Config) KeyCondition

func (config *Config) KeyCondition() (*KeyCondition, error)

KeyCondition is a function used to prepare data to be used as expressionNames, expressionValues, primary key information, and conditions to orchestrate actions in the DynamoDB table.

func (*Config) Load

func (config *Config) Load(data []byte) error

Load is the function responsible for unmarshaling the configuration YAML file into an object that can be used by the DynamoDBClient.

func (*Config) Set

func (config *Config) Set() error

Set is the function that can be used to load the content of a Config struct into the Global variable.

type Database

type Database struct {
	TableName      string                  `yaml:"TableName"`
	Keys           map[string]ComposedData `yaml:"Keys"`
	Filter         string                  `yaml:"Filter,omitempty"`
	FilterValues   map[string]string       `yaml:"FilterValues,omitempty"`
	ProjectionCols []string                `yaml:"ProjectionCols,omitempty"`
}

Database is a struct representing the necessary DynamoDB information required to perform actions on your table. To use a DynamoDBClient, it's necessary to specify a 'TableName' and the 'Keys' to be used. If you want to apply filtering to a query, you can specify the 'Filter' and 'FilterValues' attributes. When using the 'Filter', use '#name_of_the_column' to specify the column name and ':name_of_the_column' to represent the value to be filtered. In 'FilterValues', provide the actual value to be filtered.

In the following example, we have a query on the 'users' table, specifying keys 'email' and 'product' that should be equal to the values indicated in the request. Additionally, the query will only retrieve items where the 'age' is greater than 18.

Example: > Database:

		TableName: users
		Keys:
  		email:
    			Operator: "="
  		id:
    			Operator: "="
		Filter: "#age > :age"
		FilterValues:
  		age: "18"
		ProjectionCols:
		- email
		- username
		- age

type KeyCondition

type KeyCondition struct {
	ExpressionAttributeNames  map[string]*string                `json:"expressionAttributeNames,omitempty"`
	ExpressionAttributeValues map[string]*dynamo.AttributeValue `json:"expressionAttributeValues,omitempty"`
	PrimaryKeys               map[string]*dynamo.AttributeValue `json:"primaryKeys,omitempty"`
	Condition                 string                            `json:"condition,omitempty"`
}

KeyCondition is a struct used in the client to organize the parameters of the query.

type LowcodeFunction

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

func (*LowcodeFunction) FromConfigFile

func (function *LowcodeFunction) FromConfigFile(filePath string, resource string, destination string) error

type Request

type Request struct {
	AllowedMethods []string          `yaml:"AllowedMethods"`
	HTTPMethod     string            `yaml:"HttpMethod,omitempty"`
	Parameters     map[string]string `yaml:"Parameters,omitempty"`
}

Request is a struct representing the configuration of an HTTP request. This struct defines the HTTP methods allowed to be processed by the client, the method received in the request, and all the query parameters provided.

Example: > Request:

AllowedMethods:
- GET
- POST
- PUT

type Resources

type Resources struct {
	Request  Request  `yaml:"Request"`
	Database Database `yaml:"Database,omitempty"`
	Response Response `yaml:"Response,omitempty"`
}

Resources is a struct used as an attribute of the Config to group information about request, database, and response configurations.

type Response

type Response struct {
	DataStruct string `yaml:"DataStruct,omitempty"`
}

Response is a struct representing the configuration of the response. This struct specifies the data structure of the response in a JSON-based format.

Example: > Response:

DataStruct: '[{"username": "", "age": ""}]'

Directories

Path Synopsis
server
clients/dynamodb
This Go package provides ready-to-use handlers designed for quick and easy usage without the need for extensive code writing, often referred to as 'low-code.' These handlers are constructed based on the origin of the request being made to the Lambda function.
This Go package provides ready-to-use handlers designed for quick and easy usage without the need for extensive code writing, often referred to as 'low-code.' These handlers are constructed based on the origin of the request being made to the Lambda function.

Jump to

Keyboard shortcuts

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