Slide 1

Slide 1 text

© 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

Slide 2

Slide 2 text

© 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

Slide 3

Slide 3 text

© 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

Slide 4

Slide 4 text

© 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…

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

© 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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

© 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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

© 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

Slide 12

Slide 12 text

© 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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

© 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

Slide 15

Slide 15 text

© 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

Slide 16

Slide 16 text

© 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

Slide 17

Slide 17 text

© 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

Slide 18

Slide 18 text

© 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

Slide 19

Slide 19 text

© 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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

© 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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

© 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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

© 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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

© 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

Slide 28

Slide 28 text

© 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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

© 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).

Slide 33

Slide 33 text

© 2023, Amazon Web Services, Inc. or its affiliates. “init” function for global state 33

Slide 34

Slide 34 text

© 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

Slide 35

Slide 35 text

© 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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

© 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

Slide 38

Slide 38 text

© 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

Slide 39

Slide 39 text

© 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

Slide 40

Slide 40 text

© 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