Build Go Apps That Scale on AWS

Melkey

Melkey

Twitch
5 hours, 21 minutes CC
Build Go Apps That Scale on AWS

Course Description

Use Go to build and deploy scalable applications to AWS. Start by reviewing the fundamentals of the Go programming language. Then, use the AWS CDK to create and deploy your infrastructure from the command line. You’ll gain experience with AWS Lambda, DynamoDB, and API Gateway. Code a scalable end-to-end login and authentication API in Go!

This course and others like it are available as part of our Frontend Masters video subscription.

Preview
Close

Course Details

Published: April 23, 2024

Topics

Learn Straight from the Experts Who Shape the Modern Web

Your Path to Senior Developer and Beyond
  • 200+ In-depth courses
  • 18 Learning Paths
  • Industry Leading Experts
  • Live Interactive Workshops
Get Unlimited Access Now

Table of Contents

Introduction

Section Duration: 31 minutes
  • Introduction
    Melkey discusses the prerequisites and architecture of the application built throughout the course. The Go application is a simple API server with user authentication. Users can log in or create accounts. When a user is authenticated, a JWT is issued. The application will be deployed as a Lambda on AWS.
  • Why Use Go?
    Melkey shares a brief history of Go, a statically typed, compiled language. Go is beginner-friendly and versatile, making it an attractive choice for start-ups and large organizations.
  • Go Community Adoption
    Melkey describes why developers moved from Rust, Python, or other Fullstack frameworks to Go. The reasons include Go's ease of use, robust standard library, and strong type system.

Go Basics

Section Duration: 1 hour, 6 minutes
  • Setup Your First Go Program
    Melkey creates a Go program using the "go run init" command. A go.mod file is generated. The entry point to any Go program is main.go. Variables are introduced, and string, bool, and int types are demonstrated.
  • Arrays & Slices std library
    Melkey demonstrates the differences between Arrays and Slices. Arrays have a fixed length assigned when they are declared. Slices are more flexible because their length can change dynamically. This flexibility does require more memory.
  • Structs & Receivers
    Melkey introduces structs, which are class-like data types in Go. Functions can be added to structs by providing a receiver data type, allowing developers to create more complicated data types through composition. Passing structs as copies versus references is also demonstrated in this lesson.
  • Pointers & References
    Melkey dives deeper into variable references and demonstrates pointers. The ampersand symbol refers to a variable's memory address and allows a pointer to be created. If the pointer is dereferencing, it is also explained, and a demonstration of using a pointer to update the original variable's value is provided.
  • Go Modules & Imports
    Melkey demonstrates how Go modules can be divided into separate folders or packages and imported based on their package name. Capitalization of property names within a module will tell the compiler if the property is public or private.
  • Basic Struct Exercise
    Students are instructed to create a Circle struct with a single field radius of type float64, define a circumference method, and, as a bonus, add a method for calculating the area. The exercise solution can be found on the live_workshop branch linked below.

AWS & CDK

Section Duration: 1 hour, 6 minutes
  • AWS & CDK Setup
    Melkey introduces AWS and the Could Development Kit (CDK). AWS will host the application, and the CDK will allow the AWS infrastructure to be set up with code rather than through the AWS interface. Installation instructions are reviewed. The AWS CLI and CDK setup instructions are in the course repo README.
  • CDK Init
    Melkey discusses how the AWS CDK enables infrastructure as code within a project. The CDK will create and deploy the stack and provide utilities for comparing different states between the current and deployed stack. An initial project is created with the `cdk init` command.
  • CDK Constructs & Stacks
    Melkey explains the difference between CDK constructs and stacks. A stack contains one or more resources. This application will include a Lambda, DynamoDB, and API Gateway. There can be one or more stacks in an application construct.
  • Understanding Lambdas
    Melkey introduces Lambdas, serverless functions that can execute code in the cloud and be invoked by events. An advantage of serverless Lambdas is scalability. When there's an increase in traffic to an application, the underlying infrastructure is already prepared to scale.
  • Creating a Serverless Function
    Melkey creates a serverless function in the lambda package in the application. It receives an event and returns either a string or an error depending on whether a username exists on the event.
  • Bundling the Lambda
    Melkey uses the awslambda package in the CDK to build out the lambda function. The runtime, code payload, and the handler function to invoke are specified.
  • Deploying the Lambda
    Melkey deploys the lambda to AWS using the CDK CLI. The function and environment are bootstrapped, and the code is packaged in a zip file. The `cdk diff` command can compare the current environment with what's being deployed.
  • Testing the Deployment
    Melkey demonstrates how to view the lambda through the AWS CloudFormation console. A sample JSON payload is configured, and the lambda is tested.

DynamoDB & Inserting Users

Section Duration: 50 minutes
  • Database & API Handlers
    Melkey refactors the application to have a better project structure. Packages are created for the app, api, database, and types. The new DynamoDB client service is implemented. The chapter_0 branch can be used as a starting reference for this lesson.
  • Creating the App struct
    Melkey creates the App struct, initializing a new DynamoDB client and API handler. The App is initialized in the top-level main.go file. The project structure is now completely bootstrapped.
  • DynamoDB
    Melkey spends a few minutes discussing DynamoDB. It's a fully managed NoSQL database server that uses key-value stores. It's a fast, scalable database solution for serverless functions. A question about testing locally is also discussed in this lesson.
  • Check For Existing User
    Melkey implements two functions to interact with the DynamoDB service. The first function checks for the existence of the user. The second performs the insert operation, adding a record with the username and a plain-text password. Later in the course, the password will be replaced with a hashed version.
  • Register User API Handler
    Melkey adds the RegisterUserHandler function to the application's API. This function ensures the username and password are provided and uses the database functions to check for the user's existence and insert the user into the database. The AWS CDK logic is updated to create the necessary table in DynamoDB and the stack is updated.
  • Deploying Database and Lambda Changes
    Melkey runs `cdk diff` to compare the changes with the current environment. The changes show an updated lambda deployment and the addition of DynamoDB. The changes are deployed and tested in Cloud Formation on AWS.

API Gateway & Authentication Routes

Section Duration: 1 hour, 5 minutes
  • Using Interfaces to Decouple the DB
    Melkey creates an interface for the database to decouple the database platform from the application. An interface is a collection of function signatures that act as a contract for the application. This allows the underlying database to be changed without affecting application logic. The chapter_1 branch can be used as a starting reference for this lesson.
  • Hashing Passwords
    Melkey adds password hashing to the application. The bcrypt package is added to the project to both hash the plain text password and compare a plain text password with a hashed version.
  • AWS API Gateway
    Melkey introduces the AWS API Gateway. It's a fully managed service that makes it easy to create, publish, maintain, monitor, and secure APIs at scale. A gateway is like a front door for an application to access backend services.
  • Defining the API Gateway Routes
    Melkey defines the routes for the API gateway. Each route has a resource name, the URL path, and the necessary methods like GET or POST. The route resources are added to the gateway with a new lambda integration.
  • Logging In Users
    Melkey begins adding the ability to log in users to the applications. First, a GetUser function is created to return a user from the database.
  • Refactor RegisterUserHandler
    Melkey refactors the RegisterUserHandler function to accept an APIGatewayProxRequest object and return an APIGatewayProxyResponse object. The user data is unmarshaled from the request body, and the error-checking logic is updated.
  • Handling Login Requests
    Melkey implements the LoginUser function, which receives an APIGatewayProxyRequest object and populates a LoginRequest struct from the request body. Error checking is added and a "StatusOK" is returned if the login is successful.The changes are then deployed with CDK
  • Testing with cURL
    Melkey tests the API from the command line using cURL commands. A username and password are sent to the API using a POST method. If CloudWatch Logging errors appear when deploying the application, the `LoggingLevel` property in the go_cdk.go file can be removed, or the instructions mentioned in the lesson can be followed.

JSON Web Tokens & Protected URLs

Section Duration: 31 minutes
  • Creating a JWT
    Melkey introduces JSON Web Tokens (JWTs), which are a URL-safe way of representing secure token-based authentication information between two parties. A CreateToken function generates the JWT and returns it as a string. The chapter_2 branch can be used as a starting reference for this lesson.
  • Middleware Package
    Melkey creates a middleware to act as a wrapper around requests coming to routes only available to logged-in users. When a user requests a protected route, the middleware will extract the JWT from the Authorization headers and ensure the JWT is valid. Once the JWT is validated, the request is passed to the protected route's handler function.
  • Protected Routes
    Melkey adds the protected route to the application and wraps the route's handler function with the middleware. The route details are added to the CDK infrastructure code, and the changes are deployed and tested.

Wrapping Up

Section Duration: 8 minutes
  • Wrapping Up
    Melkey wraps up the course by sharing some Go resources. The final code can be found on the live_workshop branch. The `cdk destroy` command can be run to remove the AWS infrastructure from your account.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now