This post is useful for people who are maintaining their code on below scenarios:
- You are using aws cloud server to manage your application codebase.
- Using aws autoscaling feature with launch configuration for autoscaling server.
- After server codebase updated, you create instance AMI then create a launch configuration using that AMI and then updating the autoscaling group to use the new launch configuration for auto scale.
So below shell script first create an ami of an instance, then use that ami to create a launch configuration and then update the autoscaling group to use that new launch configuration.
This script accepts 4 parameter as input: (There parameters are just user defined variable in script to use)
- server_type: This is to get input for which server you want to execute the process if you are having multiple servers like dev, qa, pre production(stage), production(live) etc.
- instance-id: instance id for which AMI needs to be created.
- ami-description: Describe the purpose of AMI.
- launch-configuration-name: This name is to use for AMI name and launch configuration name.
NOTE: The parameter can be reduced based on requirement like launch-configuration-name can be avoided by using current date time as suffix for any suitable pre defined name.
You can even run the script on the instance itself for which you want run the process. with this, instance id can be fetched directly from the server using aws cli command and no need to get input from user during script execution.
Prerequisite:
- You need to have aws cli installed and setup so that aws cli command in script will get executed.
- You need to have jq installed.
https://stedolan.github.io/jq/download/
(jq is like sed for JSON data – you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text)
Script:
function executeprocess { read_server_type read_instance_id read_ami_description read_lc_name # Check if all inputs are received from user and to proceed to next step or not. # else keep asking user for correct input if [ "$server_type" != "" ] && [ "$instance_id" != "" ] && [ "$ami_description" != "" ] && [ "$launch_configuration_name" != "" ]; then echo "server_type: $server_type" echo "instance-id: $instance_id" echo "ami-description: $ami_description" echo "launch-configuration-name: $launch_configuration_name" read_confirm_action else echo "please enter correct inputs" clear executeprocess fi } # Get server type on which server the operation needs to be executed. function read_server_type { read -p "Server Type[dev/qa/live]:" server_type if [ "$server_type" == "" ]; then read_server_type fi } # Get instance id for which a AMI needs to be created. function read_instance_id { read -p "instance-id to create AMI:" instance_id if [ "$instance_id" == "" ]; then read_instance_id fi } # Get amit description. function read_ami_description { read -p "AMI Description:" ami_description if [ "$ami_description" == "" ]; then read_ami_description fi } # Get launch configuration name. function read_lc_name { read -p "launch-configuration-name:" launch_configuration_name if [ "$launch_configuration_name" == "" ]; then read_lc_name fi } # Read confirmation action from user to proceed or not. once confirmed then execute aws cli commands. function read_confirm_action { read -p "Do you want to continue[y/n]:" confirm_action if [ "$confirm_action" == "" ]; then read_confirm_action elif [ "$confirm_action" == "y" ]; then if [ "$server_type" == "dev" ] || [ "$server_type" == "qa" ] || [ "$server_type" == "live" ]; then ami_id="$(aws ec2 create-image --instance-id "$instance_id" --name "$launch_configuration_name" --description "$ami_description" --no-reboot --block-device-mappings "[{"DeviceName": "/dev/sda1","Ebs":{"VolumeSize":20, "VolumeType":"gp2", "Encrypted":false, "DeleteOnTermination":true}}]" --output json | jq -r .ImageId)" echo "AMI created: $ami_id" echo $ami_id exit; fi if [ "$server_type" == "dev" ]; then aws autoscaling create-launch-configuration --key-name "pem-file-name-key" --instance-type "t1.micro" --security-groups sg-abcdefg --instance-monitoring Enabled=false --no-ebs-optimized --block-device-mappings "[{"DeviceName": "/dev/sda1","Ebs":{"VolumeSize":8, "VolumeType":"gp2", "DeleteOnTermination":true}}]" --launch-configuration-name "$launch_configuration_name" --image-id $ami_id aws autoscaling update-auto-scaling-group --auto-scaling-group-name dev-ASG --launch-configuration-name "$launch_configuration_name" echo "Autoscaling updated." elif [ "$server_type" == "qa" ]; then aws autoscaling create-launch-configuration --key-name "pem-file-name-key" --instance-type "t1.micro" --security-groups sg-abcdefg --instance-monitoring Enabled=false --no-ebs-optimized --block-device-mappings "[{"DeviceName": "/dev/sda1","Ebs":{"VolumeSize":8, "VolumeType":"gp2", "DeleteOnTermination":true}}]" --launch-configuration-name "$launch_configuration_name" --image-id $ami_id aws autoscaling update-auto-scaling-group --auto-scaling-group-name qa-ASG --launch-configuration-name "$launch_configuration_name" echo "Autoscaling updated." elif [ "$server_type" == "live" ]; then aws autoscaling create-launch-configuration --key-name "pem-file-name-key" --instance-type "t1.micro" --security-groups sg-abcdefg --instance-monitoring Enabled=false --no-ebs-optimized --block-device-mappings "[{"DeviceName": "/dev/sda1","Ebs":{"VolumeSize":8, "VolumeType":"gp2", "DeleteOnTermination":true}}]" --launch-configuration-name "$launch_configuration_name" --image-id $ami_id aws autoscaling update-auto-scaling-group --auto-scaling-group-name live-ASG --launch-configuration-name "$launch_configuration_name" echo "Autoscaling updated." else echo 'Wrong server type entered. Script execution aborted.' fi elif [ "$confirm_action" == "n" ]; then exit fi } executeprocess
NOTE:
- The AWS command present in the script above has some demo values like –key-name, –block-device-mappings, –security-groups etc, for which you need to give the correct values based on your server configuration.
- Values start with $ symbol should be as it is since those are the dynamic values assigned during the process.
- Once modified the AWS command as per need, then just save the script in a file with .sh extension and execute the .sh file from command line. (EX: sh aws-update.sh)