MCPcopy Create free account
hub / github.com/awsdocs/aws-lambda-developer-guide / handleRequest

Function handleRequest

sample-apps/example-go/function/main.go:49–76  ·  view source on GitHub ↗
(ctx context.Context, event json.RawMessage)

Source from the content-addressed store, hash-verified

47}
48
49func handleRequest(ctx context.Context, event json.RawMessage) error {
50 // Parse the input event
51 var order Order
52 if err := json.Unmarshal(event, &order); err != nil {
53 log.Printf("Failed to unmarshal event: %v", err)
54 return err
55 }
56
57 // Access environment variables
58 bucketName := os.Getenv("RECEIPT_BUCKET")
59 if bucketName == "" {
60 log.Printf("RECEIPT_BUCKET environment variable is not set")
61 return fmt.Errorf("missing required environment variable RECEIPT_BUCKET")
62 }
63
64 // Create the receipt content and key destination
65 receiptContent := fmt.Sprintf("OrderID: %s\nAmount: $%.2f\nItem: %s",
66 order.OrderID, order.Amount, order.Item)
67 key := "receipts/" + order.OrderID + ".txt"
68
69 // Upload the receipt to S3 using the helper method
70 if err := uploadReceiptToS3(ctx, bucketName, key, receiptContent); err != nil {
71 return err
72 }
73
74 log.Printf("Successfully processed order %s and stored receipt in S3 bucket %s", order.OrderID, bucketName)
75 return nil
76}
77
78func main() {
79 lambda.Start(handleRequest)

Callers

nothing calls this directly

Calls 1

uploadReceiptToS3Function · 0.85

Tested by

no test coverage detected