/
main.go
73 lines (63 loc) · 1.83 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"context"
"fmt"
_ "net/http/pprof" // include pprop
"os"
"github.com/aws/aws-sdk-go/aws/session"
sparta "github.com/mweagle/Sparta"
spartaCF "github.com/mweagle/Sparta/aws/cloudformation"
"github.com/sirupsen/logrus"
)
/*
Supported signatures
• func ()
• func () error
• func (TIn), error
• func () (TOut, error)
• func (context.Context) error
• func (context.Context, TIn) error
• func (context.Context) (TOut, error)
• func (context.Context, TIn) (TOut, error)
*/
// Standard AWS λ function
func helloWorld(ctx context.Context) (string, error) {
logger, loggerOk := ctx.Value(sparta.ContextKeyLogger).(*logrus.Logger)
if loggerOk {
logger.Info("Accessing structured logger 🙌")
}
contextLogger, contextLoggerOk := ctx.Value(sparta.ContextKeyRequestLogger).(*logrus.Entry)
if contextLoggerOk {
contextLogger.Info("Accessing request-scoped log, with request ID field")
} else if loggerOk {
logger.Warn("Failed to access scoped logger")
} else {
fmt.Printf("Failed to access any logger")
}
return "Hello World 👋. Welcome to AWS Lambda! 🙌🎉🍾", nil
}
////////////////////////////////////////////////////////////////////////////////
// Main
func main() {
lambdaFn := sparta.HandleAWSLambda("Hello World",
helloWorld,
sparta.IAMRoleDefinition{})
sess := session.Must(session.NewSession())
awsName, awsNameErr := spartaCF.UserAccountScopedStackName("MyHelloWorldStack",
sess)
if awsNameErr != nil {
fmt.Print("Failed to create stack name\n")
os.Exit(1)
}
// Sanitize the name so that it doesn't have any spaces
var lambdaFunctions []*sparta.LambdaAWSInfo
lambdaFunctions = append(lambdaFunctions, lambdaFn)
err := sparta.Main(awsName,
"Simple Sparta application that demonstrates core functionality",
lambdaFunctions,
nil,
nil)
if err != nil {
os.Exit(1)
}
}