Automate your Shopify store with AWS
Some time ago, I was tasked to find a way to automate the writing of Shopify order information from the platform to an external source. The main goal of this was to eliminate the tedium involved with manually transferring information (along with human errors) into their external tracking sheet. This purpose of this article is to walk you through the main idea behind the solution I took in achieving this. It also hopes to serve as a kick-start guide for those seeking to integrate Shopify webhooks with the AWS lambda service. For this guide to be meaningful to the reader, you should already have both Shopify and AWS accounts created.
- Create Lambda function
- Create API gateway
- Set up Shopify Webhook
- Monitoring (Cloudwatch)
1. Creating your Lambda function
There isn’t much set up required for the lambda function.
- From the AWS console, head over to the Lambda service > ‘Create Function’.
- Give your lambda function a name, and the rest of the settings can be left to the default value. (We will need permissions for Cloudwatch to access our logs later on, but this should be enabled by default)
- Once created, add the following two lines of code. As you can see, we aren’t doing anything special here, we are simply going to print the information to the console (this will be viewed in cloudwatch to verify the set up). Once updated, don’t forget to hit ‘Deploy’
import jsondef lambda_handler(event, context):
data = json.loads(event['body']). # line 1
print(json.dumps(data)) # line 2
'body': json.dumps('Hello from Lambda!')
2. AWS API Gateway
The API gateway provides an endpoint which we will pass to Shopify to set up the webhook.
- Head over to the API gateway service. Hit ‘Create API’ > ‘Rest API’ > ‘Build’.
- Give a name to your API (everything else can be left as the default settings) before hitting the ‘Create API’ button.
If all went well, your gateway should be set up. But you’re not done just yet, we still haven’t created a way for your Shopify to communicate with your Lambda service.
- From your Gateway detail’s page, select ‘Actions’ > ‘Create Method’
2. Select ‘POST’ from the drop down box and confirm.
3. You should now see the following page, configure the POST method as follows and confirm. (remember to check ‘Use Lambda Proxy Integration — read more about it here)
Deploying your API
- Click on ‘Actions’ and select ‘Deploy API’.
- Create a new stage and fill in a name before hitting ‘Deploy’
- Once deployed, you should have been navigated to the deployed stage. A banner should appear containing the ‘Invoke URL’. Save this link somewhere! We will be using it to set up the webhook in Shopify next.
Great! We’re almost there, we’ve already done the required set up on the AWS end and all that’s left is to create the webhook.
- Head over to your Shopify admin page and navigate to ‘Settings’ ( found on the bottom left corner of your store’s page).
- Once there, you should see a list of different tabs. We’re interested in ‘Notification’.
- Next , scroll to the bottom of the page (you should see a ‘Webhooks’ section right at the bottom). Proceed to ‘Create webhook’.
- Remember that URL we copied previously? It’s time to make use of it. Simply paste it in the URL field. As for Event, we’re going to want to trigger this webhook whenever an order is created, so select ‘Order creation’ from the dropdown. Once done, hit ‘Save’.
Awesome, you should now see a a newly created item in the webhooks section. Now, whenever an order is created, Shopify is going to trigger an event to call the API that you’ve provided which in turn execute the lambda function. To test this out, hit ‘Send test notification’ (this should appear in the same row as your new webhook).
4. CloudWatch Logs
To verify that this notification has indeed been sent successfully, head back over to your AWS console and navigate to the CloudWatch service
- From CloudWatch → Logs → Log groups
- Select the log group belonging to the Lambda created
Here, you should see a new entry under Log stream created from the test notification previously sent. And Voilà, you’ve successfully set up a simple pipeline between Shopify and AWS lambda. You’re now free to automate away with your Shopify store 🌞
One interesting thing I’ve realised while putting this together was how different Shopify add-ons affected the way data was formatted in the response. But this shouldn’t be too big of an issue. For the most part, you should be able to work round it. Other than that, I hope you found this guide useful and easy to follow. I left a couple of references below on some things which I found useful while putting this together. If you found this helpful, I would greatly appreciate it if you gave it a tasty clap (you’re already at the end anyways, so why not?!)