lambda

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2019 License: BSD-3-Clause Imports: 10 Imported by: 0

README

AWS Lambda Trigger

The Lambda trigger provides your Flogo application the ability to start actions running as an AWS Lambda function.

How it works

In Lambda, functions can be triggered by a variety of event sources (like Amazon S3, or the Amazon API Gateway) and in Lambda terms, each of them would need a specific method to serve as starting point. To overcome that, and make it possible to create just one action that can be triggered by multiple events, the Lambda trigger abstracts the incoming event, making sure that no change is needed when you want to hook up other events to the same trigger. If needed you can also specify the event type to route specific events to specific actions.

Installation

Flogo CLI
flogo install github.com/project-flogo/aws-contrib/trigger/lambda

Configuration

Handler Settings:
Name Type Description
eventType string The type of event to handle, (e.g. aws:s3, aws:sns)
Output:
Name Type Description
context object The lambda context information
event object The event data
eventType string The event type, (e.g. aws:s3, aws:sns)
Reply:
Name Type Description
status int The status code to reply with
data any The data to reply with

Event Types

ID Service
aws:apigw AWS API Gateway
aws:autoscaling AWS AutoScaling
aws:cloudfront AWS CloudFront
aws:cloudwatch AWS CloudWatch
aws:cloudwatch-logs AWS CloudWatch Logs
aws:codecommit AWS CodeCommit
aws:codepipeline AWS CodePipeline
aws:cognito AWS Cognito
aws:config AWS Config
aws:dynamodb AWS Dynamodb
aws:kinesis AWS Kinesis
aws:kinesis-analytics AWS Kinesis Analytics
aws:kinesis-firehose AWS Kinesis Firehose
aws:s3 AWS S3
aws:ses AWS SES
aws:sns AWS SNS
aws:sqs AWS SQS

Context

The context object contains details about the Flogo app and the runtime

{
    "awsRequestId":"6301513f-c5c5-11e8-9997-c9e96bfbdd33",
    "functionName":"TestApp",
    "functionVersion":"$LATEST",
    "logGroupName":"/aws/lambda/TestApp",
    "logStreamName":"2018/10/01/[$LATEST]ec9b2c81da4b4307ab50738bb1eeb28f",
    "memoryLimitInMB":128
}
Name Description
awsRequestId The request ID that AWS associated with the Lambda execution
functionName The name of the function that is executed
functionVersion The version of the function that is executed
logGroupName The name of the log group in AWS CloudWatch where logs are sent to
logStreamName The logstream to which events are sent
memoryLimitInMB The amount of RAM your function has available

Example events

The events themselves are not changed by the Lambda trigger, though without changing your Lambda code a single app can handle multiple types of events. Each of the properties of the event are accessible using the dot notation in the mappings.

Amazon S3

When Amazon S3 emits a "PUT" event, the event details that are passed to the action will look like

{
    "Records": [
        {
            "awsRegion": "us-west-2",
            "eventName": "ObjectCreated:Put",
            "eventSource": "aws:s3",
            "eventTime": "1970-01-01T00:00:00.000Z",
            "eventVersion": "2.0",
            "requestParameters": {
                "sourceIPAddress": "127.0.0.1"
            },
            "responseElements": {
                "x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH",
                "x-amz-request-id": "EXAMPLE123456789"
            },
            "s3": {
                "bucket": {
                    "arn": "arn:aws:s3:::example-bucket",
                    "name": "example-bucket",
                    "ownerIdentity": {
                        "principalId": "EXAMPLE"
                    }
                },
                "configurationId": "testConfigRule",
                "object": {
                    "eTag": "0123456789abcdef0123456789abcdef",
                    "key": "test/key",
                    "sequencer": "0A1B2C3D4E5F678901",
                    "size": 1024
                },
                "s3SchemaVersion": "1.0"
            },
            "userIdentity": {
                "principalId": "EXAMPLE"
            }
        }
    ]
}
Amazon DynamoDB

When Amazon DynamoDB emits an "Update" event, the event details that are passed to the action will look like

{
    "Records": [
        {
            "awsRegion": "us-west-2",
            "dynamodb": {
                "Keys": {
                    "Id": {
                        "N": "101"
                    }
                },
                "NewImage": {
                    "Id": {
                        "N": "101"
                    },
                    "Message": {
                        "S": "New item!"
                    }
                },
                "SequenceNumber": "111",
                "SizeBytes": 26,
                "StreamViewType": "NEW_AND_OLD_IMAGES"
            },
            "eventID": "1",
            "eventName": "INSERT",
            "eventSource": "aws:dynamodb",
            "eventSourceARN": "arn:aws:dynamodb:us-west-2:account-id:table/ExampleTableWithStream/stream/2015-06-27T00:48:05.899",
            "eventVersion": "1.0"
        }
    ]
}
AWS CloudWatch

When AWS CloudWatch emits a scheduled event, the event details that are passed to the action will look like

{
    "account": "123456789012",
    "detail": {},
    "detail-type": "Scheduled Event",
    "id": "cdc73f9d-aea9-11e3-9d5a-835b769c0d9c",
    "region": "us-east-1",
    "resources": [
        "arn:aws:events:us-east-1:123456789012:rule/my-schedule"
    ],
    "source": "aws.events",
    "time": "1970-01-01T00:00:00Z"
}
Amazon API Gateway

When Amazon API Gateway forwards an API call, the event details that are passed to the action will look like

{
    "body": "eyJ0ZXN0IjoiYm9keSJ9",
    "headers": {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Accept-Encoding": "gzip, deflate, sdch",
        "Accept-Language": "en-US,en;q=0.8",
        "Cache-Control": "max-age=0",
        "CloudFront-Forwarded-Proto": "https",
        "CloudFront-Is-Desktop-Viewer": "true",
        "CloudFront-Is-Mobile-Viewer": "false",
        "CloudFront-Is-SmartTV-Viewer": "false",
        "CloudFront-Is-Tablet-Viewer": "false",
        "CloudFront-Viewer-Country": "US",
        "Host": "1234567890.execute-api.us-west-2.amazonaws.com",
        "Upgrade-Insecure-Requests": "1",
        "User-Agent": "Custom User Agent String",
        "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)",
        "X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==",
        "X-Forwarded-For": "127.0.0.1, 127.0.0.2",
        "X-Forwarded-Port": "443",
        "X-Forwarded-Proto": "https"
    },
    "httpMethod": "POST",
    "isBase64Encoded": "false",
    "path": "/path/to/resource",
    "pathParameters": {
        "proxy": "/path/to/resource"
    },
    "queryStringParameters": {
        "foo": "bar"
    },
    "requestContext": {
        "accountId": "123456789012",
        "apiId": "1234567890",
        "httpMethod": "POST",
        "identity": {
            "accessKey": null,
            "accountId": null,
            "caller": null,
            "cognitoAuthenticationProvider": null,
            "cognitoAuthenticationType": null,
            "cognitoIdentityId": null,
            "cognitoIdentityPoolId": null,
            "sourceIp": "127.0.0.1",
            "user": null,
            "userAgent": "Custom User Agent String",
            "userArn": null
        },
        "path": "/prod/path/to/resource",
        "protocol": "HTTP/1.1",
        "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
        "requestTime": "09/Apr/2015:12:34:56 +0000",
        "requestTimeEpoch": 1428582896000,
        "resourceId": "123456",
        "resourcePath": "/{proxy+}",
        "stage": "prod"
    },
    "resource": "/{proxy+}",
    "stageVariables": {
        "baz": "qux"
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromEventType

func FromEventType(et EvtType) string

func GenerateResponse

func GenerateResponse(details *RequestDetails, result map[string]interface{}) ([]byte, error)

func Invoke

func Invoke(details *RequestDetails) (map[string]interface{}, error)

Invoke starts the trigger and invokes the action registered in the handler

Types

type EvtType

type EvtType int
const (
	EtUnknown EvtType = iota
	EtFlogoOnDemand
	EtAwsApiGw
	EtAwsAutoScaling
	EtAwsCloudFront
	EtAwsCloudWatch
	EtAwsCloudWatchLogs
	EtAwsCodeCommit
	EtAwsCodePipeline
	EtAwsCognito
	EtAwsConfig
	EtAwsDynamodb
	EtAwsKinesis
	EtAwsKinesisAnalytics
	EtAwsKinesisFirehose
	EtAwsS3
	EtAwsSes
	EtAwsSns
	EtAwsSqs
)

func GetEventType

func GetEventType(payload map[string]interface{}) EvtType

func ToEventType

func ToEventType(str string) EvtType

type FlogoEvent

type FlogoEvent struct {
	Payload interface{}     `json:"payload"`
}

type HandlerSettings

type HandlerSettings struct {
	EventType string `md:"eventType"` // The type of event to handle, (e.g. aws:s3, aws:sns)
}

type LambdaFactory

type LambdaFactory struct {
}

LambdaFactory AWS Lambda Trigger factory

func (*LambdaFactory) Metadata

func (t *LambdaFactory) Metadata() *trigger.Metadata

Metadata implements trigger.Trigger.Metadata

func (*LambdaFactory) New

func (t *LambdaFactory) New(config *trigger.Config) (trigger.Trigger, error)

New Creates a new trigger instance for a given id

type LambdaTrigger

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

LambdaTrigger AWS Lambda trigger struct

func (*LambdaTrigger) Initialize

func (t *LambdaTrigger) Initialize(ctx trigger.InitContext) error

func (*LambdaTrigger) Start

func (t *LambdaTrigger) Start() error

func (*LambdaTrigger) Stop

func (t *LambdaTrigger) Stop() error

Stop implements util.Managed.Stop

type Output

type Output struct {
	Context   map[string]interface{} `md:"context"`   // The lambda context information
	Event     map[string]interface{} `md:"event"`     // The event data
	EventType string                 `md:"eventType"` // The event type, (e.g. aws:s3, aws:sns)
}

func (*Output) FromMap

func (o *Output) FromMap(values map[string]interface{}) error

func (*Output) ToMap

func (o *Output) ToMap() map[string]interface{}

type Reply

type Reply struct {
	Data   interface{} `md:"data"`   // The data to reply with
	Status int         `md:"status"` // The status code to reply with
}

func (*Reply) FromMap

func (r *Reply) FromMap(values map[string]interface{}) error

func (*Reply) ToMap

func (r *Reply) ToMap() map[string]interface{}

type RequestDetails

type RequestDetails struct {
	CtxInfo   map[string]interface{}
	Payload   []byte
	Event     map[string]interface{}
	EventType EvtType
}

func ExtractRequestDetails

func ExtractRequestDetails(ctx context.Context, payload []byte) (*RequestDetails, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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