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

Intro to Serverless

Aletheia
January 08, 2020

Intro to Serverless

Now in 2020, check out a modern introduction to Serverless technologies and frameworks

Aletheia

January 08, 2020
Tweet

More Decks by Aletheia

Other Decks in Technology

Transcript

  1. Luca Bianchi Who am I? Chief Technology Officer @ Neosperience

    github.com/aletheia @bianchiluca https://it.linkedin.com/in/lucabianchipavia https://speakerdeck.com/aletheia Chief Technology Officer @ WizKey Serverless Meetup Italy manager ServerlessDays co-organizer
  2. a few words about our meetup… Serverless Meetup • 1000+

    members • Monthly Meetups 
 https://www.meetup.com/Serverless-Italy/members/
 • Serverless OnTheRoad and OnStage
  3. A cloud platform built on AWS to deliver DCX projects

    Neosperience Cloud • Deeply understand their customers and be more useful to them by delivering relevant digital experiences. • Delight customers by delivering relevant experiences across mobile, web, in-store. • Maintain their Brand identity and increase value as platforms like Amazon, Google and Facebook drive up disintermediation and make companies unintentional utilities. • Keep pace with the variety of devices and interaction models available to customers to overcome complexity and costs associated with the alignment of apps, web apps, social media and conversational interfaces. Neosperience Cloud is the technology platform that allows creating personalized experiences for your customers that drive loyalty and faster paths to purchase. Unlike existing technologies that rely only on demographics data, we use proprietary models, developed with AI, to personalize your offering to the right segment. A compelling experience for each customer at the right time, place, and situational context. …which means fast time to market, machine learning and scalability by design.
  4. What is Serverless? “Serverless architecture replaces long-running virtual machines with

    ephemeral compute power that comes into existence on request and disappears immediately after use. Use of this architecture can mitigate some security concerns such as security patching and SSH access control, and can make much more efficient use of compute resources. These systems cost very little to operate and can have inbuilt scaling features.” — ThoughtWorks, 2016
  5. Serverless means no servers. No hardware to provision or manage

    No IT service team installing hardware But still it’s someone else server Server VM OS frameworks code your duty
  6. Serverless means no VMs. No under or over provisioning Never

    pay for idle No VM disaster recovery VM OS frameworks code your duty
  7. Serverless means no OS to config. OS is provisioned automatically

    Patches are installed by vendor Built-in best practices OS frameworks code your duty
  8. Serverless means no schedulers. Code is invoked by platform Language

    support is packed within runtime Analytics are provided out of the box frameworks code your duty
  9. Serverless means Servicefull. Patrick Debois - 2016 Server VM OS

    frameworks code your duty some one else duty
  10. The Serverless Manifesto ✓ Function as the unit of deployment

    and scaling ✓ Implicitly fault-tolerant ✓ Metrics ✓ No machines, VMs, or containers ✓ Bring Your Own Code ✓ Stateless ✓ Never pay for idle ✓ Scales per request
  11. It starts with an event • events are a great

    way to decouple services (async message passing) • largely abused in the last decade (EnterpriseServiceBus / SOA) • now revamped with CQRS and Event Sourcing to handle microservices communication • serverless functions handle events • need a different approach on architectures
  12. AWS Lambda AWS Lambda is a compute service that lets

    you run code without provisioning or managing servers. AWS Lambda executes your code only when needed and scales automatically, from a few requests per day to thousands per second. • Service released on Nov, 2014 • Introduces Event-Driven programming model • Provides a context and an event object
  13. Triggering a function • Provide new levels of empowerment to

    your customers: turn your app on - even if their smartphone is in stand-by mode - and deliver a notification to sell direct or to bring back qualified customers to your venue. • Utilize Facebook and other social media platforms to encourage digital and physical store purchases and prompt customers with personalized messages. Send promotions and relevant product information when they are ready to buy: shopping, close or inside your store, entering your competitor’s store or located in adjacent businesses.
  14. Triggering a function Alexa Skill Amazon S3 Trigger AWS Step

    Function AWS SNS Amazon Cognito Amazon SQS Amazon API Gateway Amazon DynamoDB Amazon Kinesis AWS CloudFormation AWS IoT Amazon Button
  15. Script your cloud infrastructure Infrastructure as Code (IaC) AWS CloudFormation

    is the scripting language for AWS Cloud Describe your cloud infrastructure in a JSON or YML template file and let CloudFormation build resources you need Templates can be versioned ensuring immutability Deployments can be parametrized Built-in helpers
  16. AWS CloudFormation • Cloud Services become a part of your

    project Infrastructure-as- code A scripting language for the cloud • Defines AWS resources (Lambda, S3Buckets, DynamoTables, etc) Manages resource lifecycle • Example: creating a DynamoDB Table https://docs.aws.amazon.com/AWSCloudFormation/ latest/UserGuide/Welcome.html
  17. --- Resources: MyServiceStoreEE9BC9A0: Type: AWS::S3::Bucket UpdateReplacePolicy: Retain DeletionPolicy: Retain Metadata:

    aws:cdk:path: RestStack/MyServiceStore/Resource LambdaHandlerServiceRole8F7B4955: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Statement: - Action: sts:AssumeRole Effect: Allow Principal: Service: lambda.amazonaws.com Version: '2012-10-17' ManagedPolicyArns: - Fn::Join: - '' - - 'arn:' - Ref: AWS::Partition - ":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" Metadata: aws:cdk:path: RestStack/LambdaHandler/ServiceRole/Resource LambdaHandlerServiceRoleDefaultPolicy52DB1F25: Type: AWS::IAM::Policy Properties: PolicyDocument: Statement: - Action: - s3:GetObject* - s3:GetBucket* - s3:List* - s3:DeleteObject* - s3:PutObject* - s3:Abort* Effect: Allow Resource: - Fn::GetAtt: - MyServiceStoreEE9BC9A0 - Arn - Fn::Join: - '' - - Fn::GetAtt: - MyServiceStoreEE9BC9A0 - Arn - "/*" Version: '2012-10-17' PolicyName: LambdaHandlerServiceRoleDefaultPolicy52DB1F25 Roles: - Ref: LambdaHandlerServiceRole8F7B4955 Metadata: aws:cdk:path: RestStack/LambdaHandler/ServiceRole/DefaultPolicy/Resource LambdaHandlerApiPermissionTestRestStackmyserviceapiD3EA4862GET17E42E15: Type: AWS::Lambda::Permission Properties: Action: lambda:InvokeFunction FunctionName: Fn::GetAtt: - LambdaHandler212865DC - Arn Principal: apigateway.amazonaws.com SourceArn: Fn::Join: - '' - - 'arn:' - Ref: AWS::Partition - ":execute-api:" - Ref: AWS::Region - ":" - Ref: AWS::AccountId - ":" - Ref: myserviceapi226E6B58 - "/test-invoke-stage/GET/" Metadata: aws:cdk:path: RestStack/LambdaHandler/ ApiPermission.Test.RestStackmyserviceapiD3EA4862.GET.. myserviceapi226E6B58: Type: AWS::ApiGateway::RestApi Properties: Description: This is my service. Name: MyService Metadata: aws:cdk:path: RestStack/myservice-api/Resource myserviceapiDeployment64624B21f95e073b8b06b61d3c084e7dd1be6a99: Type: AWS::ApiGateway::Deployment Properties: RestApiId: Ref: myserviceapi226E6B58 Description: Automatically created by the RestApi construct DependsOn: - myserviceapiGET805C988C Metadata: aws:cdk:path: RestStack/myservice-api/Deployment/Resource myserviceapiDeploymentStageprodB550FDE9: Type: AWS::ApiGateway::Stage Properties: RestApiId: Ref: myserviceapi226E6B58 DeploymentId: Ref: myserviceapiDeployment64624B21f95e073b8b06b61d3c084e7dd1be6a99 StageName: prod Metadata: aws:cdk:path: RestStack/myservice-api/DeploymentStage.prod/Resource myserviceapiCloudWatchRole2422BDFA: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Statement: - Action: sts:AssumeRole Effect: Allow Principal: Service: apigateway.amazonaws.com Version: '2012-10-17' ManagedPolicyArns: - Fn::Join: - '' - - 'arn:' - Ref: AWS::Partition - ":iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs" Metadata: LambdaHandler212865DC: Type: AWS::Lambda::Function Properties: Code: S3Bucket: Ref: LambdaHandlerCodeS3Bucket29322DA5 S3Key: Fn::Join: - '' - - Fn::Select: - 0 - Fn::Split: - "||" - Ref: LambdaHandlerCodeS3VersionKey948E7B0F - Fn::Select: - 1 - Fn::Split: - "||" - Ref: LambdaHandlerCodeS3VersionKey948E7B0F Handler: index.main Role: Fn::GetAtt: - LambdaHandlerServiceRole8F7B4955 - Arn Runtime: nodejs8.10 Environment: Variables: BUCKET: Ref: MyServiceStoreEE9BC9A0 DependsOn: - LambdaHandlerServiceRoleDefaultPolicy52DB1F25 - LambdaHandlerServiceRole8F7B4955 Metadata: aws:cdk:path: RestStack/LambdaHandler/Resource aws:asset:path: asset.10b682bad89f14de23059d787528e0fd16df9824892a35b89e9352a338df8d20 aws:asset:property: Code LambdaHandlerApiPermissionRestStackmyserviceapiD3EA4862GET15582A73: Type: AWS::Lambda::Permission Properties: Action: lambda:InvokeFunction FunctionName: Fn::GetAtt: - LambdaHandler212865DC - Arn Principal: apigateway.amazonaws.com SourceArn: Fn::Join: - '' - - 'arn:' - Ref: AWS::Partition - ":execute-api:" - Ref: AWS::Region - ":" - Ref: AWS::AccountId - ":" - Ref: myserviceapi226E6B58 - "/" - Ref: myserviceapiDeploymentStageprodB550FDE9 - "/GET/" Metadata: aws:cdk:path: RestStack/LambdaHandler/ ApiPermission.RestStackmyserviceapiD3EA4862.GET.. aws:cdk:path: RestStack/myservice-api/CloudWatchRole/Resource myserviceapiAccountCADB8ED1: Type: AWS::ApiGateway::Account Properties: CloudWatchRoleArn: Fn::GetAtt: - myserviceapiCloudWatchRole2422BDFA - Arn DependsOn: - myserviceapi226E6B58 Metadata: aws:cdk:path: RestStack/myservice-api/Account myserviceapiGET805C988C: Type: AWS::ApiGateway::Method Properties: HttpMethod: GET ResourceId: Fn::GetAtt: - myserviceapi226E6B58 - RootResourceId RestApiId: Ref: myserviceapi226E6B58 AuthorizationType: NONE Integration: IntegrationHttpMethod: POST RequestTemplates: application/json: '{ "statusCode": "200" }' Type: AWS_PROXY Uri: Fn::Join: - '' - - 'arn:' - Ref: AWS::Partition - ":apigateway:" - Ref: AWS::Region - ":lambda:path/2015-03-31/functions/" - Fn::GetAtt: - LambdaHandler212865DC - Arn - "/invocations" Metadata: aws:cdk:path: RestStack/myservice-api/Default/GET/Resource Parameters: LambdaHandlerCodeS3Bucket29322DA5: Type: String Description: S3 bucket for asset "RestStack/LambdaHandler/Code" LambdaHandlerCodeS3VersionKey948E7B0F: Type: String Description: S3 key for asset version "RestStack/LambdaHandler/Code" LambdaHandlerCodeArtifactHash5827C2D8: Type: String Description: Artifact hash for asset "RestStack/LambdaHandler/Code" Outputs: myserviceapiEndpointBB6FABDA: Value: Fn::Join: - '' - - https:// - Ref: myserviceapi226E6B58 - ".execute-api." - Ref: AWS::Region - "." - Ref: AWS::URLSuffix - "/" - Ref: myserviceapiDeploymentStageprodB550FDE9 - "/" A simple Rest endpoint with API Gateway, Lambda, S3 Infrastructure as Code (IaC) Powerful, but really too verbose
  18. Getting Started Install Serverless locally (trust me, it’s better)
 npm

    install serverless —save Create a function in NodeJS (Java and Python supported too)
 serverless create —template=aws-nodejs Deploy service
 serverless deploy done!
  19. Serverless Framework • Fast moving (with a bi-weekly release model)

    • Funded by a 3M investment • Manages service deployments and provisioning • Vendor independent (in a future release) • Auto-provision of resources • Supports for micro-services • Improved plugin management system • Production ready serverless framework The easy, open way to build Serverless applications The Serverless Framework is an open-source CLI for building and deploying serverless applications. With over 6 million deployments handled, the Serverless Framework is the tool developers trust to build cloud applications.
  20. CloudFormation exclude dependencies 280+ plugins Provides support for a number

    of integrations and helpers to ease development and project management, from minification to dependency check, to support for web sockets or third party technologies. Support for dev dependencies exclusion, thus reducing package size and improving function load time, then minimizing cold start issue Resources are created as CloudFormation template embedded in serverless.yml serverless.yml
  21. Who is SAM? “The goal of AWS SAM is to

    define a standard application model for serverless applications.” github.com/awslabs/serverless-application-model
  22. CloudFormation Everything is a CloudFormation primitive within a template. This

    means every new CF functionality is natively supported by SAM cloud formation template and SAM CLI Custom Transform AWS SAM is a custom transform applied to CloudFormation Templates (a macro that processes our template and expands into a CF stack CLI handles packaging AWS SAM CLI handles code packaging and upload to S3, minimizing overhead from code to cloud
  23. SAM ✓ AWS::Serverless Transform
 (CloudFormation) ✓ Pretty similar to Serverless

    Framework’s syntax ✓ Special Serverless resources
 (e.g. AWS::Serverless::Function) ✓ Natively supported by AWS 
 SAM Local 
 (github.com/awslabs/aws-sam-local)
  24. AWS SAM Example - Create a function AWS SAM Example

    (Function) (plus policies, VCP config, DLQ, tracing, etc.)
  25. AWS SAM Example - Create a function AWS SAM Example

    (Traffic Shifting) (plus alarms & pre/post-traffic hooks)
  26. Enhancing Cloud Formation AWS Cloud Development Kit “a framework for

    defining cloud infrastructure in code” https://github.com/awslabs/aws-cdk CloudFornation Custom Transform “support for user defined macros within CF script” https://aws.amazon.com/about-aws/whats-new/ 2018/09/introducing-aws-cloudformation-macros/
  27. All these solutions have in common some issues related to

    CloudFormation direct usage Writing CloudFormation code is uncomfortable • It’s error prone! • Debugging is hard and there is no strong validation • No support for conditional stack creation • No support for multiple creation of the same resource • Extremely verbose
  28. Defining a Serverless app with CDK (nodeJS) rest-stack.js rest-app.js Define

    a REST API with helpers Helpers to support fast resources definition Lambda code is packaged by asset Helpers to support fast resources definition Supports multiple nested stacks Stacks as constructs can be versioned and released as NPM libraries, then composed into an app
  29. Templates are composed using constructs Constructs First level constructs -

    generated directly from CloudFormation primitives - map directly from CloudFormation - Ensure support for any feature CloudFormation provides - start with Cfn<something> AWS Construct Library - provide constructs representing AWS Resources (such as Bucket, Topic or Table) with default configurations - provide helpers to simplify common operations - auto-configuration for required resources - enforce best practices - start with aws<something> Customer Constructs - can be heavily customised, extend cdk.Construct - used to wrap architecture components - used to standardize common patterns - can be released as NPM package
  30. but could be improved on some aspects AWS CDK is

    a game changer technology Simplifies serverless development ✓less boilerplate ✓compilers / interpreters validate stack ✓stack splitting encourages reusability ✓conditional resource creation ✓resource parametrization ✓complex deployment logics ✓construct libraries (https://github.com/eladb/awesome-cdk) #cdkwishlist ✓plugin support ✓lambda code dependency pruning ✓documentation ✓resource preset (what is already available in your account?)
  31. Once upon a time.. ght before Christmas 
 e just

    leaving.. ur manager steps into the office.. …with another great idea: 
 … everyone in your office is gone to the beach.. … the sun is shining, weather is hot, the day is almost over… … then your manager steps into the office, with an idea for a great business product!!
  32. Increase store traffic and digital interactions by retargeting customers and

    drive them to buy, based on their location and app/web/store interaction patterns. IDEA! Make it Serverless • Focus only on your code • No infrastructure to manage (your IT guy has a family, too) Scales up to production requirements • Can be released within minutes • Use DynamoDB and GSI • Serverless does not mean spaghetti-code! adopt a serverless architecture that can scale up to millions and EVOLVE through time
  33. Any complex platform implements a set of different requirements Different

    requirements • Deep Learning models • Integrating with 3rd party products • Different data types to persist • Need for speed and scalability • Team communication has a lot of friction: IT operation and dev teams
  34. Neosperience Cloud Cloud Understand Engage Grow Neosperience Cloud Services •

    Define Cloud Requirements • One endpoint serves multiple requests • It’s called the monolith
  35. Neosperience ( 2008 — 2012 ) The age of the

    monolith • Multi-region deploy of SpringMVC / Java on Apache Tomcat / JBoss • Shared RDS database managed by Hibernate • Autoscaling group for EC2 instances, Elastic IP Pros ✓ Everything within a single package ✓ Simple CI workflow ✓ Easy services coordination Cons - Lifecycle: one change in code requires a full release of Neosperience - Scalability / Costs: planning autoscale for different usages is not easy - Everything is a REST endpoint
  36. Neosperience Cloud Cloud Understand Engage Grow image video conversation relation

    interaction behavior Personalised Content Proximity Marketing Nudging & Gamification Image Personalised Commerce Personalised Advertising Customer Base Channels • Start separating concerns • Every component has the same technological stack • Define interfaces between components • It’s called microservices
  37. image video conversation relation interaction behavior Personalised Content Proximity Marketing

    Nudging & Gamification Image Personalised Commerce Personalised Advertising Customer Base Channels Customer 
 Generations Search Customer CI / CD
  38. Neosperience ( 2012 — 2015 ) Separation of concerns •

    Spring Boot/Cloud on Java Stack • Docker image for each service within NGINX • DynamoDB used as façade towards clients • RDS managed through Spring Data • Coordination service (Module Manager) Pros ✓ Smaller services, same technology for everything ✓ Immutable deployments: from CI to Docker registry ✓ Easy services coordination Cons - Still paying for idle (database, instances) - Manual provisioning of resources (through Beanstalk) - Everything is a REST endpoint
  39. image video conversation relation interaction behavior Personalised Content Proximity Marketing

    Nudging & Gamification Image Personalised Commerce Personalised Advertising Customer Base Channels Customer 
 Generations Search Customer CI / CD • Move to cloud native adoption • Script cloud resources • Each service has its own persistence • Migrate data models Neosperience Cloud
  40. Neosperience ( 2015 — now ) Here comes Serverless •

    Triggers to Lambda functions • Each service defines its own persistence • Communication is handled through Kinesis • Immutable deployments Pros ✓ Many small packages ✓ Extremely fast release cycles (smaller changes) ✓ No servers to manage (woot-woot) ✓ Scalability at its best / Cost reduction ✓ There is no difference between dev/stage/ production Cons - Required a shift in team perspective towards software development (there is no one size fits all architecture) - Expensive when utilization close to 100% - No support for dedicated hardware
  41. Some key points we had to address while moving to

    Serverless and microservices. The questions we faced How micro is a microservice? Decompose your system into domain specific computing units using Domain Driven Development (DDD) Do we want to reinvent the wheel? AWS provides a variety of managed services that can ease out software development, reducing time to market of orders of magnitude. Every time we had to implement a new functionality we asked ourselves whether there was an AWS service for that. How to deal with the outside world? Neosperience is a B2B2C ISV vendor. Our product can be used SaaS by companies or integrated through API. We need to rely on web standards REST and OAuth2 How about vendor lock-in? Serverless does not lock you in. Data does. But it’s the same with languages, tools or frameworks.
  42. Adopting microservices can really make our life as ISV better,

    with a number of benefits Neosperience is a 100% Serverless cloud solution Business Domain Support ✓17 different business domains ✓5-10 microservices each domain ✓a dozen of support services (monitoring, maintenance, OAuth2, Organization, multi-tenancy, etc.) Serverless ✓100% Serverless except for ML model training Lambda Functions ✓200+ functions AWS Resources ✓400+ AWS resources ✓managed through a 15+ CloudFormation stacks Time to market ✓improved from months to weeks ✓business features released every sprint ✓technical features released multiple times a week Costs ✓reduced by an order of magnitude Team ✓developers provision cloud resources ✓innovation in encouraged, failure impact is bounded ✓shifted from running after business requirements to waiting for business requirements Happiness Dev Team has full control on delivery Business Team has feature delivery
  43. “in the past were bigger companies that outcompeted smaller companies

    now are faster companies to outcompete slower companies” — Marc Benioff