Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Today's plan 1. What is serverless? 2. A few options in Python 3. Serverless Hello World 4. Event-driven service 5. Where do I go from there?

Slide 3

Slide 3 text

What is serverless? (and why should I care?)

Slide 4

Slide 4 text

Function as a Service (FaaS), a.k.a Serverless ‣ reduced time to production ‣ ease of maintenance ‣ on-demand scaling ‣ pay-per-use

Slide 5

Slide 5 text

"No server is easier to manage than no server." — Werner Vogel, Amazon CTO

Slide 6

Slide 6 text

What kind of workload is right for Serverless?

Slide 7

Slide 7 text

What kind of workload is right for Serverless? ‣ Chat bots & IoT ‣ Client-heavy applications ‣ Batch processing ‣ Stateless microservices

Slide 8

Slide 8 text

What's not a good fit?

Slide 9

Slide 9 text

What's not a good fit? ‣ Long-running background jobs ‣ Memory-bound computing ‣ Apps with high-perf requirements (e.g. trading)

Slide 10

Slide 10 text

Python's Serverless Landscape

Slide 11

Slide 11 text

Current Best Practices

Slide 12

Slide 12 text

Current Best Practices ‣ Assumes you are familiar with AWS ‣ Difficult for a dev to get started ‣ Focused on the infrastructure to run the code

Slide 13

Slide 13 text

What we'll do instead ‣ Leverage frameworks to do the heavy lifting ‣ Focus on the code while generating the infrastructure ‣ Learn AWS by using the services

Slide 14

Slide 14 text

Serverless Framework

Slide 15

Slide 15 text

Zappa

Slide 16

Slide 16 text

Chalice

Slide 17

Slide 17 text

Hello World!

Slide 18

Slide 18 text

Add user

Slide 19

Slide 19 text

Set permissions

Slide 20

Slide 20 text

Review your choices

Slide 21

Slide 21 text

Download your Acess key and Secret

Slide 22

Slide 22 text

Install the AWS CLI1 $ brew install awscli 1 https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html

Slide 23

Slide 23 text

Configure the AWS cli $ aws configure AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY Default region name [None]: ca-central-1 Default output format [None]: json

Slide 24

Slide 24 text

Configure the AWS cli (alternate) # ~/.aws/credentials [chalice-hello] aws_access_key_id=AKIAIOSFODNN7EXAMPLE aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

Slide 25

Slide 25 text

Install Chalice $ pip install chalice

Slide 26

Slide 26 text

Create a project $ chalice new-project helloworld

Slide 27

Slide 27 text

$ tree -a . ├── .chalice │ └── config.json ├── .gitignore ├── app.py └── requirements.txt 1 directory, 4 files

Slide 28

Slide 28 text

app.py

Slide 29

Slide 29 text

Export your AWS profile $ export AWS_PROFILE=chalice-hello

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

hello-world-dev IAM role { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents" ], "Resource": "arn:*:logs:*:*:*" } ] }

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

Let's make something useful!

Slide 39

Slide 39 text

The plan 1. event-driven instead of REST 2. read/write to S3 bucket 3. process images using a Python library

Slide 40

Slide 40 text

Create a bucket $ aws s3 mb s3://confoo2021-hello-serverless

Slide 41

Slide 41 text

@app.on_s3_event( bucket='confoo2021-hello-serverless', events=['s3:ObjectCreated:Put'], prefix='images/', suffix='.jpg' ) def resize_image(event): with tempfile.NamedTemporaryFile('w') as f: s3.download_file(event.bucket, event.key, f.name) im = Image.open(f.name) im.thumbnail((250, 250)) im.save(f.name, "JPEG", quality=80) s3.upload_file(f.name, event.bucket, f'thumbnails/{event.key}')

Slide 42

Slide 42 text

@app.on_s3_event( bucket='confoo2021-hello-serverless', events=['s3:ObjectCreated:Put'], prefix='images/', suffix='.jpg' ) def resize_image(event): with tempfile.NamedTemporaryFile('w') as f: s3.download_file(event.bucket, event.key, f.name) im = Image.open(f.name) im.thumbnail((250, 250)) im.save(f.name, "JPEG", quality=80) s3.upload_file(f.name, event.bucket, f'thumbnails/{event.key}')

Slide 43

Slide 43 text

@app.on_s3_event( bucket='confoo2021-hello-serverless', events=['s3:ObjectCreated:Put'], prefix='images/', suffix='.jpg' ) def resize_image(event): with tempfile.NamedTemporaryFile('w') as f: s3.download_file(event.bucket, event.key, f.name) im = Image.open(f.name) im.thumbnail((250, 250)) im.save(f.name, "JPEG", quality=80) s3.upload_file(f.name, event.bucket, f'thumbnails/{event.key}')

Slide 44

Slide 44 text

@app.on_s3_event( bucket='confoo2021-hello-serverless', events=['s3:ObjectCreated:Put'], prefix='images/', suffix='.jpg' ) def resize_image(event): with tempfile.NamedTemporaryFile('w') as f: s3.download_file(event.bucket, event.key, f.name) im = Image.open(f.name) im.thumbnail((250, 250)) im.save(f.name, "JPEG", quality=80) s3.upload_file(f.name, event.bucket, f'thumbnails/{event.key}')

Slide 45

Slide 45 text

@app.on_s3_event( bucket='confoo2021-hello-serverless', events=['s3:ObjectCreated:Put'], prefix='images/', suffix='.jpg' ) def resize_image(event): with tempfile.NamedTemporaryFile('w') as f: s3.download_file(event.bucket, event.key, f.name) im = Image.open(f.name) im.thumbnail((250, 250)) im.save(f.name, "JPEG", quality=80) s3.upload_file(f.name, event.bucket, f'thumbnails/{event.key}')

Slide 46

Slide 46 text

@app.on_s3_event( bucket='confoo2021-hello-serverless', events=['s3:ObjectCreated:Put'], prefix='images/', suffix='.jpg' ) def resize_image(event): with tempfile.NamedTemporaryFile('w') as f: s3.download_file(event.bucket, event.key, f.name) im = Image.open(f.name) im.thumbnail((250, 250)) im.save(f.name, "JPEG", quality=80) s3.upload_file(f.name, event.bucket, f'thumbnails/{event.key}')

Slide 47

Slide 47 text

Deploy the new function $ chalice deploy Creating deployment package. Updating policy for IAM role: hello-world-dev Creating lambda function: hello-world-dev-resize_image Configuring S3 events in bucket mybucket to function hello-world-dev-resize_image Updating lambda function: hello-world-dev Updating rest API Resources deployed: - Lambda ARN: arn:aws:lambda:ca-central-1:867045000300:function:hello-world-dev-resize_image - Lambda ARN: arn:aws:lambda:ca-central-1:867045000300:function:hello-world-dev - Rest API URL: https://y25aa7x0qg.execute-api.ca-central-1.amazonaws.com/api/

Slide 48

Slide 48 text

Deploy the new function $ chalice deploy Creating deployment package. Updating policy for IAM role: hello-world-dev Creating lambda function: hello-world-dev-resize_image Configuring S3 events in bucket mybucket to function hello-world-dev-resize_image Updating lambda function: hello-world-dev Updating rest API Resources deployed: - Lambda ARN: arn:aws:lambda:ca-central-1:867045000300:function:hello-world-dev-resize_image - Lambda ARN: arn:aws:lambda:ca-central-1:867045000300:function:hello-world-dev - Rest API URL: https://y25aa7x0qg.execute-api.ca-central-1.amazonaws.com/api/

Slide 49

Slide 49 text

Upload an image $ aws s3 cp truck.jpg s3://confoo2021-hello-serverless/images/truck.jpg

Slide 50

Slide 50 text

Check the corresponding thumbnail

Slide 51

Slide 51 text

Our Architecture

Slide 52

Slide 52 text

Going Further... ‣ custom domain ‣ security ‣ testing ‣ observability (logs, metrics, error tracking) ‣ keeping your function warm to avoid cold-starts ‣ going beyond the execution time limit

Slide 53

Slide 53 text

Example code

Slide 54

Slide 54 text

Slides

Slide 55

Slide 55 text

Links ‣ Github repository with code from this talks ‣ Chalice website and Documentation ‣ A sample TODO application

Slide 56

Slide 56 text

Thank you!