(lambdaFunction CustomResourceFunction, client httpClient)
| 25 | type CustomResourceFunction func(context.Context, Event) (physicalResourceID string, data map[string]interface{}, err error) |
| 26 | |
| 27 | func lambdaWrapWithClient(lambdaFunction CustomResourceFunction, client httpClient) (fn CustomResourceLambdaFunction) { |
| 28 | fn = func(ctx context.Context, event Event) (reason string, err error) { |
| 29 | r := NewResponse(&event) |
| 30 | |
| 31 | // A previous physical resource id exists unless this is a create request. |
| 32 | fallbackPhysicalResourceID := event.PhysicalResourceID |
| 33 | if event.RequestType == RequestCreate { |
| 34 | // If this is a create request, the fallback should be the request ID |
| 35 | fallbackPhysicalResourceID = event.RequestID |
| 36 | } |
| 37 | |
| 38 | funcDidPanic := true |
| 39 | defer func() { |
| 40 | if funcDidPanic { |
| 41 | r.Status = StatusFailed |
| 42 | r.Reason = "Function panicked, see log stream for details" |
| 43 | r.PhysicalResourceID = fallbackPhysicalResourceID |
| 44 | // FIXME: something should be done if an error is returned here |
| 45 | _ = r.sendWith(client) |
| 46 | } |
| 47 | }() |
| 48 | |
| 49 | r.PhysicalResourceID, r.Data, err = lambdaFunction(ctx, event) |
| 50 | funcDidPanic = false |
| 51 | |
| 52 | if r.PhysicalResourceID == "" { |
| 53 | r.PhysicalResourceID = fallbackPhysicalResourceID |
| 54 | log.Printf("PhysicalResourceID not set. Using fallback PhysicalResourceID: %s\n", r.PhysicalResourceID) |
| 55 | } |
| 56 | |
| 57 | if err != nil { |
| 58 | r.Status = StatusFailed |
| 59 | r.Reason = err.Error() |
| 60 | log.Printf("sending status failed: %s\n", r.Reason) |
| 61 | } else { |
| 62 | r.Status = StatusSuccess |
| 63 | } |
| 64 | |
| 65 | err = r.sendWith(client) |
| 66 | if err != nil { |
| 67 | reason = err.Error() |
| 68 | } |
| 69 | |
| 70 | return |
| 71 | } |
| 72 | |
| 73 | return |
| 74 | } |
| 75 | |
| 76 | // LambdaWrap returns a CustomResourceLambdaFunction which is something lambda.Start() |
| 77 | // will understand. The purpose of doing this is so that Response Handling boiler |
searching dependent graphs…