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

Going Serveless on AWS with Golang and SAM

gtourkas
November 27, 2019

Going Serveless on AWS with Golang and SAM

Intro to AWS Fully Managed (aka "Serverless") Services for Developers focusing on Lambda. Development Lifecycle (Code, Locally Run/Debug, Deploy) of Golang Lambda Handlers/Functions with SAM. Basic Performance and Cost analysis.

gtourkas

November 27, 2019
Tweet

Other Decks in Programming

Transcript

  1. GOING SERVERLESS ΟΝ AWS WITH GOLANG & SAM 7th Athens

    Gophers Meetup George Tourkas (@georgetourkas)
  2. SERVERLESS AWS FULLY MANAGED SERVICES ▸ Proprietary to the Cloud

    Provider ▸ No Hardware/OS(-like) Management ▸ Built-in HA ▸ (Semi-)Auto Scalability ▸ Limited/Zero Idle Capacity ▸ Seamless Integration with other Services ▸ CLI and API
  3. SERVERLESS AWS S3 (Simple Storage Service) ▸ Object Storage Service

    ▸ Static Content HTTP Server ▸ Lifecycle Rules ▸ Event Notifications ▸ Strong Consistency on Creations ▸ Eventual on Updates/Deletions
  4. SERVERLESS AWS DynamoDB ▸ Key-Value and Document DB Service ▸

    Option for On Demand or Provisioned Read/ Write Capacity ▸ Within/Across-Tables ACID ▸ Option for Strong or Eventual Consistent on Reads ▸ Table Activity Stream
  5. SERVERLESS AWS API Gateway ▸ HTTP API Service ▸ Caching

    on Headers, Query Params ▸ Stages (Prod, Acc etc.) ▸ Websocket Support ▸ Request Throttling ▸ WAF, HTTPS only
  6. SERVERLESS AWS API Gateway ▸ SDK Generation ▸ API Keys/Usage

    Plans Management ▸ Export to Swagger and OpenAPI 3 ▸ Documentation Construction Helper
  7. SERVERLESS AWS SNS (Simple Notification Service) ▸ Pub/Sub Service ▸

    Attribute-based Filter (Routing) ▸ Reliable Delivery ▸ Endpoints: ▸ HTTP/HTTPS ▸ Email ▸ SMS ▸ Push Notifications (iOS, Android etc.)
  8. SERVERLESS AWS Lambda ▸ Function(=Code Execution) Service ▸ Custom Memory

    Allocation (128-3008MB in 64 MB incr.) ▸ Proportional Alloc of vCPU, Net & Disk I/O ▸ 1000 Concurrent Execs / 3000 in Burst Per Account ▸ Max Timeout 15 minutes
  9. SERVERLESS AWS Lambda ▸ Invoked by: ▸ API Gateway Requests

    (sync) ▸ S3 Event Notifications (async) ▸ SNS Messages (async) ▸ DynamoDB Stream (poll-based) ▸ other Lambdas (sync or async) ▸ …
  10. SERVERLESS AWS LAMBDA LIFECYLE / IMPLICATIONS ▸ No/Limited Usability of

    In-Process State ▸ In-Process Caching ▸ Connection-Oriented Dependencies
  11. SERVERLESS AWS IAM (Identity & Access Management) ▸ Centralised Management

    of Authentication & Authorisation Service ▸ “Policy” = Allowed/Denied set of Actions on set of Service Resources
 
 Effect: Allow, Action:S3/GetObject, Resource: my-bucket ▸ Associated with Users, Lambdas etc.
  12. SERVERLESS AWS SAM (Serverless Application Model) ▸ Serverless App Development

    Framework ▸ Locally Run & Debug ▸ IDE Integration ▸ Single (=Code + Infra) Deployment Configuration
  13. CODE & INFRA AWS GO SDK 1. Create a “Session”

    2. Create a Service Client (S3, DynamoDB etc.) 3. ServiceClient.DoSth(*DoSthInput): (*DoSthOutput, err)
 or
 DoSthWithContext
  14. CODE & INFRA GOLANG LAMBDA SERVICE TIn TOut API GW

    events.APIGateway ProxyRequest events.APIGateway ProxyResponse SNS events.SNSEvent N/A S3 events.S3Event N/A DYNAMODB events.DynamoDB Event N/A
  15. CODE & INFRA GOLANG LAMBDA LIFECYLE Source: Anatomy of AWS

    Lambda Damian Sosnowski init() main()
  16. CODE & INFRA LAMBDA LIFECYLE / REUSE PROCESSES Source: Anatomy

    of AWS Lambda Damian Sosnowski init() main()
  17. CODE & INFRA LAMBDA LIFECYLE / REUSE PROCESSES Source: Anatomy

    of AWS Lambda Damian Sosnowski init() main() main()
  18. CODE & INFRA LAMBDA LIFECYLE / REUSE PROCESSES Source: Anatomy

    of AWS Lambda Damian Sosnowski init() main() main() main()
  19. CODE & INFRA LAMBDA LIFECYLE / REUSE PROCESSES Source: Anatomy

    of AWS Lambda Damian Sosnowski init() main() main() main() main()
  20. CODE & INFRA ONE OR MANY HANDLERS ? ▸ Many

    Handlers: ▸ Smaller/Faster Initialisation ▸ Granular Monitor ▸ Reserve Concurrency
  21. CODE & INFRA ONE OR MANY HANDLERS ? ▸ One

    Handler: ▸ Migrating from Standalone ▸ Complex Routing ▸ Middleware on http.HandlerFunc
  22. CODE & INFRA AWS LAMBDA GO API PROXY ▸ Adapters

    for: ▸ net/http ▸ gorilla mux ▸ iris ▸ negroni ▸ gin https://github.com/awslabs/aws-lambda-go-api-proxy 
 by AWS Labs
  23. DEBUG LOCALLY TYPICAL DEBUG PROCESS 1. compile 2. start the

    debugger 3. “connect” IDE to the debugger process 4. make the (HTTP) call
 
 .. to hit a breakpoint
  24. DEBUG LOCALLY TYPICAL DEBUG PROCESS 1. compile 2. start the

    debugger 3. “connect” IDE to the debugger process 4. make the (HTTP) call
 
 .. to hit a breakpoint
  25. DEBUG LOCALLY SAM DEBUG PROCESS 1. compile 2. start the

    debugger 3. make the (HTTP) call 4. “connect” IDE to the debugger process
 
 .. to hit a breakpoint
  26. DEBUG LOCALLY SAM DEBUG PROCESS 1. compile 2. start the

    debugger 3. make the (HTTP) call 4. “connect” IDE to the debugger process
 
 .. to hit a breakpoint
  27. DEPLOY GRADUAL DEPLOYMENT ▸ Canary, Linear X Percent Every Y

    Minutes ▸ Metric-based Alarms for Rollback
  28. COST LAMBDA ▸ Request Charges:
 
 Requests * $0.0000002 ▸

    Compute Charges:
 
 Duration in Secs * GB Mem / 1024 * $0.00001667
 

  29. COST LAMBDA ▸ Request Charges:
 
 30M * $0.0000002 =

    $6 ▸ Compute Charges:
 
 30M * 0.200 * 192 / 1024 * $0.00001667 = $18.75 ▸ Total Charges = $24.75

  30. COST LAMBDA ▸ Request Charges:
 
 30M * $0.0000002 =

    $6 ▸ Compute Charges:
 
 30M * 0.200 * 192 / 1024 * $0.00001667 = $18.75 ▸ Total Charges = $24.75

  31. COST AWS LAMBDA POWER TUNING ▸ Deployable via SAM ▸

    Tuning Strategies: ▸ Speed ▸ Cost ▸ Balanced https://github.com/alexcasalboni/aws-lambda-power-tuning 
 by Alex Casalboni
  32. COST TRANSFORM - NOT TRANSPORT Lambda Cost at 300 ms

    = $0.0000083 Lambda Cost at 200 ms = $0.0000063 Diff = $0,000002 DynamoDB Stream Read Request = $0,000000226
  33. COST COMPARISON SOLUTION MONTHLY COST ($) API GW + LAMBDA

    105.00 + 24.75 129.75 ALB + LAMBDA 21.03 + 24.75 45.78 ALB + EC2* 21.03 + 67.94 88.97 * 2 x t2.medium
  34. COST COMPARISON SOLUTION MONTHLY COST ($) API GW + LAMBDA

    105.00 + 24.75 129.75 ALB + LAMBDA 21.03 + 24.75 45.78 ALB + EC2* 21.03 + 67.94 88.97 * 2 x t2.medium
  35. COST COMPARISON SOLUTION MONTHLY COST ($) API GW + LAMBDA

    105.00 + 24.75 129.75 ALB + LAMBDA 21.03 + 24.75 45.78 ALB + EC2* 21.03 + 67.94 88.97 * 2 x t2.medium
  36. COST FREE TIER SERVICE FREE TIER DURATION S3 5 GB,

    20K GET, 5K PUT 12 MONTHS DYNAMODB 25 GB, 25 RCU, 25 WCU ALWAYS API GW 1 MILLION CALLS/MON 12 MONTHS SNS 1 MILLION PUB/MON 100K HTTP(S) DELIVERIES ALWAYS LAMBDA 1 MILLION EXECS/MON ALWAYS
  37. Q&A