Skip to content

Commit dcacb22

Browse files
authored
Merge pull request #44843 from hashicorp/f-log-lambda-invoke-action
`aws_lambda_invoke` action: Output logs
2 parents aea2f7b + fbb5e3a commit dcacb22

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

.changelog/44843.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
action/aws_lambda_invoke: Output logs in a progress message when `log_type` is `Tail`
3+
```

internal/service/lambda/invoke_action.go

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -173,22 +173,7 @@ func (a *invokeAction) Invoke(ctx context.Context, req action.InvokeRequest, res
173173
// Handle different invocation types
174174
switch invocationType {
175175
case awstypes.InvocationTypeRequestResponse:
176-
// For synchronous invocations, we get an immediate response
177-
statusCode := output.StatusCode
178-
payloadLength := len(output.Payload)
179-
180-
var message string
181-
if logType == awstypes.LogTypeTail && output.LogResult != nil {
182-
message = fmt.Sprintf("Lambda function %s invoked successfully (status: %d, payload: %d bytes, logs included)",
183-
functionName, statusCode, payloadLength)
184-
} else {
185-
message = fmt.Sprintf("Lambda function %s invoked successfully (status: %d, payload: %d bytes)",
186-
functionName, statusCode, payloadLength)
187-
}
188-
189-
resp.SendProgress(action.InvokeProgressEvent{
190-
Message: message,
191-
})
176+
a.handleSyncInvocation(resp, functionName, output, logType)
192177

193178
case awstypes.InvocationTypeEvent:
194179
// 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
214199
"payload_length": len(output.Payload),
215200
})
216201
}
202+
203+
func (a *invokeAction) handleSyncInvocation(resp *action.InvokeResponse, functionName string, output *lambda.InvokeOutput, logType awstypes.LogType) {
204+
statusCode := output.StatusCode
205+
payloadLength := len(output.Payload)
206+
207+
// Send success message
208+
resp.SendProgress(action.InvokeProgressEvent{
209+
Message: fmt.Sprintf("Lambda function %s invoked successfully (status: %d, payload: %d bytes)",
210+
functionName, statusCode, payloadLength),
211+
})
212+
213+
// Output logs if available
214+
if logType != awstypes.LogTypeTail || output.LogResult == nil {
215+
return
216+
}
217+
218+
logData, err := base64.StdEncoding.DecodeString(aws.ToString(output.LogResult))
219+
if err != nil {
220+
resp.SendProgress(action.InvokeProgressEvent{
221+
Message: fmt.Sprintf("Failed to decode Lambda logs: %s", err),
222+
})
223+
return
224+
}
225+
226+
resp.SendProgress(action.InvokeProgressEvent{
227+
Message: fmt.Sprintf("Lambda function logs:\n%s", string(logData)),
228+
})
229+
}

website/docs/actions/lambda_invoke.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ This action supports the following arguments:
216216
* `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.
217217
* `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`).
218218
* `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.
219-
* `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.
219+
* `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.
220220
* `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.
221221
* `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).
222222
* `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`).

0 commit comments

Comments
 (0)