Intermediate 25 min

Test, Debug, and Observe

Now let’s test the complete workflow. Upload a file, watch the function run, and verify the email is sent.

Testing Workflow

Here’s how to test everything:

Step 1: Upload a test file

# Create a test file
echo "This is a test file" > test-document.txt

# Upload to S3
aws s3 cp test-document.txt s3://my-notifications-bucket-2025/

# Verify upload
aws s3 ls s3://my-notifications-bucket-2025/

Step 2: Check function logs

The function should run automatically. Check the logs:

# View recent logs
aws logs tail /aws/lambda/notify-on-upload --follow

# Or in CloudWatch Console:
# 1. Go to CloudWatch → Log groups
# 2. Find /aws/lambda/notify-on-upload
# 3. Click latest log stream
# 4. View log entries

Step 3: Verify email received

  • Check your recipient email inbox
  • Look for email with subject “New file uploaded: test-document.txt”
  • Verify email content includes file details

What to Look For in Logs

Successful execution:

Event received: {...}
File uploaded: test-document.txt in bucket my-notifications-bucket-2025
Message prepared: New file uploaded: test-document.txt
Email sent successfully
Notification sent for: test-document.txt

Error indicators:

ERROR: Email error: ...
ERROR: Missing environment variable: SENDGRID_API_KEY
ERROR: Permission denied

Common Errors and Solutions

Error: Function didn’t run

Symptoms:

  • No logs appear
  • File uploaded but nothing happens

Possible causes:

  • Trigger not configured
  • Function in wrong region
  • Missing permissions

Solutions:

  1. Verify trigger is enabled in console
  2. Check function and bucket are in same region
  3. Review IAM permissions
  4. Check trigger configuration

Error: Permission denied

Symptoms:

  • Function runs but fails with permission error
  • “Access denied” in logs

Possible causes:

  • Function lacks permission to access storage
  • Email service API key invalid
  • Missing IAM role permissions

Solutions:

  1. Check function execution role has storage read permission
  2. Verify email service API key is correct
  3. Review IAM policies

Error: Email sending failed

Symptoms:

  • Function runs but email not received
  • “Email error” in logs

Possible causes:

  • Invalid API key
  • Sender email not verified
  • Rate limit exceeded
  • Invalid recipient email

Solutions:

  1. Verify API key in environment variables
  2. Verify sender email in email service dashboard
  3. Check rate limits
  4. Verify recipient email address

Error: Missing environment variable

Symptoms:

  • “undefined” or “null” errors
  • “Missing environment variable” in logs

Possible causes:

  • Environment variable not set
  • Wrong variable name
  • Variable not saved

Solutions:

  1. Check environment variables in function configuration
  2. Verify variable names match code
  3. Redeploy function after setting variables

Debugging Decision Tree

Use this flow to debug issues:

Did the function run?
├─ No → Check trigger configuration
│        ├─ Is trigger enabled?
│        ├─ Are function and bucket in same region?
│        └─ Are permissions correct?

└─ Yes → Are there errors in logs?
         ├─ Yes → Check error type
         │        ├─ Permission error → Check IAM roles
         │        ├─ Email error → Check API key and email service
         │        └─ Missing variable → Check environment variables

         └─ No → Did email arrive?
                  ├─ No → Check email service logs
                  └─ Yes → Success! ✅

Common Error Messages

Error MessageUsually MeansSolution
AccessDeniedPermission issueCheck IAM roles and policies
InvalidApiKeyWrong API keyVerify environment variable
Email not verifiedSender email not verifiedVerify email in service dashboard
Function timeoutFunction took too longIncrease timeout or optimize code
ResourceNotFoundExceptionFunction or bucket not foundVerify names and regions

Monitoring Best Practices

Set up alerts:

  • Function errors
  • Email delivery failures
  • High error rates

Track metrics:

  • Function invocations
  • Execution duration
  • Error rates
  • Email delivery rates

Regular checks:

  • Review logs weekly
  • Test workflow monthly
  • Monitor costs

Testing Checklist

Before considering the system complete:

  • File upload works - Can upload files to bucket
  • Function triggers - Function runs when file uploaded
  • Logs show execution - Can see function logs
  • Email sent - Email arrives in inbox
  • Email content correct - File details are accurate
  • Error handling works - Errors are logged properly
  • Multiple files work - System handles multiple uploads

What’s Next?

In the final page, we’ll cover security best practices, understand costs, and learn how to clean up resources when you’re done.