github.com/mweagle/Sparta@v1.15.0/doc_usercustomresource_test.go (about) 1 package sparta 2 3 import ( 4 "context" 5 6 "github.com/aws/aws-lambda-go/lambdacontext" 7 spartaCFResources "github.com/mweagle/Sparta/aws/cloudformation/resources" 8 gocf "github.com/mweagle/go-cloudformation" 9 "github.com/sirupsen/logrus" 10 ) 11 12 // Standard AWS λ function 13 func helloWorld(ctx context.Context, 14 props map[string]interface{}) (string, error) { 15 lambdaCtx, _ := lambdacontext.FromContext(ctx) 16 Logger().WithFields(logrus.Fields{ 17 "RequestID": lambdaCtx.AwsRequestID, 18 "Properties": props, 19 }).Info("Lambda event") 20 return "Event processed", nil 21 } 22 23 // User defined λ-backed CloudFormation CustomResource 24 func userDefinedCustomResource(ctx context.Context, 25 event spartaCFResources.CloudFormationLambdaEvent) (map[string]interface{}, error) { 26 27 logger, _ := ctx.Value(ContextKeyLogger).(*logrus.Logger) 28 lambdaCtx, _ := lambdacontext.FromContext(ctx) 29 30 var opResults = map[string]interface{}{ 31 "CustomResourceResult": "Victory!", 32 } 33 34 opErr := spartaCFResources.SendCloudFormationResponse(lambdaCtx, 35 &event, 36 opResults, 37 nil, 38 logger) 39 return opResults, opErr 40 } 41 42 func ExampleLambdaAWSInfo_RequireCustomResource() { 43 44 lambdaFn, _ := NewAWSLambda(LambdaName(helloWorld), 45 helloWorld, 46 IAMRoleDefinition{}) 47 48 cfResName, _ := lambdaFn.RequireCustomResource(IAMRoleDefinition{}, 49 userDefinedCustomResource, 50 nil, 51 nil) 52 53 lambdaFn.Decorator = func(serviceName string, 54 lambdaResourceName string, 55 lambdaResource gocf.LambdaFunction, 56 resourceMetadata map[string]interface{}, 57 S3Bucket string, 58 S3Key string, 59 buildID string, 60 cfTemplate *gocf.Template, 61 context map[string]interface{}, 62 logger *logrus.Logger) error { 63 64 // Pass CustomResource outputs to the λ function 65 resourceMetadata["CustomResource"] = gocf.GetAtt(cfResName, "CustomResourceResult") 66 return nil 67 } 68 69 var lambdaFunctions []*LambdaAWSInfo 70 lambdaFunctions = append(lambdaFunctions, lambdaFn) 71 72 Main("SpartaUserCustomResource", 73 "Uses a user-defined CloudFormation CustomResource", 74 lambdaFunctions, 75 nil, 76 nil) 77 }