Skip to content

Instantly share code, notes, and snippets.

View anuragmathur1's full-sized avatar

Anurag Mathur anuragmathur1

  • Sydney, AU
View GitHub Profile
@anuragmathur1
anuragmathur1 / create-ec2-instance.sh
Created September 7, 2017 04:07
Create ec2 instance using aws cli.
aws ec2 run-instances \
--image-id ami-xxxxxxxx \
--instance-type m4.large \
--security-group-ids sg-xxxxxxxx sg-xxxxxxxx sg-xxxxxxxx \
--subnet-id subnet-xxxxxxxx \
--iam-instance-profile Arn=arn:aws:iam::xxxxxxxxxxxx:instance-profile/instance-profile \
--count 1 \
--block-device-mappings '[ { "DeviceName": "/dev/xvda", "Ebs": {"VolumeSize": 100, "VolumeType": "gp2"}}]' \
--key-name instance-key \
--associate-public-ip-address \
@anuragmathur1
anuragmathur1 / create-rds-instance.sh
Last active September 7, 2017 04:16
Create RDS instance from cli - Example
aws rds create-db-instance \
--db-name newdb \
--db-instance-identifier my-instance \
--db-instance-class db.t2.micro \
--engine MySQL \
--engine-version 5.6.35 \
--master-username dbuser \
--master-user-password dbpwd \
--vpc-security-group-ids sg-xxxxxxxx \
--db-subnet-group-name db-subnet-group \
@anuragmathur1
anuragmathur1 / create-cloudformation-stack.sh
Created September 7, 2017 04:15
Example of creating a cloudformation stack with example parameters
aws cloudformation create-stack \
--stack-name my-stack \
--template-body file:///full-path-to-file/my-stack.template \
--parameters \
ParameterKey=DynaoDbReadCapacity,ParameterValue=2 \
ParameterKey=DynaoDbWriteCapacity,ParameterValue=2 \
ParameterKey=LambdaBucket,ParameterValue=Bucket-name \
--profile=work
@anuragmathur1
anuragmathur1 / find_all_ec2.sh
Created September 20, 2017 06:16
Find all instances and list their Private IP, instance-id and Name
# Find all instances and list their Private IP, instance-id and Name
# You may specify more filters like the vpc
# Name=vpc-id,Values=vpc-xxxxxxxx
aws ec2 describe-instances --filters --query 'Reservations[].Instances[].[PrivateIpAddress,InstanceId,Tags[?Key==`Name`].Value[]]' --output text --profile=elmodev | sed '$!N;s/\n/ /'
@anuragmathur1
anuragmathur1 / cleanup_ami_by_date.py
Last active December 21, 2017 03:58
Delete AMIs and associated snapshots created on a given date
#!/usr/bin/python
import boto3
client = boto3.client('ec2')
account_number = boto3.client('sts').get_caller_identity()['Account']
response = client.describe_images(Owners=[account_number])
for image in response['Images']:
if "2017-08-21" in image['CreationDate']:
image_id = image['ImageId']
snapshot_id = image['BlockDeviceMappings'][0]['Ebs']['SnapshotId']
@anuragmathur1
anuragmathur1 / run_remote_jenkins_job.rb
Created April 13, 2018 05:34
Example : Ruby script to invoke Jenkins job remotely
require 'jenkins_api_client'
SERVER_IP = 'jenkins.dev.example.com'
$build = JenkinsApi::Client.new(:server_ip => "#{SERVER_IP}",:username => 'admin', :password => 'admin')
$opts = {'build_start_timeout' => 60, 'cancel_on_build_start_timeout' => true}
$build.job.build("test-project",{:parameter1=>"value-1",\
:parameter2=>"value-2",\
:parameter3=>"value-3",\
:parameter4=>"value-4"} || {}, $opts)
@anuragmathur1
anuragmathur1 / delete_all_s3_Bucket_versions.py
Created May 7, 2019 05:02
You can not use cli to delete all versions of an object in a bucket. That will prevent you from removing the bucket as well. The below 4 lines will delete all versions of all all objects in the given s3 bucket.
#!/usr/bin/env python
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('<<Bucket-name-here>>')
bucket.object_versions.all().delete()
@anuragmathur1
anuragmathur1 / install-aws-cdk.sh
Created July 17, 2019 23:48
Install aws cdk - ubuntu18
apt-get install python3-pip -y
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 1
sudo update-alternatives --config python
sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1
sudo update-alternatives --config pip
pip install aws-cdk.cdk
@anuragmathur1
anuragmathur1 / delete-snapshots.py
Created October 1, 2019 05:24
Delete EBS snapshots that are not attached to any AMI and are at least 90 days old
#!/usr/bin/python
## The script deletes the snapshots that are :
## - not attached to an AMI
## - are atleast 90 days old
## Output :
## - soem csv files if a report is required
## - the return values ( return_code && request_id from the delete command )
## Usage
@anuragmathur1
anuragmathur1 / pull_all_repos.sh
Created October 4, 2019 03:57
Pull all gitlab repos from the group
GITLAB_GROUP="<group-name>"
GITLAB_PRIVATE_TOKEN="<token>"
GITLAB_ADDRESS="gitlab.<name>"
for i in `curl -s "https://$GITLAB_ADDRESS/api/v4/groups/$GITLAB_GROUP/projects?private_token=$GITLAB_PRIVATE_TOKEN&amp;per_page=999" | \
grep -o "\"ssh_url_to_repo\":[^ ,]\+" | \
awk -F ':' '{gsub(/"/, "", $2); gsub(/"/, "", $3); print $2":"$3}'`; do git clone $i; done