• 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

AWS

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

AWS Instance Types

October 11, 2022 by Gokul Deepak S Leave a Comment

There are different types of AWS instances. Here you will also know for which requirement a particular instance type has to be used. Let’s start with the naming convention of the instance types.

AWS Naming conventions:

m5.2xlarge

m: instance class

5: Generation

2xlarge: size within the instance class

General Purpose:

Mostly for diversified workloads with balanced resources.

The balance between Compute, Memory, Networking

Use cases:

  • Webservers
  • Code Repositories

Compute Optimized:

Mostly used for compute-intensive tasks which require high-performance processors

Use cases:

  • Batch processing workloads
  • Media transcoding
  • High-performance web servers
  • High-performance computing (HPC)
  • Scientific modelling and machine learning
  • Dedicated gaming servers

Memory Optimized:

Mostly for workloads which process large data sets in memory.

Use cases:

  • High-performance, relational/non-relational databases
  • Distributed web-scale cache stores
  • In-memory databases optimized for BI(Business intelligence)
  • Applications performing real-time processing of big unstructured data

Storage Optimized:

Mostly used for storage-intensive tasks which require high, sequential read and write access to large data sets on local storage.

Use Case:

  • High-frequency online transaction processing (OLTP) systems
  • Relational and NoSQL databases
  • Data warehousing applications
  • Cache for in-memory databases like Redis, Memcache
  • Distributed file systems

All information on EC2instances is available at ec2instances.info

Filed Under: AWS Tagged With: AWS, Notes

Primary Sidebar

Popular Pages

  • Home
  • Important Docker Commands to remember
  • Terraform Cheat sheet
  • Learn with Gokul
  • CORS error in Apache Tomcat - Solved
  • Runcloud vs SiteGround Comparision
  • Strategy to prepare for Terraform Associate Exam Preparation
  • How to find the date of the Facebook group it was created
  • How to remove Linux OS in Tamilnadu Government Free Laptop
  • Running crontab on AWS auto scaling group

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