Deploying Serverless Apps With Terraform

whoami

James Strong

Technical Principal @ Contino

@strongjz

https://jamesstrong.dev

Agenda

  1. Serverless
  2. AWS Serverless Application Model (SAM)
  3. Development
  4. Terraform deployment
  5. Demo Slack Bucks

Architecture

Slack Bucks Architecture

Serverless

Image Credit

AWS Serverless Application Model (SAM)

AWS SAM

AWS SAM template specification

   Transform: 'AWS::Serverless-2016-10-31'
   Resources:
   
     ThumbnailFunction:
       # This resource creates a Lambda function.
       Type: 'AWS::Serverless::Function'
       
       Properties:
         
         # This function uses the Nodejs v6.10 runtime.
         Runtime: nodejs6.10
           
         # This is the Lambda function's handler.
         Handler: index.handler
         
         # The location of the Lambda function code.
         CodeUri: ./src
         
         # Event sources to attach to this function. In this case, we are attaching
         # one API Gateway endpoint to the Lambda function. The function is
         # called when a HTTP request is made to the API Gateway endpoint.
         Events:
   
           ThumbnailApi:
               # Define an API Gateway endpoint that responds to HTTP GET at /thumbnail
               Type: Api
               Properties:
                   Path: /thumbnail
                   Method: GET

AWS SAM CLI

Reference

  • sam build
  • sam deploy
  • sam init
  • sam local generate-event
  • sam local invoke
  • sam local start-api
  • sam local start-lambda
  • sam logs
  • sam package
  • sam publish
  • sam validate

AWS SAM CLI

  • sam local generate-event
  • sam local invoke
  • sam local start-api
  • sam local start-lambda

Lambda

Lambda

“AWS Lambda is a compute resource that runs your code in response to events and automatically manages the compute resources for you, making it easy to build applications that respond quickly to new information”

Lambda

  • Code: s3
  • Metadata: DynamoDB
  • Execution: EC2 instances
  • Permissions: IAM Roles

Lambda

  • Function
  • Resources Allocation
  • Code
  • Event
  • Trigger

Limits

Lambda

“Run and forget”

Lambda

Drawback: Does NOT respond to HTTP Events

AWS API Gateway

AWS API Gateway

  • Create REST APIs
  • Create WebSocket APIs

AWS API Gateway

  • Endpoint
  • Methods
  • Integration - Backend

AWS API Gateway Integrations

  • Lambda
  • HTTP
  • S3 Proxy
  • Kinesis Proxy

AWS API Gateway Endpoints

  • Edge Optimized
  • Regional
  • Private API

AWS API Gateway Methods

  • HTTP Method
  • Request Parameters
  • Request Model
  • Request Authorization
  • Request Validation

AWS API Gateway Authorization

  • IAM roles and policies (AWS_IAM)
  • Amazon Cognito user pool (COGNITO_USER_POOLS)
  • A Lambda authorizer (CUSTOM)

Dynamodb

Dynamodb

Serverless Web scale NOSQL AWS managed database.

S3

s3

Web Scale Storage

Serverless Development

Lambda setup

There’s three basic options for structuring this using AWS Lambda:

  • Monolith — One lambda function manages all the API actions.
  • Service — A single lambda function is responsible for a group of related actions.
  • Microservice — A single lambda function is responsible for only one action.

Lambda setup - Monolith

Lambda setup - Service

Lambda setup - Microservice

Ngrok

Ngrok

Spend more time programming. One command for an instant, secure URL to your localhost server through any NAT or firewall.

https://ngrok.com/product

Ngrok

Ngrok

AWS SAM

Install Guide

brew upgrade
brew update
brew tap aws/tap
brew install aws-sam-cli

First, we install the Lambda go libraries

go get github.com/aws/aws-sdk-go
go get github.com/aws/aws-lambda-go/events
go get github.com/aws/aws-lambda-go/lambda

Next, we install the core library

go get github.com/awslabs/aws-lambda-go-api-proxy
go get github.com/awslabs/aws-lambda-go-api-proxy/gin

AWS Dynamodb Local

AWS Dynamodb Local

AWS Dynamodb Local Running

docker run -p 8000:8000 amazon/dynamodb-local:1.11.477

Deployment

AWS Serverless Application Model

This is great if we stick to the AWS Cloudformation

Slack Bucks Architecture

Terraform code

https://github.com/strongjz/slack-bucks/blob/master/terraform/

Demo

https://github.com/strongjz/slack-bucks

Closing remarks

@strongjz

strong.james.e@gmail.com

https://jamesstrong.dev