@@ -2,6 +2,7 @@ package lambda
22
33import (
44 "context"
5+ "encoding/base64"
56 "errors"
67 "fmt"
78
@@ -152,3 +153,98 @@ func listFunctions(ctx context.Context, client *lambda.Client) ([]types.Function
152153
153154 return functions , nil
154155}
156+
157+ // Common errors for Lambda execution.
158+ var (
159+ ErrInvokeFunction = errors .New ("failed to invoke function" )
160+ )
161+
162+ // LambdaExecuteOperation represents an operation to execute a Lambda function.
163+ type LambdaExecuteOperation struct {
164+ profile string
165+ region string
166+ }
167+
168+ // NewLambdaExecuteOperation creates a new Lambda execute operation.
169+ func NewLambdaExecuteOperation (profile , region string ) * LambdaExecuteOperation {
170+ return & LambdaExecuteOperation {
171+ profile : profile ,
172+ region : region ,
173+ }
174+ }
175+
176+ // Name returns the operation's name.
177+ func (o * LambdaExecuteOperation ) Name () string {
178+ return "Execute Function"
179+ }
180+
181+ // Description returns the operation's description.
182+ func (o * LambdaExecuteOperation ) Description () string {
183+ return "Execute Lambda Function with JSON Payload"
184+ }
185+
186+ // IsUIVisible returns whether this operation should be visible in the UI.
187+ func (o * LambdaExecuteOperation ) IsUIVisible () bool {
188+ return true
189+ }
190+
191+ // ExecuteFunction executes a Lambda function with the given payload.
192+ func (o * LambdaExecuteOperation ) ExecuteFunction (ctx context.Context , functionName string , payload string ) (* cloud.LambdaExecuteResult , error ) {
193+ // Create a new AWS SDK client
194+ client , err := getClient (ctx , o .profile , o .region )
195+ if err != nil {
196+ return nil , err
197+ }
198+
199+ // Invoke the function
200+ input := & lambda.InvokeInput {
201+ FunctionName : aws .String (functionName ),
202+ Payload : []byte (payload ),
203+ LogType : types .LogTypeTail , // Include logs in the response
204+ }
205+
206+ output , err := client .Invoke (ctx , input )
207+ if err != nil {
208+ return nil , fmt .Errorf ("%w: %w" , ErrInvokeFunction , err )
209+ }
210+
211+ // Decode the base64-encoded logs
212+ logResult := ""
213+ if output .LogResult != nil {
214+ decodedLogs , err := base64 .StdEncoding .DecodeString (* output .LogResult )
215+ if err == nil {
216+ logResult = string (decodedLogs )
217+ }
218+ }
219+
220+ // Convert the payload to a string
221+ payloadStr := ""
222+ if output .Payload != nil {
223+ payloadStr = string (output .Payload )
224+ }
225+
226+ // Create the result
227+ result := & cloud.LambdaExecuteResult {
228+ StatusCode : int (output .StatusCode ),
229+ ExecutedVersion : aws .ToString (output .ExecutedVersion ),
230+ Payload : payloadStr ,
231+ LogResult : logResult ,
232+ }
233+
234+ return result , nil
235+ }
236+
237+ // Execute executes the operation with the given parameters.
238+ func (o * LambdaExecuteOperation ) Execute (ctx context.Context , params map [string ]interface {}) (interface {}, error ) {
239+ functionName , ok := params ["functionName" ].(string )
240+ if ! ok {
241+ return nil , fmt .Errorf ("function name is required" )
242+ }
243+
244+ payload , ok := params ["payload" ].(string )
245+ if ! ok {
246+ return nil , fmt .Errorf ("payload is required" )
247+ }
248+
249+ return o .ExecuteFunction (ctx , functionName , payload )
250+ }
0 commit comments