Skip to content

Running the Foxy Custom Shipping Endpoint on AWS Lambda

Adam Judd edited this page Mar 1, 2018 · 3 revisions

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.

1. Create files for upload

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.

2. Create Lambda function

  1. Go to https://aws.amazon.com/, and either create a new account or login to your existing account to access the console.
  2. 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.
  3. 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:
  4. Give your function a name (for example "foxyShippingEndpoint"), and if not already selected, chose the "Node.js 4.3" runtime.
  5. 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".
  6. Click "Create Function" to proceed.
  7. On the following screen, you'll see your created function. Under "Function Code", select "Upload a .ZIP file" from the "Code entry type" dropdown.
  8. Use the file picker to select the zip archive you created earlier
  9. After selecting your file, click "Save" in the top right hand corner.

3. Create an API Endpoint

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:

  1. From your Lambda function, within the "Designer" section, select "API Gateway" under "Add Triggers".
  2. In the configuration options that appear below, select "Create a new API" from the "API" dropdown, then in the fields that appear:
    1. Enter an "API name" and "Deployment stage" (something like "production")
    2. Set "Security" to "Open"
    3. Press "Add"
  3. Press "Save" in the top right hand corner.

At this point the page will update to add in your newly created endpoint.

  1. 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.
  2. Under the "Resources" column in the middle of the page, click "ANY".
  3. Back in the "Resources" column, select "Actions" and click "Delete Method".
  4. Under "Actions" again, select "Create Method".
  5. A select box will appear below your function name, select "POST" and click the tick icon.
  6. In the right hand panel that appears:
  7. If not already, set "Integration type" to "Lambda Function".
  8. Select the AWS region you created your Lambda function in under "Lambda Region".
  9. 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.
  10. Click "Save" and "OK" in the permission prompt that appears.
  11. The right hand panel will be replaced with a map of your API endpoint, click the "Integration Request" option.
  12. In the new options it displays, at the bottom, expand "Body Mapping Templates"
  13. Set "Request body passthrough" to "When there are no templates defined"
  14. Click "Add mapping template"
  15. Enter application/json in the text box that appears and press the tick icon to the right
  16. In the code text area that appears below, enter { "body": $input.json('$') }
  17. Click "Save" at the bottom
  18. 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.
  19. 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.

4. Connect your new endpoint into Foxy

Now you've got a public API endpoint you can provide to Foxy!

  1. Switch to your store's Foxy administration and go to the "Shipping" section.
  2. 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.

5. Test!

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.