Created
August 24, 2019 01:13
-
-
Save mweagle/ba7e7680d551ac82ebec173d2748d747 to your computer and use it in GitHub Desktop.
Code for Serverless Day Challenge on AWS Twitch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"os" | |
"time" | |
gocf "github.com/mweagle/go-cloudformation" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/rdsdataservice" | |
sparta "github.com/mweagle/Sparta" | |
) | |
var dbConn *rdsdataservice.RDSDataService | |
func init() { | |
sess, _ := session.NewSession() | |
dbConn = rdsdataservice.New(sess) | |
} | |
// MunnsNews is the content type | |
type MunnsNews struct { | |
ID string | |
ParentID string | |
Timestamp time.Time | |
Content string | |
UserID string | |
} | |
func addEntry(ctx context.Context, request MunnsNews) (string, error) { | |
return "Hello World π. Welcome to Munns News! πππΎ", nil | |
} | |
func listEntry(ctx context.Context, request MunnsNews) (interface{}, error) { | |
input := &rdsdataservice.ExecuteStatementInput{ | |
SecretArn: aws.String("arn:aws:secretsmanager:us-west-2:123412341234:secret:rds-db-credentials/cluster-BMUTEGQB5A7JVSQXLKPL753HMM/admin-T8oyJ0"), | |
ResourceArn: aws.String("arn:aws:rds:us-west-2:123412341234:cluster:news-2"), | |
Database: aws.String("news"), | |
Sql: aws.String("SELECT * from `comments`"), | |
} | |
results, resultsErr := dbConn.ExecuteStatement(input) | |
if resultsErr != nil { | |
return "", resultsErr | |
} | |
return *results, nil | |
} | |
func deleteEntry(ctx context.Context, request MunnsNews) (string, error) { | |
return "Hello World π. Welcome to Munns News! πππΎ", nil | |
} | |
func rdsDataPermissions() []sparta.IAMRolePrivilege { | |
return []sparta.IAMRolePrivilege{ | |
{ | |
Actions: []string{"rds-data:BatchExecuteStatement", | |
"rds-data:BeginTransaction", | |
"rds-data:CommitTransaction", | |
"rds-data:ExecuteStatement", | |
"rds-data:ExecuteSql", | |
"rds-data:RollbackTransaction"}, | |
Resource: gocf.String("*"), | |
}, | |
{ | |
Actions: []string{"secretsmanager:*"}, | |
Resource: gocf.String("*"), | |
}, | |
} | |
} | |
//////////////////////////////////////////////////////////////////////////////// | |
// Main | |
func main() { | |
iamRole := sparta.IAMRoleDefinition{} | |
iamRole.Privileges = append(iamRole.Privileges, rdsDataPermissions()...) | |
// 1. Lambda function | |
addFn, _ := sparta.NewAWSLambda("AddEntry", | |
addEntry, | |
iamRole) | |
delFn, _ := sparta.NewAWSLambda("DeleteEntry", | |
deleteEntry, | |
iamRole) | |
listFn, _ := sparta.NewAWSLambda("ListEntries", | |
listEntry, | |
iamRole) | |
// Sanitize the name so that it doesn't have any spaces | |
var lambdaFunctions []*sparta.LambdaAWSInfo | |
lambdaFunctions = append(lambdaFunctions, addFn, delFn, listFn) | |
err := sparta.MainEx("Twitch", | |
"Twitch coding exercise", | |
lambdaFunctions, | |
nil, | |
nil, | |
nil, | |
false) | |
if err != nil { | |
os.Exit(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment