Optimizing Your Shopify Integration: AWS Lambda, Ruby, and the Shopify API Gem
Tags: aws, ruby, tutorial
Published: 2023-10-24 11:11:14

## **Introduction**
[Shopify](https://www.shopify.com/) is a popular e-commerce platform that empowers businesses to create and manage online stores. To enhance the functionality of your Shopify store, it is often necessary to interact with the [Shopify API](https://shopify.dev/docs/api). [AWS Lambda](https://aws.amazon.com/pm/lambda/), a serverless computing service, offers an ideal environment for building applications integrating with the Shopify API with the Ruby 3.2 runtime. In this article, we’ll guide you through accessing the Shopify API through AWS Lambda using the Ruby programming language and the [Shopify API Gem](https://github.com/Shopify/shopify-api-ruby).

## Prerequisites
Before we get started, ensure you have the following prerequisites in place:

1. An AWS account: You will need an AWS account to create and manage AWS Lambda functions.
2. A Shopify development store: If you don’t already have a Shopify store, you can create a development store for free on the [Shopify platform](https://www.shopify.com/partners). This store will allow you to test your API interactions.
3. Ruby knowledge: You should be familiar with the Ruby programming language to implement the AWS Lambda function and utilize the Shopify API Gem.

## Setting up Your Environment

1. Install and Configure AWS CLI: If you haven’t already, install the AWS Command Line Interface (CLI) and configure it with your AWS credentials.
2. Set up the Shopify API: Create a [custom app](https://help.shopify.com/current/manual/apps/app-types/custom-apps) in your Shopify store to obtain API credentials, including the API key, API secret key, and access scopes.

## Creating a Ruby-based AWS Lambda Function
Now, let’s create an AWS Lambda function in Ruby that interacts with the Shopify API using the Shopify API Gem:

Open a terminal and create a new Ruby function directory:

```
mkdir shopify_lambda_function
```

```
cd shopify_lambda_function
```
2. Create a `Gemfile` for managing Ruby gems:

```
source 'https://rubygems.org'

gem 'aws-sdk-lambda'
gem 'shopify_api'
```
3. Here comes the tricky part `shopify_api` gem has two dependency gems `oj`,`openssl` which are native gems. These gems with Native extensions are C code modules that are compiled and linked to Ruby gems. Thus,

**Native extension gems need to be built in the same environment as they will be run.**

If your Ruby development environment (e.g. a Mac) is different from your target Lambda in terms of operating system or architecture, there may be problems creating your Lambda image through - bundle install locally.

To overcome this challenge I have written an article Building [AWS Lambda Functions with Ruby 3.2 and Native Gem Libraries](https://medium.com/p/4c2f77b9f99b) it includes the Steps to Build AWS Ruby Lambdas with Native Extension Gems.

4. After installing the gems Let's Create a Ruby script to interact with the Shopify API using the Shopify API Gem. For example, in the file named `lambda_function.rb`:

```
require 'json'
require 'shopify_api'

def lambda_handler(event:, context:)
  # Start by initializing the ShopifyAPI::Context with the parameters 
  ShopifyAPI::Context.setup(
  api_key: "<api-key>",
  api_secret_key: "<api-secret-key>",
  host: "<https://application-host-name.com>",
  scope: "read_orders,read_products,etc",
  is_embedded: true, # Set to true if you are building an embedded app
  api_version: "2022-01", # The version of the API you would like to use
  is_private: false, # Set to true if you have an existing private app
)

# Initialising shopify API session
shopify_session = ShopifyAPI::Auth::Session.new(
  shop: "#{your_shop_name}.myshopify.com"
  access_token: "the_token_for_your_custom_app_found_in_admin"
)

  # Activate session to be used in all API calls
  # session must be type `ShopifyAPI::Auth::Session`
  ShopifyAPI::Context.activate_session(shopify_session)

  # Make API requests
  orders = ShopifyAPI::Order.all(session: shopify_session, status: "any")

  {
    statusCode: 200,
    body: JSON.generate(orders)
  }
end
```
Please refer to [Shopify's official documentation](https://shopify.dev/docs/api) for further API methods

5. Now, you can create the `.zip `deployment package that includes your function code and its dependencies:

please note if you have already created a `function.zip` folder in the previous step delete it and recreate it with the updated `lambda_function.rb`

```
zip -r function.zip lambda_function.rb vendor
```

## Creating and Deploying the AWS Lambda Function

1. Create an AWS Lambda function using the AWS Management Console or AWS CLI. Specify the runtime as Ruby 3.2 and upload the function.zip file.
2. Set up an [API Gateway to trigger the Lambda function](https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html), enabling you to access it via an HTTP endpoint.

## Testing the Lambda Function
You can now test the Lambda function by triggering it through the API Gateway endpoint. Ensure that the API Gateway and Lambda functions are properly configured, and use an HTTP client like cURL or [Postman to make requests to your Lambda function’s endpoint](https://learning.postman.com/docs/integrations/available-integrations/aws-api-gateway/).

## Conclusion
Integrating your Shopify store with AWS Lambda and Ruby, along with the Shopify API Gem, streamlines the process of building custom solutions and automating tasks. This setup can be used to retrieve store data, create custom applications, and perform various other operations via the Shopify API while benefiting from the ease of use and functionality provided by the Shopify API Gem. Ensure the security of your API credentials and handle errors effectively to establish a robust and reliable integration between Shopify and AWS Lambda.