AWS CDK Construct Library for sending Stripe events to SNS topics.
This library provides a CDK Construct for receiving Stripe webhook events through AWS EventBridge and sending notifications to SNS topics. It integrates with Stripe's partner event source to deliver real-time event notifications.
- EventBridge Integration: Receives events from Stripe's partner event source
- Event Filtering: Processes only specified event types
- SNS Notifications: Sends notifications to SNS with customizable message templates
- Automatic IAM Setup: Automatically configures required permissions
- TypeScript Support: Provides full type safety
npm install cdk-construct-stripe-events-to-snsBefore using this library, you need to set up Stripe Event Destinations and create a partner event bus in Amazon EventBridge. Follow these steps:
- Go to your Stripe Dashboard
- Navigate to Developer settings β Workbench
- Enable Workbench if not already enabled
- Open the Webhooks tab in Workbench
- Click Create new destination
- Select Account to listen to events from your own account
- Choose the event types you want to receive
- Select Amazon EventBridge as your destination type
- Enter your AWS account ID and region
- Click Create destination
- Go to AWS Management Console β EventBridge β Partner event sources
- Select the region you chose in Step 1.2
- Find the newly created partner event source (format:
aws.partner/stripe.com/{UNIQUE_ID}) - Click Associate with event bus
- Select permissions and click Associate
- Navigate to EventBridge β Rules
- Click Create Rule
- Select your event bus from the dropdown
- Under Event source, select AWS events or EventBridge partner events
- Under Event Pattern, select EventBridge partners β Stripe
- Choose event types or select All events
- Configure your target (SNS, Lambda, etc.)
- Click Create Rule
For detailed instructions, refer to the official Stripe documentation.
Now you can install and use the library in your CDK project:
npm install cdk-construct-stripe-events-to-snsimport { Construct } from 'constructs';
import * as cdk from 'aws-cdk-lib';
import * as events from 'aws-cdk-lib/aws-events';
import * as sns from 'aws-cdk-lib/aws-sns';
import { StripeEventsToSns } from 'cdk-construct-stripe-events-to-sns';
export class MyStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// Reference Stripe's partner event source
const eventBus = events.EventBus.fromEventBusName(
this,
'StripePartnerEventBus',
'aws.partner/stripe.com/ed_xxxxxx'
);
// Create SNS topic
const topic = new sns.Topic(this, 'StripeNotificationTopic', {
topicName: 'StripeEventNotifications',
});
// Create Stripe event notification system
new StripeEventsToSns(this, 'StripeEventNotifier', {
eventBus,
topic,
eventTypes: ['payment_intent.succeeded', 'customer.created'],
messageTemplate: EventField => ({
version: '1.0',
source: 'stripe',
content: {
textType: 'client-markdown',
title: ':bell: Stripe Event Notification',
description: [
`**Event Type:** ${EventField.fromPath('$.detail-type')}`,
`**Event ID:** ${EventField.fromPath('$.id')}`,
`**Occurred At:** ${EventField.fromPath('$.time')}`,
'',
'**Details:**',
`\`\`\`json\n${EventField.fromPath('$.detail')}\n\`\`\``,
].join('\n'),
},
metadata: {
eventId: EventField.fromPath('$.id'),
eventType: EventField.fromPath('$.detail-type'),
eventTime: EventField.fromPath('$.time'),
},
}),
});
}
}// SNS topic for #shifter-activities channel
const snsTopicForShifterActivitiesChannel = new sns.Topic(
this,
'SnsTopicForShifterActivitiesChannel',
{
topicName: 'NotifyToShifterActivitiesChannel',
}
);
// Stripe subscription creation event notification
new StripeEventsToSns(this, 'StripeSubscriptionCreatedToSNS', {
eventBus,
topic: snsTopicForShifterActivitiesChannel,
eventTypes: ['customer.subscription.created'],
messageTemplate: EventField => ({
version: '1.0',
source: 'custom',
content: {
textType: 'client-markdown',
title: ':warning: Stripe Subscription Created Notification',
description: [
'A new subscription has been created',
'',
'π *Details:*',
`β’ *Subscription ID:* ${EventField.fromPath('$.detail.data.object.id')}`,
`β’ *Customer ID:* ${EventField.fromPath('$.detail.data.object.customer')}`,
`β’ *Currency:* ${EventField.fromPath('$.detail.data.object.currency')}`,
`β’ *Plan Name:* ${EventField.fromPath('$.detail.data.object.items.data[0].plan.nickname')}`,
`β’ *Billing Interval:* ${EventField.fromPath('$.detail.data.object.items.data[0].plan.interval')}`,
'',
'π *Stripe Dashboard:*',
`β’ <https://dashboard.stripe.com/customers/${EventField.fromPath('$.detail.data.object.customer')}|π€ View Customer>`,
`β’ <https://dashboard.stripe.com/subscriptions/${EventField.fromPath('$.detail.data.object.id')}|π View Subscription>`,
].join('\n'),
nextSteps: [
'Please review the new subscription details in the Stripe Dashboard',
'Verify that customer billing information and plan are correctly configured',
],
},
metadata: {
customerId: EventField.fromPath('$.detail.data.object.customer'),
subscriptionId: EventField.fromPath('$.detail.data.object.id'),
eventTime: EventField.fromPath('$.time'),
eventSource: EventField.fromPath('$.source'),
},
}),
});| Property | Type | Required | Description |
|---|---|---|---|
eventBus |
events.IEventBus |
β | EventBridge event bus that receives Stripe events |
topic |
sns.ITopic |
β | SNS topic to send notifications to |
eventTypes |
string[] |
β | Array of Stripe event types to monitor |
messageTemplate |
(EventField) => any |
β | Template function to generate SNS message content |
EventField methods available within the messageTemplate function:
EventField.fromPath(path): Extract values from event data using JSONPathEventField.fromPath('$.id'): Get event IDEventField.fromPath('$.detail-type'): Get event typeEventField.fromPath('$.time'): Get event occurrence timeEventField.fromPath('$.detail.data.object.*'): Access Stripe event detail data
- Connect SNS topic with Slack channel
Supports all Stripe event types. Major event types include:
payment_intent.succeeded- Payment successfulcustomer.created- Customer createdcustomer.subscription.created- Subscription createdinvoice.payment_succeeded- Invoice payment successfulcharge.dispute.created- Chargeback occurred
For details, see Stripe Webhook Events.
npm installnpm run buildnpm run testnpm run watchThis library (cdk-construct-stripe-events-to-sns) and lambda-stripe-notifications are both designed to handle Stripe events, but they serve different use cases:
- Simple event forwarding: You need to forward Stripe events to SNS without additional processing
- No Lambda overhead: You want to avoid Lambda execution costs and cold starts
- Custom message formatting: You need full control over the SNS message format using EventBridge's message templating
- All event types: You need to handle any Stripe event type with flexible filtering
- Direct integration: You prefer EventBridge β SNS direct integration without intermediate processing
- Stripe API calls: You need to fetch additional details from Stripe API (e.g., retrieving full checkout session details)
- Slack notifications: You specifically need formatted Slack notifications via AWS Chatbot
- Complex processing: You need to perform custom business logic or data transformation
- Checkout events: You're primarily handling
checkout.session.completedandcheckout.session.async_payment_succeededevents - Multi-language support: You need built-in support for Japanese and English notification messages
| Feature | StripeEventsToSns |
lambda-stripe-notifications |
|---|---|---|
| Architecture | EventBridge β SNS | EventBridge β Lambda β SNS |
| Lambda Required | β No | β Yes |
| Stripe API Calls | β No | β Yes |
| Message Customization | β Full control via templates | |
| Event Types | β All Stripe events | |
| Cost | π° Lower (no Lambda) | π° Higher (Lambda execution) |
| Latency | β‘ Lower (direct) | β‘ Higher (Lambda processing) |
| Use Case | Generic event forwarding | Specialized Slack notifications |
MIT License
Pull requests and issue reports are welcome.
If you encounter any issues, please report them on the GitHub Issues page.