-
Notifications
You must be signed in to change notification settings - Fork 0
Running the Foxy Custom Shipping Endpoint on AWS Lambda
AWS Lambda is a low-cost compute service - that allows you to run code in response to events, but with the benefits of Amazon's scalability. For most stores, the free tier provided by Lambda would exceed your requirements, but even if not, it's incredibly cheap. As an example, your store would have to do over 1 million shipping requests in a given month before you broke through the free tier limits. On the other end of the scale, if your shipping endpoint was hit 10 million times in a month, your costs for Lambda would be around US$1.80.
This set up also makes use of Amazon's API Gateway service which is priced based on the number of requests it receives and the size of the response it sends. If your store had 1 million shipping requests in a month, it would cost you around USD$3.55 (but would also fall under the free tier for the first 12 months). To use the same 10 million request in a month as above, the API Gateway would cost you just over $35.
Create a folder and make a new file called index.js
and paste the following:
var ShippingResponse = require('foxy-shipping-endpoint');
exports.handler = function (event, context) {
var cart_details = event.body;
console.log("Starting request for " + cart_details['_embedded']['fx:shipment']['postal_code'] + " " + cart_details['_embedded']['fx:shipment']['country']);
var rates = new ShippingResponse(cart_details);
// Include custom logic here
console.log("Finishing");
context.succeed(rates.output(false));
};
As detailed in the readme - create your custom shipping rate logic and include it within the script where noted above.
When you have your logic ready, in your terminal, cd
to that folder and run npm install foxy-shipping-response
.
Make a zip archive of the index.js
file and the node_modules
folder that was just created.
- Go to https://aws.amazon.com/, and either create a new account or login to your existing account to access the console.
- From the console, under "Compute" select "Lambda". If you don't already have a Lambda function - you'll see a landing screen. Click the "Get Started Now" button. If you do already have functions, create a new function.
- You'll be presented with some options. Choose "Author from scratch" if not selected by default (it should be). Below the selections, you'll be able to configure your Lambda function:
- Give your function a name (for example "foxyShippingEndpoint"), and if not already selected, chose the "Node.js 4.3" runtime.
- Under "Role", if you have a role already you can select "Choose an existing role" and then select that role in the next dropdown labelled "Existing Role". If you don't have a role already, select "Create a new role from template(s)", give it a name and select "Basic Edge Lambda Permissions" under "Policy Templates".
- Click "Create Function" to proceed.
- On the following screen, you'll see your created function. Under "Function Code", select "Upload a .ZIP file" from the "Code entry type" dropdown.
- Use the file picker to select the zip archive you created earlier
- After selecting your file, click "Save" in the top right hand corner.
At this point you've got a lambda function, but no way to connect it to Foxy. This is where Amazon's API endpoints come in:
- From your Lambda function, within the "Designer" section, select "API Gateway" under "Add Triggers".
- In the configuration options that appear below, select "Create a new API" from the "API" dropdown, then in the fields that appear:
- Enter an "API name" and "Deployment stage" (something like "production")
- Set "Security" to "Open"
- Press "Add"
- Press "Save" in the top right hand corner.
At this point the page will update to add in your newly created endpoint.
- From the "Designer" section, if it's not already, click the the API endpoint you just created, and in the "API Gateway" section that appears below the designer, click the name of the API endpoint you created. This will open that endpoint in the API endpoint management console in a new tab.
- Under the "Resources" column in the middle of the page, click "ANY".
- Back in the "Resources" column, select "Actions" and click "Delete Method".
- Under "Actions" again, select "Create Method".
- A select box will appear below your function name, select "POST" and click the tick icon.
- In the right hand panel that appears:
- If not already, set "Integration type" to "Lambda Function".
- Select the AWS region you created your Lambda function in under "Lambda Region".
- In the text box that appears below, begin to type the Lambda function name you created earlier and select it when it offers an autocomplete option. If you don't see it autocomplete, ensure you've selected the right region.
- Click "Save" and "OK" in the permission prompt that appears.
- The right hand panel will be replaced with a map of your API endpoint, click the "Integration Request" option.
- In the new options it displays, at the bottom, expand "Body Mapping Templates"
- Set "Request body passthrough" to "When there are no templates defined"
- Click "Add mapping template"
- Enter
application/json
in the text box that appears and press the tick icon to the right - In the code text area that appears below, enter
{ "body": $input.json('$') }
- Click "Save" at the bottom
- At the top of the middle column, click the "Actions" dropdown and select "Deploy API". Select the stage you created earlier as the stage in the popup that appears.
- Once the page refreshes, within the middle column, expand the options there until you see the "POST" method option, and click that. Take note of the "Invoke URL" displayed on the right - copy this to your clipboard.
Note - if you need to reference the URL to invoke your function later on, you can also see it from your Lambda function page. Click the "API Gateway" panel within the "Designer", expand the "Details" option within the section below, and it will show your "Invoke URL" there too.
Now you've got a public API endpoint you can provide to Foxy!
- Switch to your store's Foxy administration and go to the "Shipping" section.
- At the bottom of the options, enable the "use custom endpoint" option and paste the URL into the text input that appears. Save the page.
At this point - everything should be connected together. If you hit your store checkout with a live shipping product, the custom rates you created in the Lambda function should be displayed as options you can select after entering a shipping address.s.