@@ -4,15 +4,22 @@ package cfn
4
4
5
5
import (
6
6
"context"
7
+ "encoding/json"
8
+ "errors"
7
9
"log"
8
10
"net/http"
9
11
12
+ "github.com/aws/aws-lambda-go/events"
10
13
"github.com/aws/aws-lambda-go/lambdacontext"
11
14
)
12
15
13
16
// CustomResourceLambdaFunction is a standard form Lambda for a Custom Resource.
14
17
type CustomResourceLambdaFunction func (context.Context , Event ) (reason string , err error )
15
18
19
+ // SNSCustomResourceLambdaFunction is a standard form Lambda for a Custom Resource
20
+ // that is triggered via a SNS topic.
21
+ type SNSCustomResourceLambdaFunction func (context.Context , events.SNSEvent ) (reason string , err error )
22
+
16
23
// CustomResourceFunction is a representation of the customer's Custom Resource function.
17
24
// LambdaWrap will take the returned values and turn them into a response to be sent
18
25
// to CloudFormation.
@@ -73,3 +80,24 @@ func lambdaWrapWithClient(lambdaFunction CustomResourceFunction, client httpClie
73
80
func LambdaWrap (lambdaFunction CustomResourceFunction ) (fn CustomResourceLambdaFunction ) {
74
81
return lambdaWrapWithClient (lambdaFunction , http .DefaultClient )
75
82
}
83
+
84
+ // LambdaWrapSNS wraps a Lambda handler with support for SNS-based custom
85
+ // resources. Usage and purpose otherwise same as LambdaWrap().
86
+ func LambdaWrapSNS (lambdaFunction CustomResourceFunction ) SNSCustomResourceLambdaFunction {
87
+ inner := LambdaWrap (lambdaFunction )
88
+ return func (ctx context.Context , event events.SNSEvent ) (reason string , err error ) {
89
+ if len (event .Records ) != 1 {
90
+ err = errors .New ("expected exactly 1 incoming record" )
91
+ return
92
+ }
93
+
94
+ message := event .Records [0 ].SNS .Message
95
+
96
+ var innerEvent Event
97
+ if err = json .Unmarshal ([]byte (message ), & innerEvent ); err != nil {
98
+ return
99
+ }
100
+
101
+ return inner (ctx , innerEvent )
102
+ }
103
+ }
0 commit comments