Use Grunt to trigger OpsWorks deployment

Back to OpsWorks flow. On the previous post I showed how to use Dokku with OpsWorks to achieve zero downtime deployments and if you don't need it for what I will talk about in the post, you should still have a look at it.

Now! The goal here is to have a grunt task to trigger that deployment preventing ud from having to login into OpsWorks website to trigger each deployment. (keep in mind that each deployment can take several minutes and it is not advised to have more then one deployment running at any given time)


So, start in you project folder and install the necessary dependencies with:

npm install grunt-aws-sdk --save-dev

After that you just need to add a section named aws in your project's Gruntfile as follows:

grunt.initConfig({
  aws: {
    options: {
      credentials: {
        accessKeyId: 'YOUR_ACCESS_KEY_ID',
        secretAccessKey: 'YOUR_SECRET_ACCESS_KEY',
        region: 'us-east-1'
      }
    },
    'deploy': {
      service: 'opsworks',
      method: 'createDeployment',
      params: {
        StackId: 'YOUR_STACK_ID',
        AppId: 'YOUR_APP_ID',
        Command: {
          Name: "deploy"
        }
      }
    }
  },
});

grunt.registerTask('deploy', [''aws:deploy'']);

(Do not change the region as OpsWorks only have one endpoint)

To find the Stack ID go to your Stack and click on the Stack Settings:

Stack Settings

On the next screen, the Stack ID will be shown under the label OpsWorks ID:

Stack ID

After that you need the Application ID that can be found on the OpsWorks Application page:

Application ID


Now you can use grunt deploy on you project folder to trigger your deloyments.

As a bonus and if you use Dokku, this is my command flow:

grunt build
git commit -am "MESSAGE"
git push origin production
grunt deploy

References:

https://github.com/EastCloud/grunt-aws-sdk
https://github.com/aws/aws-sdk-js
http://docs.aws.amazon.com/general/latest/gr/rande.html#opsworks_region