Serverless commercetools Integration

One command deployments for your e-commerce Serverless code. Serverless Commercetools Plugin commercetools API Extensions and Subscriptions allow you to customize and extend e-commerce functionality based on your company’s unique business needs. The Serverless Framework allows for fast and painless development of serverless cloud applications by introducing easy to use testing, CI/CD, scaling, and monitoring. Serverless […]

One command deployments for your e-commerce Serverless code.

Serverless Commercetools Plugin

commercetools API Extensions and Subscriptions allow you to customize and extend e-commerce functionality based on your company’s unique business needs. The Serverless Framework allows for fast and painless development of serverless cloud applications by introducing easy to use testing, CI/CD, scaling, and monitoring. Serverless cloud functions are often used to add additional behavior through commercetools API extensions. A common use case is validating shopping cart contents. Serverless functions can also be used as asynchronous background processes in response to events on the commercetools platform through commercetools Subscriptions. One example would be to send a confirmation email once an order has been completed. Checkout the link above to a new Serverless Framework plugin that deploys and registers a function as a commercetools API Extension. This plugin can also be used to attach your Serverless code to a commercetools Subscription. The deployments work for both AWS and Google Cloud Platform.

Deploy Serverless Function and commercetools Extension

Step 1.

The Serverless Platform allows you to easily build cloud applications. It is extensible with plugins. Install the Serverless Framework, create a lambda with the simple ‘serverless’ command, and then add the commercetools plugin :

$ npm install -g serverless
$ serverless
$ cd 'your new serverless project folder'
$ serverless plugin install --name serverless-commercetools-plugin

Step 2.

Environment variables to configure the AWS CLI

Set your cloud credentials via command line.

export AWS_ACCESS_KEY_ID=“your_id”
export AWS_SECRET_ACCESS_KEY=”your_key”

Step 3.

Setting up commercetools extensions/subscriptions is as easy as adding your client variables and configuration to the serverless.yaml and then running deploy. Note that the Lambda’s ARN is smartly assembled by the plugin itself so you do not need to add it to the post body. See the example serverless.yaml.

commercetools/serverless-commercetools-plugin

Example 1: commercetools Extension configuration with Deploy of Lambda. 
//serverless.yml env vars
CTP_DEPLOY_TYPE: "extension"
CTP_POST_BODY: '{
"destination": {
"type": "AWSLambda",
"accessKey": "your_key",
"accessSecret": "your_secret"
},
"triggers": [{
"resourceTypeId": "cart",
"actions": ["Create", "Update"]
}]
}'
command-line$ serverless deploy

You now have a commercetools Extension and lambda function attached. With Extensions one can extend the functionality of the commercetools APIs. One could now create some carts for the case above using Postman to run your Serverless extension code. Please note that you can set a key on extensions and subscriptions. The key prevents the same extension or subscription from being deployed twice from CI/CD and manually deploys.

API Extensions

For Google Cloud Platform function deploys simple set the CTP_POST_BODY to use the HTTP destination.

HTTP Destination 
type - String - "HTTP"
url - String
authentication - HttpDestinationAuthentication - Optional

Deploy Serverless Function and commercetools Subscription

Add the deploy type and body to your client variables to the serverless.yaml and then run deploy.

Example 2: commercetools Subscription configuration with Serverless Deploy of a Lambda.
CTP_DEPLOY_TYPE: "subscription"
CTP_POST_BODY: '{
"destination": {
"type": "SQS",
"queueUrl": "<url_to_your_queue>",
"accessKey": "<your_key>",
"accessSecret": "<your_secret>",
"region": "<your_region>"
},
"messages": [
{
"resourceTypeId": "order"
}
]
}'
command-line$ serverless deploy

Using Subscriptions

commercetools has patented the extensibility

Serverless Plugin Directory

The Serverless Plugin Directory features 1,000’s of plugins to extend your serverless development. Explore the directory below as you work with commercetools and the Serverless Framework. We are excited to see what e-commerce recipes you create.

Serverless Plugins Directory | Download Count + Filters


Serverless commercetools Integration was originally published in commercetools tech on Medium, where people are continuing the conversation by highlighting and responding to this story.

Source: Commercetools