Intermittent Error: write EPIPE when running stripe client in AWS Lambda
Problem
We're using the stripe node client 8.71.0 on an AWS Lambda running node 12.x. A stripe customers.list call is called first thing when the lambda executes. 33% of the time - we get this error on that call. It consistently happens so does not seem to be transient. I did read https://github.com/stripe/stripe-node/issues/650, and setting maxNetworkRetries in stripe to 2 seems to resolve the issue. However it seems that just masks the issue. Is this a stripe issue or AWS Lambda issue? Probably lambda, I submitted a request with AWS. But putting this here in case others run into it. 2020-10-13T12:02:58.032Z c184006d-fe96-490a-9bfe-696b8271769a ERROR StripeConnectionError: An error occurred with our connection to Stripe. at /var/task/node_modules/stripe/lib/StripeResource.js:234:9 at ClientRequest.<anonymous> (/var/task/node_modules/stripe/lib/StripeResource.js:489:67) at ClientRequest.emit (events.js:315:20) at ClientRequest.EventEmitter.emit (domain.js:483:12) at TLSSocket.socketErrorListener (_http_client.js:426:9) at TLSSocket.emit (events.js:315:20) at TLSSocket.EventEmitter.emit (domain.js:483:12) at emitErrorNT (internal/streams/destroy.js:92:8) at emitErrorAndCloseNT (internal/streams/destroy.js:60:3) at processTicksAndRejections (internal/process/task_queues.js:84:21) { type: 'StripeConnectionError', raw: { message: 'An error occurred with our connection to Stripe.', detail: Error: write EPIPE at WriteWrap.o
Error Output
error on that call. It consistently happens so does not seem to be transient.
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Implement Retry Logic and Increase Timeout for Stripe API Calls
The intermittent EPIPE error occurs due to AWS Lambda's execution environment occasionally closing the connection before the Stripe API responds. This can happen due to network latency or Lambda's timeout settings being too low for the Stripe API response time. Increasing the timeout and implementing retry logic can help mitigate this issue.
Awaiting Verification
Be the first to verify this fix
- 1
Increase Lambda Timeout
Increase the timeout setting for your AWS Lambda function to ensure it has enough time to complete the Stripe API call. The default timeout is 3 seconds, which may not be sufficient for all network conditions.
bashaws lambda update-function-configuration --function-name yourFunctionName --timeout 10 - 2
Implement Retry Logic
Modify the Stripe client configuration to include retry logic. Set the maxNetworkRetries to 2 to allow for automatic retries on connection errors.
javascriptconst stripe = require('stripe')('your_stripe_secret_key', { maxNetworkRetries: 2 }); - 3
Handle Connection Errors Gracefully
Add error handling in your Lambda function to catch and log connection errors. This will help in identifying if the issue persists after implementing the fixes.
javascripttry { const customers = await stripe.customers.list(); } catch (error) { console.error('Error fetching customers:', error); } - 4
Monitor Lambda Execution Logs
Set up monitoring for your Lambda function using AWS CloudWatch to track the frequency of the EPIPE errors. This will help confirm if the changes have resolved the issue.
bashaws logs filter-log-events --log-group-name /aws/lambda/yourFunctionName --filter-pattern 'EPIPE'
Validation
Confirm that the EPIPE errors have significantly decreased or are no longer occurring in the CloudWatch logs after implementing the changes. Additionally, monitor the response times of the Stripe API calls to ensure they are within acceptable limits.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep