53GB

AWS Serverless Application with Lambda, API gateway, GoLang and Terraform



AWS Serverless Application with Lambda, API gateway, GoLang and Terraform

This tutorial will explain how to create a Serverless web application using API gateway and lambda for your backend logic, S3 for static content hosting.

— Contents of this video —
0:00​ Intro
2:17​ Create GoLang lambda
7:28 Create HTML/JS
11:07​ Create Terraform (API Gateway + lambda)
21:56​ Deploy Terraform
21:55 AWS Console validation
25:04 Local HTML test
26:28 Host HTML in S3
29:25 Final test using HTML in S3

User ► S3 (HTML/JS) ► API gateway ► lambda
Deployment using terraform.

Also don’t forget to add extra permission to your user (for terraform) and role (used ARN) to add permission for PI gateway.

—–
Terraform:
provider “aws” {
region = “us-east-2”
profile = var.profile
}

variable “profile” {
default = “dev-01”
}

################ Lambda ################

// compile the code
resource “null_resource” “compile” {
triggers = {
build_number = timestamp()
}
provisioner “local-exec” {
command = “GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o ../target/lambdabin -ldflags ‘-w’ ../src/lambda.go”
}
}

// zip the code
data “archive_file” “zip” {
type = “zip”
source_file = “../target/lambdabin”
output_path = “../target/lambdabin.zip”
depends_on = [null_resource.compile]
}

resource “aws_lambda_function” “flavio-test-api-gateway” {
function_name = “flavio-test-api-gateway”
handler = “lambdabin”
runtime = “go1.x”
role = “” // TODO put your ARN role in here
filename = data.archive_file.zip.output_path
source_code_hash = data.archive_file.zip.output_base64sha256
memory_size = 128
timeout = 10
}

resource “aws_lambda_permission” “allow_api” {
statement_id = “AllowAPIgatewayInvokation”
action = “lambda:InvokeFunction”
function_name = aws_lambda_function.flavio-test-api-gateway.function_name
principal = “apigateway.amazonaws.com”
}

################ API GATEWAY ################

resource “aws_api_gateway_rest_api” “test-flavio-api-gateway” {
name = “test-flavio-api-gateway”
endpoint_configuration {
types = [“REGIONAL”]
}
}

resource “aws_api_gateway_resource” “person” {
rest_api_id = aws_api_gateway_rest_api.test-flavio-api-gateway.id
parent_id = aws_api_gateway_rest_api.test-flavio-api-gateway.root_resource_id
path_part = “person”
}

// POST
resource “aws_api_gateway_method” “post” {
rest_api_id = aws_api_gateway_rest_api.test-flavio-api-gateway.id
resource_id = aws_api_gateway_resource.person.id
http_method = “POST”
authorization = “NONE”
api_key_required = false
}

resource “aws_api_gateway_integration” “integration” {
rest_api_id = aws_api_gateway_rest_api.test-flavio-api-gateway.id
resource_id = aws_api_gateway_resource.person.id
http_method = aws_api_gateway_method.post.http_method
integration_http_method = “POST”
type = “AWS_PROXY”
uri = aws_lambda_function.flavio-test-api-gateway.invoke_arn
}

// GET
resource “aws_api_gateway_method” “get” {
rest_api_id = aws_api_gateway_rest_api.test-flavio-api-gateway.id
resource_id = aws_api_gateway_resource.person.id
http_method = “GET”
authorization = “NONE”
api_key_required = false
}

resource “aws_api_gateway_integration” “integration-get” {
rest_api_id = aws_api_gateway_rest_api.test-flavio-api-gateway.id
resource_id = aws_api_gateway_resource.person.id
http_method = aws_api_gateway_method.get.http_method
integration_http_method = “POST”
type = “AWS_PROXY”
uri = aws_lambda_function.flavio-test-api-gateway.invoke_arn
}

################ Deployment of API gateway ################

resource “aws_api_gateway_deployment” “deployment1” {
rest_api_id = aws_api_gateway_rest_api.test-flavio-api-gateway.id

triggers = {
redeployment = sha1(jsonencode(aws_api_gateway_rest_api.test-flavio-api-gateway.body))
}

depends_on = [aws_api_gateway_integration.integration]
lifecycle {
create_before_destroy = true
}
}

resource “aws_api_gateway_stage” “example” {
deployment_id = aws_api_gateway_deployment.deployment1.id
rest_api_id = aws_api_gateway_rest_api.test-flavio-api-gateway.id
stage_name = var.profile
}

output “complete_unvoke_url” {value = “${aws_api_gateway_deployment.deployment1.invoke_url}${aws_api_gateway_stage.example.stage_name}/${aws_api_gateway_resource.person.path_part}”}

#GoLang #Terraform #Serverless

Exit mobile version