diff --git a/.changelog/44843.txt b/.changelog/44843.txt new file mode 100644 index 000000000000..2beefdb5130d --- /dev/null +++ b/.changelog/44843.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +action/aws_lambda_invoke: Output logs in a progress message when `log_type` is `Tail` +``` \ No newline at end of file diff --git a/internal/service/lambda/invoke_action.go b/internal/service/lambda/invoke_action.go index 0e0fc0ed2ff2..bf40bf9b0707 100644 --- a/internal/service/lambda/invoke_action.go +++ b/internal/service/lambda/invoke_action.go @@ -173,22 +173,7 @@ func (a *invokeAction) Invoke(ctx context.Context, req action.InvokeRequest, res // Handle different invocation types switch invocationType { case awstypes.InvocationTypeRequestResponse: - // For synchronous invocations, we get an immediate response - statusCode := output.StatusCode - payloadLength := len(output.Payload) - - var message string - if logType == awstypes.LogTypeTail && output.LogResult != nil { - message = fmt.Sprintf("Lambda function %s invoked successfully (status: %d, payload: %d bytes, logs included)", - functionName, statusCode, payloadLength) - } else { - message = fmt.Sprintf("Lambda function %s invoked successfully (status: %d, payload: %d bytes)", - functionName, statusCode, payloadLength) - } - - resp.SendProgress(action.InvokeProgressEvent{ - Message: message, - }) + a.handleSyncInvocation(resp, functionName, output, logType) case awstypes.InvocationTypeEvent: // For asynchronous invocations, we only get confirmation that the request was accepted @@ -214,3 +199,31 @@ func (a *invokeAction) Invoke(ctx context.Context, req action.InvokeRequest, res "payload_length": len(output.Payload), }) } + +func (a *invokeAction) handleSyncInvocation(resp *action.InvokeResponse, functionName string, output *lambda.InvokeOutput, logType awstypes.LogType) { + statusCode := output.StatusCode + payloadLength := len(output.Payload) + + // Send success message + resp.SendProgress(action.InvokeProgressEvent{ + Message: fmt.Sprintf("Lambda function %s invoked successfully (status: %d, payload: %d bytes)", + functionName, statusCode, payloadLength), + }) + + // Output logs if available + if logType != awstypes.LogTypeTail || output.LogResult == nil { + return + } + + logData, err := base64.StdEncoding.DecodeString(aws.ToString(output.LogResult)) + if err != nil { + resp.SendProgress(action.InvokeProgressEvent{ + Message: fmt.Sprintf("Failed to decode Lambda logs: %s", err), + }) + return + } + + resp.SendProgress(action.InvokeProgressEvent{ + Message: fmt.Sprintf("Lambda function logs:\n%s", string(logData)), + }) +} diff --git a/website/docs/actions/lambda_invoke.html.markdown b/website/docs/actions/lambda_invoke.html.markdown index 4372d8008799..f78a2cd204a7 100644 --- a/website/docs/actions/lambda_invoke.html.markdown +++ b/website/docs/actions/lambda_invoke.html.markdown @@ -216,7 +216,7 @@ This action supports the following arguments: * `client_context` - (Optional) Up to 3,583 bytes of base64-encoded data about the invoking client to pass to the function in the context object. This is only used for mobile applications and should contain information about the client application and device. * `function_name` - (Required) Name, ARN, or partial ARN of the Lambda function to invoke. You can specify a function name (e.g., `my-function`), a qualified function name (e.g., `my-function:PROD`), or a partial ARN (e.g., `123456789012:function:my-function`). * `invocation_type` - (Optional) Invocation type. Valid values are `RequestResponse` (default) for synchronous invocation that waits for the function to complete and returns the response, `Event` for asynchronous invocation that returns immediately after the request is accepted, and `DryRun` to validate parameters and verify permissions without actually executing the function. -* `log_type` - (Optional) Set to `Tail` to include the execution log in the response. Only applies to synchronous invocations (`RequestResponse` invocation type). Defaults to `None`. When set to `Tail`, the last 4 KB of the execution log is included in the response. +* `log_type` - (Optional) Set to `Tail` to include the execution log in the response. Only applies to synchronous invocations (`RequestResponse` invocation type). Defaults to `None`. When set to `Tail`, the last 4 KB of the execution log is included in the response and output as part of the progress messages. * `payload` - (Required) JSON payload to send to the Lambda function. This should be a valid JSON string that represents the event data for your function. The payload size limit is 6 MB for synchronous invocations and 256 KB for asynchronous invocations. * `region` - (Optional) Region where this action should be [run](https://docs.aws.amazon.com/general/latest/gr/rande.html#regional-endpoints). Defaults to the Region set in the [provider configuration](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#aws-configuration-reference). * `qualifier` - (Optional) Version or alias of the Lambda function to invoke. If not specified, the `$LATEST` version will be invoked. Can be a version number (e.g., `1`) or an alias (e.g., `PROD`).