├── .gitignore ├── .travis.yml ├── README.md ├── deploy.sh ├── gulpfile.js ├── package.json ├── src ├── assets │ ├── images │ │ └── scape_long.png │ └── stylesheets │ │ ├── _base.scss │ │ ├── _mixins.scss │ │ ├── _variables.scss │ │ ├── application.scss │ │ └── components │ │ ├── _components.scss │ │ ├── _scape.scss │ │ └── _trolley.scss └── templates │ ├── layouts │ └── application.jade │ └── views │ ├── error.jade │ └── index.jade └── terraform ├── main.tf ├── terraform.tfstate └── terraform.tfstate.backup /.gitignore: -------------------------------------------------------------------------------- 1 | dist 2 | node_modules 3 | .sass-cache 4 | tmp 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | 3 | node_js: 4 | - 6.0.0 5 | 6 | # establish environment variables 7 | env: 8 | global: 9 | - GH_USER_NAME="TravisCI" 10 | - GH_USER_EMAIL="mikedball@gmail.com" 11 | - GH_REPO="mdb/terraform-example" 12 | # encrypted GH_TOKEN and AWS credentials 13 | - secure: "EBtNoRGUNjgom7lk6+O7Zh9A33X/251Cg7j5C+HqfkPMQcQwS6MEAXPT1vbnh1HoQixR1e6VTHdXxvWyE/14+98qtJiHnSbGiY67ZvQNDuFLb2+PKx7xhhl9heNj8Xk1K1SYJtfYQZIvZNl32V9db6eR3r7kKlWlpUVmnSnXrnm4ztI8se45OX/XPjAnARdBvkpbcTSprrAf7Qudc5R86ain18tJah6PICd12TIH4Cpdcr6CVL8kRK808VH+AS2oii7QcKXc084gBOJJLCiwa2DrcSEPOOk0AIn5ft+XVcaCsV6oOc6NliFKEPoJkaxbYWtunDlnqgB6epuaGrf99TfCg4E9R8sXBFqJwdMGDu3xM6Nddw87tMj/oCbUmjrNnl4qAxIMBD2TdjwFS1lNaXAML8W/jx3bNGSEg5MAYrqLL32eJta/vxRJwpCVnXUHxef9JcZMNZcvuKMdHC98JQIYbGRRFZ0cFtqMe63tgWafCi3WS+FIqSWnGdiKZ7dS110ANHaiQkDAZKTlh/9YJpzR9LyOoq7xXYtQIUovyDD2j498mAkcgByEbyZ39k6xMvLHHXsdUq25tdaMvqE3ZUASIDWqDk1QPfxkXX6n62Tj2X1HCA+3JI/DKyEfzt3QV4rntiP4Qv9jSuxNpd47rgsgVFg+HGJmko9QzAA/g+E=" 14 | 15 | # install ruby sass/compass for use by gulp-node-sass 16 | before_install: 17 | - gem install sass -v 3.4.22 18 | - gem install compass -v 1.0.3 19 | 20 | install: 21 | - npm install 22 | 23 | script: 24 | - npm run build 25 | 26 | # install terraform 27 | before_deploy: 28 | - curl -fSL "https://releases.hashicorp.com/terraform/0.6.15/terraform_0.6.15_linux_amd64.zip" -o terraform.zip 29 | - sudo unzip terraform.zip -d /opt/terraform 30 | - sudo ln -s /opt/terraform/terraform /usr/bin/terraform 31 | - rm -f terraform.zip 32 | 33 | # terraform apply 34 | deploy: 35 | - provider: script 36 | skip_cleanup: true 37 | script: "./deploy.sh" 38 | on: 39 | repo: mdb/terraform-example 40 | branch: master 41 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/mdb/terraform-example.svg?branch=master)](https://travis-ci.org/mdb/terraform-example) 2 | 3 | # terraform-example 4 | 5 | A reference repo demonstrating how to continuously deploy via [terraform](http://terraform.io) 6 | from [TravisCI](https://travis-ci.org/mdb/terraform-example). 7 | 8 | Via TravisCI (or locally), use Node.js to compile a static website; use Terraform to: 9 | 10 | * create `www.your-domain.com` and `your-domain.com` AWS S3 buckets configured for static website hosting 11 | * redirect requests to the `www.your-domain.com` bucket to `your-domain.com` 12 | * deploy the `index.html` and `error.html` objects to the `your-domain.com` bucket 13 | * establish an AWS Route53 `your-domain.com` DNS zone 14 | * establish a Route53 A record set pointing `your-domain.com` to the `your-domain.com` S3 bucket 15 | 16 | ## TravisCI build flow 17 | 18 | TravisCI: 19 | 20 | 1. Uses Node.js to compile `src` to a static website. 21 | 2. If the branch is `master`, installs `terraform` 22 | 3. If the branch is `master`, executes `deploy.sh` to deploy the static website to AWS S3 website fronted by `mikeball.me` via: 23 | 1. `terraform plan` 24 | 2. `terraform apply` 25 | 3. commit `terraform.tfstate` back to this repo with a `[ci skip]` commit message such that a TravisCI build is not triggered. 26 | 27 | ## Giving it a spin 28 | 29 | To deploy your own: 30 | 31 | 1. Fork this repo. 32 | 33 | 2. Visit [travis-ci.org](https://travis-ci.org/profile); activate CI for your fork of this repo. 34 | 35 | 3. Install the `travis` CLI if you don't already have it: 36 | 37 | ``` 38 | $ gem install travis 39 | ``` 40 | 41 | 3. Use the `travis` CLI to encrypt your AWS credentials and your [Github access token]() in environment variables: 42 | 43 | ``` 44 | $ travis encrypt AWS_ACCESS_KEY_ID=123 AWS_SECRET_ACCESS_KEY=456 GH_TOKEN=123 45 | ``` 46 | 47 | 4. Add the encrypted credentials string to your `.travis.yml`, replacing the current `secure` value: 48 | 49 | ``` 50 | ... 51 | env: 52 | secure: "ENCRYPTED STRING HERE" 53 | ... 54 | ``` 55 | 56 | 5. Replace `GH_USER_NAME`, `GH_USER_EMAIL`, and `GH_REPO` in the `.travis.yml` with your details. 57 | 58 | 6. Replace the `domain` var in [`terraform/main.tf`](https://github.com/mdb/terraform-example/blob/master/terraform/main.tf#L6) with your domain name. 59 | 60 | 7. Remove my `tfstate` files to start fresh: 61 | 62 | ``` 63 | $ git rm terraform/terraform.tfstate* 64 | $ git commit -m 'removed mdb tfstate' 65 | ``` 66 | 67 | 8. Push & deploy: 68 | 69 | ``` 70 | $ git push origin master 71 | ``` 72 | 73 | 9. Note that you may need to point the DNS servers associated with `your-domain.com` to those dynamically assigned to 74 | your A record by AWS. For example, my A record [uses these DNS servers](https://github.com/mdb/terraform-example/blob/master/terraform/terraform.tfstate#L48). 75 | -------------------------------------------------------------------------------- /deploy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -o errexit -o nounset 4 | 5 | cd terraform 6 | 7 | terraform plan 8 | 9 | terraform apply 10 | 11 | echo "Setting git user name" 12 | git config user.name $GH_USER_NAME 13 | 14 | echo "Setting git user email" 15 | git config user.email $GH_USER_EMAIL 16 | 17 | echo "Adding git upstream remote" 18 | git remote add upstream "https://$GH_TOKEN@github.com/$GH_REPO.git" 19 | 20 | git checkout master 21 | 22 | git add . 23 | 24 | NOW=$(TZ=America/New_York date) 25 | 26 | git commit -m "tfstate: $NOW [ci skip]" 27 | 28 | echo "Pushing changes to upstream master" 29 | git push upstream master 30 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | gulp = require('gulp'), 3 | gutil = require('gulp-util'), 4 | del = require('del'), 5 | runSequence = require('run-sequence'), 6 | changed = require('gulp-changed'), 7 | 8 | // HTML and CSS preprocessors 9 | jade = require('gulp-jade'), 10 | sass = require('gulp-ruby-sass'), 11 | 12 | // Image optimization 13 | imagemin = require('gulp-imagemin'), 14 | 15 | // Development server 16 | connect = require('gulp-connect'), 17 | livereload = require('gulp-livereload'), 18 | watch = require('gulp-watch'); 19 | 20 | var src = './src', 21 | dest = './dist', 22 | tmp = './tmp'; 23 | 24 | var srcs = { 25 | pub: src + '/public/**/*', 26 | img: src + '/assets/images/**/*', 27 | jade: src + '/templates/**/*', 28 | jadeViews: src + '/templates/views/**/*.jade', 29 | sass: src + '/assets/stylesheets/**/*.scss' 30 | }; 31 | 32 | var dests = { 33 | pub: dest + '/', 34 | jade: dest + '/', 35 | assets: dest + '/assets/', 36 | img: dest + '/assets/images/', 37 | sass: dest + '/assets/stylesheets/' 38 | }; 39 | 40 | var env, 41 | developmentServerPort = 4000, 42 | jadeLocals = {}; 43 | 44 | // TASKS 45 | gulp.task('default', function(callback) { 46 | env = jadeLocals.environment = 'development'; 47 | 48 | runSequence( 49 | ['delete:tmp', 'dest:tmp'], 50 | ['public', 'images', 'templates', 'stylesheets'], 51 | ['httpd', 'watch'], 52 | callback 53 | ); 54 | }); 55 | 56 | gulp.task('build', function(callback) { 57 | env = jadeLocals.environment = 'production'; 58 | 59 | runSequence( 60 | ['delete:dist'], 61 | ['public', 'stylesheets'], 62 | ['images:optimized', 'templates'], 63 | callback 64 | ); 65 | }); 66 | 67 | // SUBTASKS 68 | gulp.task('templates', function() { 69 | return gulp.src(srcs.jadeViews) 70 | .pipe(jade({ 71 | basedir: srcs.jade.replace('**/*', ''), 72 | locals:jadeLocals 73 | })) 74 | .pipe(gulp.dest(dests.jade)); 75 | }); 76 | 77 | gulp.task('stylesheets', function() { 78 | return gulp.src(srcs.sass) 79 | .pipe(sass({ 80 | style: 'compressed', 81 | compass: true 82 | }).on('error', gutil.log)) 83 | .pipe(gulp.dest(dests.sass)); 84 | }); 85 | 86 | gulp.task('images', function() { 87 | return gulp.src(srcs.img) 88 | .pipe(changed(dests.img)) 89 | .pipe(gulp.dest(dests.img)); 90 | }); 91 | 92 | gulp.task('images:optimized', function() { 93 | return gulp.src(srcs.img) 94 | .pipe(imagemin()) 95 | .pipe(gulp.dest(dests.img)); 96 | }); 97 | 98 | gulp.task('public', function() { 99 | return gulp.src(srcs.pub) 100 | .pipe(gulp.dest(dests.pub)); 101 | }); 102 | 103 | // DEVELOPMENT SUBTASKS 104 | gulp.task('watch', function() { 105 | gulp.watch(srcs.pub, ['public']); 106 | gulp.watch(srcs.img, ['images']); 107 | gulp.watch(srcs.jade, ['templates']); 108 | gulp.watch(srcs.sass, ['stylesheets']); 109 | }); 110 | 111 | gulp.task('httpd', ['livereload'], function() { 112 | connect.server({ 113 | root: dest, 114 | port: developmentServerPort, 115 | livereload: true 116 | }); 117 | }); 118 | 119 | gulp.task('livereload', function() { 120 | var glob = dest + '/**/*'; 121 | 122 | watch(glob) 123 | .pipe(connect.reload()); 124 | }); 125 | 126 | // HELPER TASKS 127 | var originalDest; 128 | 129 | gulp.task('dest:tmp', function(callback) { 130 | var key; 131 | 132 | for (key in dests) { 133 | dests[key] = dests[key].replace(dest, tmp); 134 | } 135 | 136 | originalDest = dest; 137 | dest = tmp; 138 | 139 | callback(); 140 | }); 141 | 142 | gulp.task('delete:dist', function(callback) { 143 | del([ dest +'*' ], callback); 144 | }); 145 | 146 | gulp.task('delete:tmp', function(callback) { 147 | del([ tmp +'*' ], callback); 148 | }); 149 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "terraform-example", 3 | "version": "0.0.1", 4 | "author": "Mike Ball ", 5 | "scripts": { 6 | "start": "gulp", 7 | "build": "gulp build", 8 | "test": "gulp build" 9 | }, 10 | "devDependencies": { 11 | "del": "^0.1.3", 12 | "express": "^4.9.7", 13 | "gulp": "^3.8.8", 14 | "gulp-changed": "^1.0.0", 15 | "gulp-concat": "^2.4.1", 16 | "gulp-connect": "^2.0.6", 17 | "gulp-imagemin": "^1.1.0", 18 | "gulp-jade": "^0.9.0", 19 | "gulp-livereload": "^2.1.1", 20 | "gulp-rename": "^1.2.0", 21 | "gulp-ruby-sass": "^0.7.1", 22 | "gulp-strip-debug": "^1.0.1", 23 | "gulp-util": "^3.0.1", 24 | "gulp-watch": "^1.1.0", 25 | "run-sequence": "^1.0.1" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/assets/images/scape_long.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mdb/terraform-example/6ff1f47fe9ee480a84a3d97fb2ed6e210eaf83f1/src/assets/images/scape_long.png -------------------------------------------------------------------------------- /src/assets/stylesheets/_base.scss: -------------------------------------------------------------------------------- 1 | @import 'compass'; 2 | @import 'variables'; 3 | @import 'mixins'; 4 | -------------------------------------------------------------------------------- /src/assets/stylesheets/_mixins.scss: -------------------------------------------------------------------------------- 1 | @mixin breakpoint($point: desktop) { 2 | @if $point == desktop { 3 | @media (max-width: $desktop_break_point) { @content; } 4 | } 5 | 6 | @else if $point == tablet { 7 | @media (max-width: $tablet_break_point) { @content; } 8 | } 9 | 10 | @else if $point == mobile { 11 | @media (max-width: $mobile_break_point) { @content; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/assets/stylesheets/_variables.scss: -------------------------------------------------------------------------------- 1 | $desktop_break_point: 959px; 2 | $tablet_break_point: 768px; 3 | $mobile_break_point: 480px; 4 | -------------------------------------------------------------------------------- /src/assets/stylesheets/application.scss: -------------------------------------------------------------------------------- 1 | @import 'base'; 2 | @import 'components/components'; 3 | 4 | * { 5 | margin: 0; 6 | padding: 0; 7 | } 8 | 9 | header, small { 10 | display: none; 11 | } 12 | -------------------------------------------------------------------------------- /src/assets/stylesheets/components/_components.scss: -------------------------------------------------------------------------------- 1 | @import 'trolley'; 2 | @import 'scape'; 3 | -------------------------------------------------------------------------------- /src/assets/stylesheets/components/_scape.scss: -------------------------------------------------------------------------------- 1 | @include keyframes(passing-country) { 2 | 0% { background-position: 500px 0; } 3 | 100% { background-position: -3500px 0; } 4 | } 5 | 6 | @mixin animate-landscape { 7 | @include animation(passing-country 35s linear 1 0s forwards); 8 | } 9 | 10 | body { 11 | width: 100%; 12 | height: 100%; 13 | display: block; 14 | background: #f9f6ef image-url('/assets/images/scape_long.png'); 15 | background-size: 2300px 736px; 16 | noise: 626262; 17 | density: 15; 18 | opacity: 15; 19 | @include animate-landscape; 20 | 21 | &:after { 22 | content: '.'; 23 | text-indent: -5000px; 24 | width: 100%; 25 | height: 100%; 26 | position: absolute; 27 | background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAMAAAAp4XiDAAAAUVBMVEWFhYWDg4N3d3dtbW17e3t1dXWBgYGHh4d5eXlzc3OLi4ubm5uVlZWPj4+NjY19fX2JiYl/f39ra2uRkZGZmZlpaWmXl5dvb29xcXGTk5NnZ2c8TV1mAAAAG3RSTlNAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEAvEOwtAAAFVklEQVR4XpWWB67c2BUFb3g557T/hRo9/WUMZHlgr4Bg8Z4qQgQJlHI4A8SzFVrapvmTF9O7dmYRFZ60YiBhJRCgh1FYhiLAmdvX0CzTOpNE77ME0Zty/nWWzchDtiqrmQDeuv3powQ5ta2eN0FY0InkqDD73lT9c9lEzwUNqgFHs9VQce3TVClFCQr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+tktDYYSNflcxI0iyO4TPBdlRcpeqjK/piF5bklq77VSEaA+z8qmJTFzIWiitbnzR794USKBUaT0NTEsVjZqLaFVqJoPN9ODG70IPbfBHKK+/q/AWR0tJzYHRULOa4MP+W/HfGadZUbfw177G7j/OGbIs8TahLyynl4X4RinF793Oz+BU0saXtUHrVBFT/DnA3ctNPoGbs4hRIjTok8i+algT1lTHi4SxFvONKNrgQFAq2/gFnWMXgwffgYMJpiKYkmW3tTg3ZQ9Jq+f8XN+A5eeUKHWvJWJ2sgJ1Sop+wwhqFVijqWaJhwtD8MNlSBeWNNWTa5Z5kPZw5+LbVT99wqTdx29lMUH4OIG/D86ruKEauBjvH5xy6um/Sfj7ei6UUVk4AIl3MyD4MSSTOFgSwsH/QJWaQ5as7ZcmgBZkzjjU1UrQ74ci1gWBCSGHtuV1H2mhSnO3Wp/3fEV5a+4wz//6qy8JxjZsmxxy5+4w9CDNJY09T072iKG0EnOS0arEYgXqYnXcYHwjTtUNAcMelOd4xpkoqiTYICWFq0JSiPfPDQdnt+4/wuqcXY47QILbgAAAABJRU5ErkJggg==); 28 | opacity: .8; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/assets/stylesheets/components/_trolley.scss: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /src/templates/layouts/application.jade: -------------------------------------------------------------------------------- 1 | doctype html 2 | html( lang="en" ) 3 | head 4 | meta( charset="utf-8" ) 5 | meta( http-equiv="X-UA-Compatible" content="IE=edge" ) 6 | link( rel="stylesheet" href='/assets/stylesheets/application.css' ) 7 | title Route 34 8 | body 9 | block content 10 | -------------------------------------------------------------------------------- /src/templates/views/error.jade: -------------------------------------------------------------------------------- 1 | extends /layouts/application 2 | block content 3 | header 4 | h1='Malfunction' 5 | -------------------------------------------------------------------------------- /src/templates/views/index.jade: -------------------------------------------------------------------------------- 1 | extends /layouts/application 2 | block content 3 | header 4 | h1='Hello' 5 | -------------------------------------------------------------------------------- /terraform/main.tf: -------------------------------------------------------------------------------- 1 | variable "region" { 2 | default = "us-west-2" 3 | } 4 | 5 | variable "domain_name" { 6 | default = "mikeball.me" 7 | } 8 | 9 | provider "aws" { 10 | region = "${var.region}" 11 | } 12 | 13 | resource "aws_s3_bucket" "site" { 14 | bucket = "${var.domain_name}" 15 | region = "${var.region}" 16 | acl = "public-read" 17 | website { 18 | index_document = "index.html" 19 | error_document = "error.html" 20 | } 21 | policy = <