• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Gokul Deepak

Gokul Deepak

GoCool and Debug

  • Home
  • Terraform
  • WordPress
  • Android
  • How to
  • About Me

Linux

Running crontab on AWS auto scaling group

December 30, 2023 by Gokul Deepak S Leave a Comment

Linux has a convenient feature of running cronjob which will get executed at the correct time. In AWS we have a great feature of scaling out multiple instances based on traffic.

There are some requirements where the admin has to write a cron job which will be saved in the AWS AMI and provisioned in Autoscalling. As the multiple instances will be running a cron job, The single cron job will get executed parallelly in all the instances in ASG(Autoscaling group). This will get conflicted if we want to run the cron job only once.

The script which is provided below will handle the situation in Auto scaling and get only one cron job trigger even if multiple instances are running in AWS ASG.

Running crontab on AWS auto scaling group:

Step 1:

Log in to the instance where you are preparing the Golden Image. If you wonder what the golden image is. It simply means the customized AMI for our application.

Step 2:

vi /opt/asg-cron-script.sh

save the bash script in any location or with any file name as per your requirement.

#!/bin/bash

# Collect some information about the instance

INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)

INSTANCE_REGION=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/.$//')

INSTANCE_ASG=$(aws autoscaling describe-auto-scaling-instances --region $INSTANCE_REGION --instance-ids $INSTANCE_ID --query "AutoScalingInstances[].AutoScalingGroupName" --output text)

# Query the ASG

FIRST_INSTANCE_ID=$(aws autoscaling describe-auto-scaling-groups --region $INSTANCE_REGION --auto-scaling-group-name $INSTANCE_ASG --query "AutoScalingGroups[].Instances[0].InstanceId" --output text)

if [ "$FIRST_INSTANCE_ID" == "$INSTANCE_ID" ]; then

        exit 0 #WRITE YOUR SCRIPT HERE BY REPLACING exit 0

else

        exit 1

fi

To save the file press Esc + wq!

Step 3:

To  call the above script we have to edit the crontab. Enter the below command

crontab -e

Edit the file by adding the below line

* * * * * /bin/bash /opt/asg-cron-script.sh

Step 4:

Go to the AWS console -> IAM services -> Policies -> Create new policy

Add the below json file

{
  "Statement": [
    {
      "Action": [
        "autoscaling:DescribeAutoScalingGroups",
        "autoscaling:DescribeAutoScalingInstances",
        "ec2:DescribeInstanceAttribute",
        "ec2:DescribeInstanceStatus",
        "ec2:DescribeInstances"
      ],
      "Effect": "Allow",
      "Resource": [
        "*"
      ]
    }
  ]
}

Step 5:

Create a role

AWS console -> IAM services -> Roles  -> Create new Role

Step 6:

Attach policy to the new role created and attach the role to the ASG instances/Launch configuration  (“IAM Instance Profile” within the Launch Configuration)

—-Tada—-

Mission Completed

Filed Under: AWS, Linux

Vi and Vim Editor commands

August 23, 2020 by Gokul Deepak S Leave a Comment

Here I’m mentioning all the commands related to Vi and Vim editor that I learn

Command to delete all lines in Vi Editor:

:1,$d 

Delete a single line where the cursor is

dd

Find and /replace

:%s/find_word/replace_word/g

To search a word that has the character forward slash / or any special characters then simply include backward slash \ before that \/

:%s/find\/words\~/replacewords/g

Add Text to beginning of every line – use ˆ
Add Text to end of every line- use $

:%s/ˆ/text/
:%s/$/text/

Remove Blank Lines

:g/^$/d

Remove Blank Lines that contains space

:g/^ *$/d

Filed Under: Linux

How to Send Files from your localhost to Another Server through SSH

June 17, 2020 by Gokul Deepak S Leave a Comment

Objective: Upload Files/Folder from my local to server. (How to Send Files from your localhost to Another Server through SSH)

Challenge: I’m bored to use Filezilla. So just have to use putty or Git Bash or Windows Subsystem.

I have used Ubuntu Subsystem in my Windows 10 for SSH access.

Solution: Use Secure Copy Command from your local machine and login to server directly.

Command:

sudo scp -i key.pem -r /mnt/d/sourcefolder/ [email protected]:/var/html/destination/
  1. Sudo – So to not ask permission for each and every file.
  2. scp – Secure Copy command to copy files between servers. you should use scp so it would use SSH for transferring files and would use authentication like password or pem key.
  3. -i key.pem – this is the key for my server login authentication.
  4. -r -recursive files/subfolders inside the folder.
  5. /mnt/d/sourcefolder/ – Source folder in local.
  6. [email protected]: – user in the particular server and its IPv4. you can also use root or ubuntu.
  7. /var/html/destination – Destination folder

Task Completed Successfully.

Note: Remember that the folder copied to the server is under the user centos/root/ubuntu.

Filed Under: Linux

Primary Sidebar

Popular Pages

  • Home
  • How to find the date of the Facebook group it was created
  • Yureka Yu Plus vs Lenovo K3 Note
  • How to Install Windows without GRUB rescue
  • How to Remove LinkWithin logo under 'You Might Also Like' widget in blogger

Archives

Categories

  • Android
  • Apache
  • AWS
  • Blogger
  • Computer Tips
  • DevOps
  • Docker
  • Earn
  • Facebook Tips
  • Git
  • Google Tips
  • Hosting
  • How to
  • Laptop Tips
  • Laptops
  • Linux
  • Mobile
  • MongoDB
  • Nginx
  • Node JS
  • Notepad++
  • PostgresQL
  • Terraform
  • Ubuntu
  • Wordpress
  • Xenforo

Tags

Android Android One AWS Bluehost Comparisions Custom ROM Elementor Facebook Genesis Genesis Sample Theme Google Drive How to Lineage OS Notes OnePlus One OwnCloud Rooting Smartphones Stock ROM Studiopress Tomcat Wordpress plugin

Copyright © 2025 · Maintained by GokulDeepak