Skip to content

Instantly share code, notes, and snippets.

@marioarizaj
marioarizaj / serverless-policy.json
Last active January 18, 2021 10:56
Serverless policy for IAM user
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"cloudformation:List*",
"cloudformation:Get*",
"cloudformation:ValidateTemplate"
],
CREATE DATABASE serverless_example;
-- Replace 'test-password' with a secure password of your choice
CREATE USER srv_serverless WITH PASSWORD 'test-password';
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO srv_serverless;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO srv_serverless;
create table authors
(
id serial not null primary key ,
first_name text not null,
.PHONY: build clean deploy
build:
go get ./...
go mod vendor
env GOOS=linux go build -ldflags="-s -w" -o bin/hello hello/main.go
env GOOS=linux go build -ldflags="-s -w" -o bin/world world/main.go
clean:
rm -rf ./bin ./vendor
service: serverless-example
frameworkVersion: '2'
provider:
name: aws
runtime: go1.x
lambdaHashingVersion: 20201221
environment:
DB_NAME: ${ssm:/db-name~true}
type StorageIFace interface {
}
type Storage struct {
db *pg.DB
}
var sto StorageIFace
package models
import "time"
type Author struct {
ID int `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
package models
import "time"
type Article struct {
ID int `json:"id"`
Name string `json:"name"`
AuthorID int `json:"author_id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
package common
import "github.com/marioarizaj/serverless-example/models"
func (s *Storage) CreateAuthor(author *models.Author) (*models.Author, error) {
_, err := s.db.Model(author).Insert(author)
if err != nil {
return nil, err
}
return author, nil
type StorageIFace interface {
CreateAuthor(author *models.Author) (*models.Author, error)
GetAllAuthors() ([]models.Author, error)
CreateArticle(article *models.Article) (*models.Article, error)
GetAllArticles() ([]models.Article, error)
}
// Code down here does not change
package main
import (
"encoding/json"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/marioarizaj/serverless-example/api/common"
"github.com/marioarizaj/serverless-example/models"
)