-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathdoc_usercustomresource_test.go
80 lines (66 loc) · 2.15 KB
/
doc_usercustomresource_test.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
74
75
76
77
78
79
80
package sparta
import (
"context"
"github.com/aws/aws-lambda-go/lambdacontext"
gof "github.com/awslabs/goformation/v5/cloudformation"
goflambda "github.com/awslabs/goformation/v5/cloudformation/lambda"
spartaCFResources "github.com/mweagle/Sparta/v3/aws/cloudformation/resources"
"github.com/rs/zerolog"
)
// Standard AWS λ function
func helloWorld(ctx context.Context,
props map[string]interface{}) (string, error) {
lambdaCtx, _ := lambdacontext.FromContext(ctx)
Logger().Info().
Str("RequestID", lambdaCtx.AwsRequestID).
Interface("Properties", props).
Msg("Lambda event")
return "Event processed", nil
}
// User defined λ-backed CloudFormation CustomResource
func userDefinedCustomResource(ctx context.Context,
event spartaCFResources.CloudFormationLambdaEvent) (map[string]interface{}, error) {
logger, _ := ctx.Value(ContextKeyLogger).(*zerolog.Logger)
lambdaCtx, _ := lambdacontext.FromContext(ctx)
var opResults = map[string]interface{}{
"CustomResourceResult": "Victory!",
}
opErr := spartaCFResources.SendCloudFormationResponse(lambdaCtx,
&event,
opResults,
nil,
logger)
return opResults, opErr
}
func ExampleLambdaAWSInfo_RequireCustomResource() {
lambdaFn, _ := NewAWSLambda(LambdaName(helloWorld),
helloWorld,
IAMRoleDefinition{})
cfResName, _ := lambdaFn.RequireCustomResource(IAMRoleDefinition{},
userDefinedCustomResource,
nil,
nil)
lambdaFn.Decorator = func(ctx context.Context,
serviceName string,
lambdaResourceName string,
lambdaResource *goflambda.Function,
resourceMetadata map[string]interface{},
lambdaFunctionCode *goflambda.Function_Code,
buildID string,
template *gof.Template,
logger *zerolog.Logger) (context.Context, error) {
// Pass CustomResource outputs to the λ function
resourceMetadata["CustomResource"] = gof.GetAtt(cfResName, "CustomResourceResult")
return ctx, nil
}
var lambdaFunctions []*LambdaAWSInfo
lambdaFunctions = append(lambdaFunctions, lambdaFn)
mainErr := Main("SpartaUserCustomResource",
"Uses a user-defined CloudFormation CustomResource",
lambdaFunctions,
nil,
nil)
if mainErr != nil {
panic("Failed to invoke sparta.Main: %s" + mainErr.Error())
}
}