Recently, we migrated a client from a LAMP stack WordPress instance, which was load balanced across multiple EC2 VMs, to a single flattened Amazon S3 bucket [post coming soon]. At the 11th hour, a hidden requirement presented itself. Unknown to the client’s marketing team and us, IT had been adding hundreds of 301 redirects, all contained within a included .conf file. The challenge here was to architect an elegant solution where IT would manage the redirect, and Marketing would retain their push button deployment process. S3 has an interesting way of providing this feature – you set metadata to the object header that has a specific key of “Website Redirect Location”.

A short background… Our client is a tech company in the valley and this is their marketing website. Primarily a brochure site, this presented an opportunity to save our client a substantial amount of money as well as construct more secure site. S3 was the simple choice for hosting. In terms of infrastructure, the costs would roughly go from over $20k per year for multiple EC2 VMs to under $4k for some S3 buckets. Although we take every precaution to harden our WordPress instances, nothing beats a flattened site for security. Our client had 1 requirement… 1-button deployment. Well, we got pretty close out the gate, but we are still working on configuring everything that needs to happen down to 1-button. Currently its a 2-button process… Jenkins is indispensable.

Now on to why you are reading this… if you have worked with S3 at all, then you know there are practically an infinite number of ways to create an object programmatically (at least when googling it seems like there is a new utility or sdk out every week). After my own bit of research I decided that there was one elegant way we could add redirects to our deployment process.

Installing the Node.js flavor of the aws-sdk we got to work building a small app that would be called within our Jenkins build process to deploy the WordPress site as a flattened site to S3. To help streamline the code, we decided that a super simple json file would contain all the redirect source and target information. The IT department can add the redirects to this file as needed, and Marketing can go about their day.

redirect.json contains:


{
	"stella/index.html": "marvel.com",
	"zoey/index.html": "disney.com"
}

The following code does the magic:


var AWS = require('aws-sdk');
var config = {
	region: 'us-west-2'
};
AWS.config.update(config);

var s3 = new AWS.S3({
	params: {Bucket: 'YOUR-BUCKET'}
});

var jsonObj = require("./redirect.json");

for(var myRedirect in jsonObj) {
	s3.putObject ({
		Key: myRedirect,
		WebsiteRedirectLocation: 'http://'+jsonObj[myRedirect]
	}, handler);
}

And lastly, callback the response object. Print the error or success to the command line.


function handler(err,data) {
    if(err) {
        console.log(err)
    } else {
    	console.log(data)
    }
}

So, now IT can add whatever redirects they require to the json file, the Node.js app is called within Jenkins build process to deploy the site, and all is well.

UPDATE

We built a WordPress frontend for Source and Targets that is accessed by the node app via the wp-rest-api. Easy Peazy.