HubSpot AWS Integration
HubSpot is an all-in-one CRM platform that helps businesses attract visitors, convert leads, and close customers through integrated marketing, sales, service, and content management tools. It empowers organizations to streamline operations, build meaningful customer relationships, and drive sustainable growth through data-driven insights and automation.
Table of Contents
- Overview
- Architecture
- Features
- Prerequisites
- Setup Instructions
- Customizing HubSpot Forms
- Testing
- Troubleshooting
- Contributing
- Resources
Overview
This integration enables real-time data synchronization between HubSpot and your application through AWS Lambda and API Gateway. When events occur in HubSpot (such as contact creation), webhooks trigger Lambda functions to process these events and update your application accordingly.
Architecture
βββββββββββββββββ
β β
β HubSpot β
β β
βββββββββ¬ββββββββ
β
β Webhook Event
βΌ
βββββββββββββββββ βββββββββββββββββ βββββββββββββββββ
β β β β β β
β API Gateway ββββ Lambda ββββ Your β
β + Security β β Function β β Application β
β β β β β β
βββββββββββββββββ βββββββββββββββββ βββββββββββββββββ
Features
- Real-time event processing from HubSpot
- Contact creation/update synchronization
- Secure webhook handling with signature validation
- Automatic retrieval of complete contact details
- Custom field mapping between HubSpot and your application
- Comprehensive security implementation
Prerequisites
- AWS Account with access to:
- Lambda
- API Gateway
- CloudWatch
- HubSpot account with super admin access
Setup Instructions
AWS Setup
1. Create Lambda Function
- Open the AWS Management Console and navigate to Lambda
- Click βCreate functionβ
- Select βAuthor from scratchβ
- Enter function name (e.g.,
hubspot-webhook-handler) - Select Node.js runtime (14.x or later)
- Create a new role with basic Lambda permissions
- Click βCreate functionβ
- Upload the Lambda code from this repository or copy the code below:
// See the lambda-function.js file in this repository
exports.handler = async (event) => {
try {
console.log('Received event:', JSON.stringify(event, null, 2))
// Parse the body if it's a string
const body = typeof event.body === 'string' ? JSON.parse(event.body) : event.body
// HubSpot sends an array of events
const events = Array.isArray(body) ? body : [body]
// Use Promise.all with array methods instead of for...of loop
await Promise.all(events.map(async (hubspotEvent) => {
console.log('Processing event:', JSON.stringify(hubspotEvent, null, 2))
// Check if this is a contact creation event and route the request based on event
if (hubspotEvent.subscriptionType === 'contact.creation') {
// Get the contact ID from the event
const contactId = hubspotEvent.objectId
// Fetch the complete contact details from HubSpot
const contactDetails = await getContactDetails(contactId)
console.log(contactDetails)
// Process the contact details (e.g., add to your application)
await processContactInApplication(contactDetails)
}
}))
// Return a successful response to HubSpot
return {
statusCode: 200,
body: JSON.stringify({ message: 'Event processed successfully' }),
}
} catch (error) {
console.error('Error processing webhook:', error)
return {
statusCode: 500,
body: JSON.stringify({ message: 'Error processing webhook', error: error.message }),
}
}
}
2. Set Up Environment Variables
In your Lambda function, configure these environment variables:
HUBSPOT_API_KEY: Your HubSpot API key/access tokenHUBSPOT_CLIENT_SECRET: For webhook signature validationWEBHOOK_SECRET_TOKEN: Custom secret token for additional security
3. Configure API Gateway
- Create a new REST API
- Create a resource (e.g.,
/hubspot-webhooks) - Add a POST method with Lambda integration
- Deploy the API to a stage (e.g., βprodβ)
- Note the invoke URL - youβll need this for HubSpot
handler: handlers/hubspot_webhook.handler
runtime: nodejs18.x
layers:
- ${ssm:NODE_LAYER}
events:
- http:
path: /webhook
method: post
cors: true
HubSpot Setup
1. Create Private App for API Access
- Go to HubSpot > Settings > Integrations > Private Apps
- Click βCreate private appβ
- Name your app (e.g., βContact Sync Integrationβ)
- Request scopes:
crm.objects.contacts.readcrm.objects.contacts.write(if needed)
- Create app and copy the access token
2. Configure Webhook Subscription
- Go to HubSpot > Settings > Integrations > Webhooks
- Click βCreate webhookβ
- Enter your API Gateway endpoint URL
- Select events to subscribe to (e.g., βcontact.creationβ)
- Set throttling rate if needed
- Save the webhook
3. Test the Integration
- Create a new contact in HubSpot
- Check CloudWatch logs to verify the webhook was received
- Confirm that the contact data was processed correctly
Security Implementation
- Add the security code from this repository to your Lambda function
- Configure shared secret in both HubSpot (if supported) and Lambda
- Update API Gateway resource policy for IP restrictions
Customizing HubSpot Forms
Adding Custom Fields
- Go to HubSpot > Settings > Properties under Data Management
- Click βCreate propertyβ
- Configure the property details:
- Group: Choose or create new group
- Label: What users will see
- Field type: Text, dropdown, date, etc.
Making Fields Required
- Edit your form in HubSpot
- Click on the field you want to make required
- Toggle the βRequiredβ switch to ON
- Save your form
Auto-filling User Emails
For forms that need to capture the current userβs email:
- Enable βPre-populate fields with known valuesβ in form settings
- Use HubSpotβs owner property for internal tracking
- For more advanced scenarios, implement custom JavaScript with HubSpotβs forms API
Troubleshooting
Common Issues
-
Webhook not triggering Lambda
- Check API Gateway configuration
- Verify HubSpot webhook setup
- Check CloudWatch logs for errors
-
Missing contact data
- Verify HubSpot API key permissions
- Check property names in the API request
- Look for API rate limiting issues