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

Building Serverless applications with Go - Golang Global Summit 2023

Building Serverless applications with Go - Golang Global Summit 2023

Serverless technologies allow developers to focus on writing code rather than managing infrastructure. In this talk, we will explore how to use the Go programming language for Serverless applications on AWS. Through hands-on examples, use-cases and demos, you will learn how to use Go with AWS Lambda, DynamoDB and other services in the serverless ecosystem. Whether you are a Go developer who wants to build scalable, cost-effective apps, or a Cloud practitioner curious about how to apply Go superpowers to Serverless application development - this session is for you!

Abhishek Gupta

October 30, 2023
Tweet

More Decks by Abhishek Gupta

Other Decks in Programming

Transcript

  1. © 2023, Amazon Web Services, Inc. or its affiliates. ©

    2023, Amazon Web Services, Inc. or its affiliates. Building Serverless applications with Go Abhishek Gupta Principal Developer Advocate Amazon Web Services @abhi_tweeter linkedin/in/abhirockzz
  2. © 2023, Amazon Web Services, Inc. or its affiliates. Why

    Serverless 2 No infrastructure provisioning, no management Automatic scaling Pay for value Highly available and secure
  3. © 2023, Amazon Web Services, Inc. or its affiliates. AWS

    serverless spectrum From Primitives to… (Examples: compute, containers, buses) 3 …Peripherals (Examples: databases, analytics, workflows) Amazon Confidential AWS Lambda AWS App Runner Amazon S3 Amazon API Gateway AWS Fargate Amazon ECS Amazon EFS Amazon EventBridge Amazon SNS Amazon SQS Amazon Aurora Serverless AWS Step Functions AWS Glue Amazon DynamoDB AWS AppSync Amazon OpenSearch Amazon Kinesis Amazon CloudWatch Amazon QuickSight
  4. © 2023, Amazon Web Services, Inc. or its affiliates. What

    is AWS Lambda 4 Event Source Function Services / Other Business logic .NET/C# Node.js, Python, Java, Go, Ruby, PowerShell Bring Your Own Changes in data state Requests to endpoints Changes in resource state Many more…
  5. © 2023, Amazon Web Services, Inc. or its affiliates. ©

    2023, Amazon Web Services, Inc. or its affiliates. Writing AWS Lambda Functions with Go
  6. © 2023, Amazon Web Services, Inc. or its affiliates. What’s

    needed - aws-lambda-go https://github.com/aws/aws-lambda-go
  7. © 2023, Amazon Web Services, Inc. or its affiliates. What’s

    needed – lambda, events package https://github.com/aws/aws-lambda-go/tree/main/lambda https://github.com/aws/aws-lambda-go/tree/main/events
  8. © 2023, Amazon Web Services, Inc. or its affiliates. “Hello

    World” Go function 8 output Handler function lambda “runtime” package main function (entry point) input
  9. © 2023, Amazon Web Services, Inc. or its affiliates. Function

    variations 9 https://docs.aws.amazon.com/lambda/latest/dg/golang-handler.html Using “events” package
  10. © 2023, Amazon Web Services, Inc. or its affiliates. ©

    2023, Amazon Web Services, Inc. or its affiliates. Sample application
  11. © 2023, Amazon Web Services, Inc. or its affiliates. URL

    shortener application in Go U S I N G A W S L A M B D A , A M A Z O N D Y N A M O D B A N D A M A Z O N A P I G A T E W A Y https://dev.to/aws/build-a-serverless-url-shortener-with-go-10i2
  12. © 2023, Amazon Web Services, Inc. or its affiliates. ©

    2023, Amazon Web Services, Inc. or its affiliates. Demo – Implement Go Lambda functions for the URL shortener app
  13. © 2023, Amazon Web Services, Inc. or its affiliates. ©

    2023, Amazon Web Services, Inc. or its affiliates. Amazon DynamoDB
  14. © 2023, Amazon Web Services, Inc. or its affiliates. Highly

    available and durable Consistently fast at any scale Fully managed Secure Integrates with AWS Lambda, Amazon Redshift, and more Amazon DynamoDB Overview Cost-effective
  15. © 2023, Amazon Web Services, Inc. or its affiliates. Partition

    Key Mandatory Key-value access pattern Determines data distribution Optional Model 1:N relationships Enables rich query capabilities DynamoDB Table A1 (partition key) A2 (sort key) A3 A4 A7 A1 (partition key) A2 (sort key) A6 A4 A5 A1 (partition key) A2 (sort key) A1 (partition key) A2 (sort key) A3 A4 A5 Sort Key Table Items All items for key ==, <, >, >=, <= “begins with” “between” “contains” “in” sorted results counts top/bottom N values
  16. © 2023, Amazon Web Services, Inc. or its affiliates. 16

    https://aws.amazon.com/sdk-for-go/ https://aws.github.io/aws-sdk-go-v2/docs/getting-started/ https://github.com/aws/aws-sdk-go-v2
  17. © 2023, Amazon Web Services, Inc. or its affiliates. service

    dynamodb types dynamodbstreams types feature dynamodb attributevalue expression dynamodbstreams attributevalue H T T P S : / / P K G . G O . D E V / G I T H U B . C O M / A W S / A W S - S D K - G O - V 2
  18. © 2023, Amazon Web Services, Inc. or its affiliates. Data

    plane Operations and APIs 18 Operation API Create PutItem BatchWriteItem Read GetItem BatchGetItem Query Scan Update UpdateItem Delete DeleteItem BatchWriteItem Transactions TransactWriteItems TransactGetItems PartiQL ExecuteStatement BatchExecuteStatement ExecuteTransaction
  19. © 2023, Amazon Web Services, Inc. or its affiliates. ©

    2023, Amazon Web Services, Inc. or its affiliates. Demo – Implement persistence module for the URL shortener app
  20. © 2023, Amazon Web Services, Inc. or its affiliates. ©

    2023, Amazon Web Services, Inc. or its affiliates. Packaging and Deployment
  21. © 2023, Amazon Web Services, Inc. or its affiliates. AWS

    Lambda Runtimes 21 go1.x provided.al2 recommended Migrating AWS Lambda functions from the Go1.x runtime to the custom runtime on Amazon Linux 2
  22. © 2023, Amazon Web Services, Inc. or its affiliates. Packaging

    – Zip file 22 GOOS=linux GOARCH=<arm64 or amd64> go build -o bootstrap main.go zip function.zip bootstrap aws lambda create-function …. BUILD 👷 ZIP 📦 DEPLOY 🚀
  23. © 2023, Amazon Web Services, Inc. or its affiliates. Packaging

    – Docker container image C H O O S E T H E A P P R O P R I A T E B A S E I M A G E 23 provided.al2 base image Custom (non AWS) base image
  24. © 2023, Amazon Web Services, Inc. or its affiliates. Packaging

    – Docker container image 24 docker build --platform linux/<amd64 or arm64> -t my-func:v1 . aws ecr create-repository … docker push <ID>.dkr.ecr.us-east-1.amazonaws.com/my-func:v1 aws lambda create-function --package-type Image --code ImageUri=<image>…. BUILD 👷 PUSH 📦 DEPLOY 🚀
  25. © 2023, Amazon Web Services, Inc. or its affiliates. Deployment

    Options • Directly using the AWS Lambda console • AWS CLI – aws lambda create-function • AWS Serverless Application Model (SAM) CLI – sam deploy • Infrastructure as Code § AWS CloudFormation § AWS Cloud Development Kit (CDK) § Terraform § Pulumi 25
  26. © 2023, Amazon Web Services, Inc. or its affiliates. ©

    2023, Amazon Web Services, Inc. or its affiliates. AWS CDK – Infrastructure as Code
  27. © 2023, Amazon Web Services, Inc. or its affiliates. AWS

    Cloud Development Kit (CDK) Your language – just code Autocomplete – inline documentation Sane defaults – reusable classes Java
  28. © 2023, Amazon Web Services, Inc. or its affiliates. Paradigm

    Shift AWS CloudFormation CDK App Source Code Stack A Template A Template B AWS CloudFormation Stack B Parameterized Template Stack 1 Stack 2 CloudFormation Parameters and intrinsic functions CDK Typed OO language: loops, conditions, inheritence, etc
  29. © 2023, Amazon Web Services, Inc. or its affiliates. AWS

    CDK for Go CDK API Reference Documentation
  30. © 2023, Amazon Web Services, Inc. or its affiliates. ©

    2023, Amazon Web Services, Inc. or its affiliates. Demo – Infrastructure as “Go” code
  31. © 2023, Amazon Web Services, Inc. or its affiliates. ©

    2023, Amazon Web Services, Inc. or its affiliates. Patterns and Common Best Practices
  32. © 2023, Amazon Web Services, Inc. or its affiliates. Reusing

    objects across invocations 32 Benefit Efficient runtime. Lower cold starts. Action • Create, initialize, and configure objects expected to be reused across multiple invocations during function initialization instead of doing it in handler (e.g. static constructor).
  33. © 2023, Amazon Web Services, Inc. or its affiliates. AWS

    Lambda Go API Proxy 34 https://community.aws/tutorials/golang-and-aws-lambda/01-golang-apis-on-aws-lambda
  34. © 2023, Amazon Web Services, Inc. or its affiliates. Right-size

    Your Functions 35 Benefit Run the workload at the lowest cost without compromising on performance Action • Use Lambda Power Tuning to identify the optimal memory configuration without increasing cost • Act on suggestions from Compute Optimizer • Reassess after major changes
  35. © 2023, Amazon Web Services, Inc. or its affiliates. Lambda

    Power Tuning 36 https://docs.aws.amazon.com/lambda/latest/operatorguide/profile-functions.html
  36. © 2023, Amazon Web Services, Inc. or its affiliates. Lambda

    Event Filtering 37 Benefit • Reduction in Lambda function invocations • Remove filtering logic from function code Action • Create event sources with filter conditions • Discard irrelevant events with event filters instead of code • Update function code to remove redundant logic
  37. © 2023, Amazon Web Services, Inc. or its affiliates. Event

    Filtering 38 MSG MSG MSG MSG MSG MSG MSG MSG MSG MSG MSG MSG process discard process discard discard process F i l t e r
  38. © 2023, Amazon Web Services, Inc. or its affiliates. Resources

    39 Building Lambda functions with Go Working with the AWS CDK in Go Amazon DynamoDB Examples using the AWS SDK for Go
  39. © 2023, Amazon Web Services, Inc. or its affiliates. ©

    2023, Amazon Web Services, Inc. or its affiliates. Thank you! Abhishek Gupta Principal Developer Advocate Amazon Web Services @abhi_tweeter linkedin/in/abhirockzz