Slide 1

Slide 1 text

Alex Casalboni Technical Evangelist, AWS @alex_casalboni Porting your web app to serverless in 30 minutes is how @ 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved

Slide 2

Slide 2 text

About me • Software Engineer & Web Developer • Worked in a startup for 4.5 years • ServerlessDays Organizer • AWS Customer since 2013

Slide 3

Slide 3 text

Agenda 1. Why serverless computing 2. Choosing a programming language 3. Live coding (app migration)

Slide 4

Slide 4 text

Why serverless computing

Slide 5

Slide 5 text

No server or container management Flexible scaling No idle capacity $ High availability

Slide 6

Slide 6 text

Event-driven architectures Services (anything) Event source Serverless Function

Slide 7

Slide 7 text

Common Use Cases Web apps • Static websites • Complex web apps • Packages for Flask and Express Data processing • Real time • MapReduce • Batch • Machine learning inference Chatbots • Powering chatbot logic Backends • Apps and services • Mobile • IoT > > Amazon Alexa • Powering voice-enabled apps • Alexa Skills Kit IT automation • Policy engines • Extending AWS services • Infrastructure management

Slide 8

Slide 8 text

AWS Serverless Application Model (SAM) Simplified template-driven deployment model for serverless applications New serverless resource types An extension (Macro) of AWS CloudFormation Open specification (Apache 2.0) github.com/awslabs/serverless-application-model

Slide 9

Slide 9 text

AWSTemplateFormatVersion: '2010-09-09' Resources: GetHtmlFunctionGetHtmlPermissionProd: Type: AWS::Lambda::Permission Properties: Action: lambda:invokeFunction Principal: apigateway.amazonaws.com FunctionName: Ref: GetHtmlFunction SourceArn: Fn::Sub: arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${ServerlessRestApi}/Prod/ANY/* ServerlessRestApiProdStage: Type: AWS::ApiGateway::Stage Properties: DeploymentId: Ref: ServerlessRestApiDeployment RestApiId: Ref: ServerlessRestApi StageName: Prod ListTable: Type: AWS::DynamoDB::Table Properties: ProvisionedThroughput: WriteCapacityUnits: 5 ReadCapacityUnits: 5 AttributeDefinitions: - AttributeName: id AttributeType: S KeySchema: - KeyType: HASH AttributeName: id GetHtmlFunction: Type: AWS::Lambda::Function Properties: Handler: index.gethtml Code: S3Bucket: flourish-demo-bucket S3Key: todo_list.zip Role: Fn::GetAtt: - GetHtmlFunctionRole - Arn Runtime: nodejs4.3 GetHtmlFunctionRole: Type: AWS::IAM::Role Properties: ManagedPolicyArns: - arn:aws:iam::aws:policy/AmazonDynamoDBReadOnlyAccess - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Action: - sts:AssumeRole Effect: Allow Principal: Service: - lambda.amazonaws.com ServerlessRestApiDeployment: Type: AWS::ApiGateway::Deployment Properties: RestApiId: Ref: ServerlessRestApi Description: 'RestApi deployment id: 127e3fb91142ab1ddc5f5446adb094442581a90d' StageName: Stage GetHtmlFunctionGetHtmlPermissionTest: Type: AWS::Lambda::Permission Properties: Action: lambda:invokeFunction Principal: apigateway.amazonaws.com FunctionName: Ref: GetHtmlFunction SourceArn: Fn::Sub: arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${ServerlessRestApi}/*/ANY/* ServerlessRestApi: Type: AWS::ApiGateway::RestApi Properties: Body: info: version: '1.0' title: Ref: AWS::StackName paths: "/{proxy+}": x-amazon-apigateway-any-method: x-amazon-apigateway-integration: httpMethod: ANY type: aws_proxy uri: Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03- 31/functions/${GetHtmlFunction.Arn}/invocations responses: {} swagger: '2.0' CloudFormation template

Slide 10

Slide 10 text

SAM Template Tells AWS CloudFormation this is a SAM template it needs to transform Creates a Lambda function, an API Gateway resource/method and all mapping and permissions necessary AWSTemplateFormatVersion: '2010-09-09’ Transform: AWS::Serverless-2016-10-31 Resources: GetHtmlFunction: Type: AWS::Serverless::Function Properties: CodeUri: s3://sam-demo-bucket/todo_list.zip Handler: index.gethtml Runtime: nodejs4.3 Policies: AmazonDynamoDBReadOnlyAccess Events: GetHtml: Type: Api Properties: Path: /{proxy+} Method: ANY

Slide 11

Slide 11 text

github.com/awslabs/aws-sam-cli AWS SAM CLI CLI tool for local testing of serverless apps Response object and function logs available on your local machine Uses open-source Docker-Lambda images to mimic Lambda’s execution environment (timeout, memory limits, runtimes)

Slide 12

Slide 12 text

AWS Lambda Runtimes

Slide 13

Slide 13 text

2015 Node.js 0.10 Python 2.7 Java 2016 Node.js 4.3 .NET 2017 Node.js 6.10 Python 3.6 2018 Node.js 8.10 Python 3.7 .NET 2.0 & 2.1 PowerShell Go & Ruby 2019 Node.js 10 Lambda runtimes history

Slide 14

Slide 14 text

How do we port a web app to AWS Lambda?

Slide 15

Slide 15 text

The easy way, cheating a bit! $ npm install aws-serverless-express github.com/awslabs/aws-serverless-express

Slide 16

Slide 16 text

The easy way, cheating a bit! (!) $ pip install zappa $ zappa init $ zappa deploy production https://www.zappa.io

Slide 17

Slide 17 text

from flask import Flask app = Flask(__name__) @app.route('/’) def hello_world(): return 'Hello, World!' from chalice import Chalice app = Chalice(__name__) @app.route('/’) def hello_world(): return 'Hello, World!' The easy way, cheating a bit! (! !) $ pip install chalice # code refactor $ chalice deploy https://chalice.readthedocs.io

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Coding time!

Slide 20

Slide 20 text

github.com/alexcasalboni/express-app-serverless-porting

Slide 21

Slide 21 text

github.com/alexcasalboni/flask-app-serverless-porting

Slide 22

Slide 22 text

awsdeveloperworkshopcollision.splashthat.com

Slide 23

Slide 23 text

Alex Casalboni Technical Evangelist, AWS @alex_casalboni @ 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved Thank you! is how