capistrano3-asg-ami
Capistrano 3 plugin for updating AWS Launch Template AMI with autoscale group first healthy instance.
Requirements
- aws-sdk-ec2 ~> 1
- aws-sdk-autoscaling ~> 1
- capistrano ~> 3
Installation
Add this line to your application's Gemfile:
gem 'capistrano3-asg-ami'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install capistrano3-asg-ami
Add this line to your application's Capfile:
require 'capistrano/autoscaling'
Usage
Create IAM with AmazonEC2FullAccess permission and Set credentials in the capistrano deploy script / stage files add the following lines
set :aws_region, 'ap-northeast-1'
set :aws_access_key_id, 'YOUR AWS KEY ID'
set :aws_secret_access_key, 'YOUR AWS SECRET KEY'
set :aws_autoscaling_group_name, 'YOUR NAME OF AUTO SCALING GROUP NAME'
set :aws_launch_template_id, 'Your AUTOSCALE GROUP Launch template ID'
set :aws_autoscale_ami_prefix, 'server-name-'
set :aws_keep_prev_no_of_ami, 1
To call this task after deploy, in your desire capistrano environment file add the following in deploy section
namespace :deploy do
after :finishing, 'autoscaling:update_ami'
end
And to update autoscale group launch template manually
cap [stage] autoscaling:update_ami
i.e [stage] can be staging or production
How this works
- Fetch only running instances that have an auto scaling group name you specified
- Create AMI with first healthy instance
- Tag AMI with autoscale group name
- Wait for AMI to be available for use
- Update Launch template with update AMI
- Deleting stale images base on (aws_keep_prev_no_of_ami) variable if not set will only keep the latest one and delete the other AMI tagged with autoscale group name
Contributing
- Fork it ( https://github.com/Aftab-Akram/capistrano3-asg-ami/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
To test while developoing just bundle console
on the project root directory and execute
Capistrano::AutoScaling::VERSION
for a quick test
Contact
Aftab Akram - LinkedIn - aftabakram04@gmail.com