AWS Developer Tools Blog

Using the AWS SDK for JavaScript from Behind a Proxy

The AWS SDK for JavaScript can be configured to work from behind a network proxy. In browsers, proxy connections are transparently managed, and the SDK works out of the box without any additional configuration. This article focuses on using the SDK in Node.js from behind a proxy.

Node.js itself has no low-level support for proxies, so in order to configure the SDK to work with a proxy, you will need override the default http.Agent.

This article shows you how to override the default http.Agent with the proxy-agent npm module. Note that some http.Agent modules may not support all proxies. You can visit npmjs.com for a list of available http.Agent libraries that support proxies.

Installation

>npm install proxy-agent --save

In your code

The proxy-agent module automatically maps proxy protocols to agent instances. Currently, the supported protocols are HTTP(s), Secure Socket (SOCKS), and Proxy Auto-Config (PAC).

var AWS = require('aws-sdk');
var proxy = require('proxy-agent');

AWS.config.update({
  httpOptions: { 
    agent: proxy('http://user:password@internal.proxy.com') 
  }
});

var s3 = new AWS.S3({region: 'us-west-2'});
s3.getObject({Bucket: 'bucket', Key: 'key'}, function (err, data) {
  console.log(err, data);
});

Overriding the default http.Agent is simple, and allows you to configure proxy settings that SDK can use. We hope you find this information useful and are able to easily use the SDK in Node.js from behind a proxy!