Slide 1

Slide 1 text

Serverless Architecture for Powerful Data Pipelines Jason A Myers

Slide 2

Slide 2 text

Credit: Horst Felske and Fritz Schiemann ex-convex.org

Slide 3

Slide 3 text

Issues — Scheduled — Complex — Scaling — Recovery

Slide 4

Slide 4 text

Credit: Anonymous

Slide 5

Slide 5 text

Python Toolkits and Services — Luigi — Airflow — AWS Data Pipeline

Slide 6

Slide 6 text

SERVERLESS

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Cloud Functions

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Simple Pipeline overview

Slide 12

Slide 12 text

Simple Pipeline overview

Slide 13

Slide 13 text

Simple Pipeline overview

Slide 14

Slide 14 text

Simple Pipeline overview

Slide 15

Slide 15 text

Simple Pipeline overview

Slide 16

Slide 16 text

Serverless Python Tools — Zappa — Apex — Chalice — Serverless

Slide 17

Slide 17 text

Apex — Multiple Environment Support — Function Deployment — Infrastructure as code via Terraform

Slide 18

Slide 18 text

Project Structure !"" functions # $"" listener # $"" main.py !"" infrastructure # !"" dev # # !"" main.tf # # !"" outputs.tf # # $"" variables.tf # !"" prod # # !"" main.tf # # !"" outputs.tf # # $"" variables.tf !"" project.json $"" project.prod.json

Slide 19

Slide 19 text

Project Structure !"" functions # $"" listener # $"" main.py !"" infrastructure # !"" dev # # !"" main.tf # # !"" outputs.tf # # $"" variables.tf # !"" prod # # !"" main.tf # # !"" outputs.tf # # $"" variables.tf !"" project.json $"" project.prod.json

Slide 20

Slide 20 text

Project Structure !"" functions # $"" listener # $"" main.py !"" infrastructure # !"" dev # # !"" main.tf # # !"" outputs.tf # # $"" variables.tf # !"" prod # # !"" main.tf # # !"" outputs.tf # # $"" variables.tf !"" project.json $"" project.prod.json

Slide 21

Slide 21 text

Apex package.json { "name": "listener", "description": "S3 File Listener", "runtime": "python3.6", "memory": 128, "timeout": 5, "role": "arn:aws:iam::ACOUNTNUM:role/listen_lambda_function", "environment": {}, "defaultEnvironment": "dev" }

Slide 22

Slide 22 text

S3 Event Handler import logging import boto3 log = logging.getLogger() log.setLevel(logging.DEBUG) def get_bucket_key(event); bucket = event['Records'][0]['s3']['bucket']['name'] key = event['Records'][0]['s3']['object']['key'] return bucket, key def handle(event, context): log.info('{}-{}'.format(event, context)) bucket_name, key_name = get_bucket_key(event)

Slide 23

Slide 23 text

S3 Event Handler import logging import boto3 log = logging.getLogger() log.setLevel(logging.DEBUG) def get_bucket_key(event); bucket = event['Records'][0]['s3']['bucket']['name'] key = event['Records'][0]['s3']['object']['key'] return bucket, key def handle(event, context): log.info('{}-{}'.format(event, context)) bucket_name, key_name = get_bucket_key(event)

Slide 24

Slide 24 text

S3 Event Handler import logging import boto3 log = logging.getLogger() log.setLevel(logging.DEBUG) def get_bucket_key(event); bucket = event['Records'][0]['s3']['bucket']['name'] key = event['Records'][0]['s3']['object']['key'] return bucket, key def handle(event, context): log.info('{}-{}'.format(event, context)) bucket_name, key_name = get_bucket_key(event)

Slide 25

Slide 25 text

S3 Event Handler (cont.) values = { 'bucket_name': bucket_name, 'key_name': key_name, 'timestamp': datetime.utcnow().isoformat() } client = boto3.client('sqs') client.publish( TopicArn=topic_arn, Message=json.dumps(values) )

Slide 26

Slide 26 text

AWS Logging Permissions data "aws_iam_policy_document" "listener_logging" { statement { sid = "AllowRoleToOutputCloudWatchLogs" effect = "Allow" actions = ["logs:*"] resources = ["*"] } } resource "aws_iam_policy" "listener_logs" { name = "listener_logs" description = "Allow listener to log operations" policy = "${data.aws_iam_policy_document.listener_logging.json}" }

Slide 27

Slide 27 text

AWS IAM Role Assumption data "aws_iam_policy_document" "listener_lambda_assume_role" { statement { sid = "AllowRoleToBeUsedbyLambda" effect = "Allow" actions = ["sts:AssumeRole"] principals { type = "Service" identifiers = ["lambda.amazonaws.com"] } } } resource "aws_iam_role" "listener_lambda_function" { name = "listener_lambda_function" assume_role_policy = "${data.aws_iam_policy_document.listener_lambda_assume_role.json}" }

Slide 28

Slide 28 text

AWS Policy A!achment resource "aws_iam_policy_attachment" "listener_logs_attach" { name = "listener_logs_attach" roles = ["${aws_iam_role.listener_lambda_function.name}"] policy_arn = "${aws_iam_policy.listener_logs.arn}" }

Slide 29

Slide 29 text

Deploy Function and Infrastructure # apex deploy # apex infra plan # apex infra apply

Slide 30

Slide 30 text

What about those other functions...

Slide 31

Slide 31 text

Lambda Packages Zappa Project/Gun.io

Slide 32

Slide 32 text

Lambda Packages List bcrypt cffi PyNaCl datrie LXML misaka MySQL-Python numpy OpenCV Pillow (PIL) psycopg2 PyCrypto cryptography pyproj python-ldap python- Levenshtein regex

Slide 33

Slide 33 text

Dependency Handling in Apex "hooks":{ "build": "pip install -r requirements.txt -t ." }

Slide 34

Slide 34 text

Function Considerations — atomic — idempotent

Slide 35

Slide 35 text

Dis is the Remix — Longer Jobs — Legacy Pipelines

Slide 36

Slide 36 text

Hybrid Pipeline overview

Slide 37

Slide 37 text

Hybrid Pipeline overview

Slide 38

Slide 38 text

Hybrid Pipeline overview

Slide 39

Slide 39 text

Hybrid Pipeline overview

Slide 40

Slide 40 text

Hybrid Pipeline overview

Slide 41

Slide 41 text

Closing thoughts — Workload and Phases are important — 14x improvement — 0.73x improvement

Slide 42

Slide 42 text

Questions?