This repository aims to demonstrate how to use Terraform to orchestrate Commercetools using it's powerful declarative schemas by utilising the labd/commercetools provider.
This project assumes accounts for the following services:
- Commercetools 60-day free trial of
commercetools
and generate an API client to access specific keys in order to drive this. - Sanity for the Studio, CMS and storefront UI.
- Commercelayer as the e-commerce layer and checkout functionality.
- Netlify for deployments.
Requires the terraform
CLI tool. See guide here on how to install.
Additionally, you are going to need to
Create an environment file .env
with the following credentials:
Variable | Description |
---|---|
BUILD_LANGUAGES |
The supported locales (the default is en-US, it-IT, fr-FR ). |
NEXT_PUBLIC_SITE_NAME |
Optional name for the <title> head tag (you can also edit this directly in the code). |
NEXT_PUBLIC_SITE_URL |
Optional URL of your deployed project for the og:url meta property (you can also edit this directly in the code). |
NEXT_PUBLIC_CL_CLIENT_ID |
Your Commerce Layer sales channels application client ID (you can create this automatically by following this onboarding guide or manually on the Commerce Layer dashboard. |
NEXT_PUBLIC_CL_ENDPOINT |
Your Commerce Layer organization's base endpoint (you can copy this on the Commerce Layer dashboard. |
NEXT_PUBLIC_SANITY_PROJECT_TITLE |
Optional name for the <title> head tag in Sanity studio (you can also edit this directly in the code). |
NEXT_PUBLIC_SANITY_PROJECT_ID |
Sanity project ID (you can get this from sanity.io/manage after creating a new project with the npm -y create sanity@latest CLI command or this quick start wizard. |
NEXT_PUBLIC_SANITY_DATASET |
Sanity dataset (you can get this from sanity.io/manage). |
NEXT_PUBLIC_SANITY_API_VERSION |
Sanity API version in the ISO date format, e.g 2022-02-15 (you can learn more about this in Sanity docs). |
NEXT_PUBLIC_SANITY_TOKEN |
Sanity API token (you can get this from sanity.io/manage). |
Firstly, you need to ensure the following .env
variables are present in your project:
ctp_project_key=
ctp_client_id=
ctp_client_secret=
ctp_scopes=
ctp_api_url=
ctp_auth_url=
ctp_debug=1
Secondly, initialise terraform
:
terraform init
Thirdly, stage and list changes that can be applied:
terraform plan
Finally, apply those changes and push to your commercetools
project:
terraform apply
- Run the command below to install the required dependencies:
pnpm preinstall
pnpm install
- Run the command below to start the development server:
pnpm run dev
Data is......
Log into your application via the CLI using the previously created CLI credentials like so:
pnpm run cl:login
Install the seeder plugin using the command below:
pnpm run cl plugins:install seeder
Install the checkout plugin using the command below:
pnpm run cl plugins:install checkout
Run the command below to import three demo markets (UK, USA, and Europe), a set of SKUs, related price lists, related prices, stock locations, and inventory into your organization using the multi_market
business model.
pnpm run cl seed -b multi_market
To see the commands for other seeder options, type the command below:
pnpm run cl:help
Kindly follow the steps below to add the dataset the template was developed with (structured text and image files for all Sanity schema documents). This data will match the commerce data in Commerce Layer (SKUs, prices, etc.).
- Extract the
production.tar.gz
file in the/data
directory using the command below:
tar -xcvf ./data/production.tar.gz
The extracted folder name should look like production-export-2021-02-26t14-15-56-557z
.
- Move this to the
.sanity-template/data
directory:
mkdir -p ./.sanity-template/data
mv production-export-2021-02-26t14-15-56-557z ./.sanity-template/data
Ensure you set this path as your $NEXT_PUBLIC_SANITY_DATASET_RELPATH
and the dataset name to $NEXT_PUBLIC_SANITY_DATASET
environment variables.
- Run the command below to import the
data.ndjson
file in the extracted folder.
pnpm run sanity:import
4 Check the Sanity studio now on localhost:3000/studio
to preview the imported content.
The labd/commercetools Terraform provider allows you extend commercetools in these ways. In this example we merely add a category but, there are so many other resources here to sink your teeth into.