Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Porting your web app to serverless in 30 minute...

Porting your web app to serverless in 30 minutes [Collision @ Toronto]

This live-coding session will guide you through the process of migrating an existing Express application to AWS Lambda. We will analyze the existing application, decompose it into Lambda functions, reshape the frontend as a static website, and finally deploy it to the Cloud. Throughout this step-by-step process, you will learn the benefits of serverless and how it will change the way you think of scalability, availability, security, infrastructure management, and cost optimization.

Alex Casalboni

May 21, 2019
Tweet

More Decks by Alex Casalboni

Other Decks in Programming

Transcript

  1. 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
  2. About me • Software Engineer & Web Developer • Worked

    in a startup for 4.5 years • ServerlessDays Organizer • AWS Customer since 2013
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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)
  8. 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
  9. The easy way, cheating a bit! $ npm install aws-serverless-express

    github.com/awslabs/aws-serverless-express
  10. The easy way, cheating a bit! (!) $ pip install

    zappa $ zappa init $ zappa deploy production https://www.zappa.io
  11. 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
  12. Alex Casalboni Technical Evangelist, AWS @alex_casalboni @ 2019, Amazon Web

    Services, Inc. or its Affiliates. All rights reserved Thank you! is how