Posts

Showing posts with the label Scripting

AMI Age Calculator of Running AWS EC2 Instances and Generate CSV Report

#!/bin/bash # Initialize variables with default values ACCOUNTID="<Your AccountID>" REGION="<Your Region>" OUTPUT_CSV="$ACCOUNTID-$REGION-ami_age_report.csv" # Define the CSV file name rm -rf $OUTPUT_CSV # Parse command line options while getopts "a:b:" option; do case $option in a) ACCOUNTID=${OPTARG} ;; b) REGION=${OPTARG} ;; *) echo "usage: $0 [-a ACCOUNTID] [-b REGION]" >&2 exit 1 ;; esac done # List instances and AMI IDs in the specified region instances_json=$(aws ec2 describe-instances --region "$REGION" --query 'Reservations[*].Instances[*].[InstanceId,ImageId]' --output json) # For local # Get the current timestamp current_time=$(date -u +%s) # Initialize the CSV file with headers echo "AccountID,Region,InstanceID,AMIID,AMIAge (months)" > "$OUTPUT_CSV" # Iterate through instances and append to the CSV file for row in $(echo "$insta...

AWS CLI Get Security Group ID with Name. Python Boto3 [ wild card support] Python Boto3

 """ Author Praveen This Simple Boto Script will list the AWS  security group with name provide [ Wildcard supported] """ import boto3 AWS_REGION = "us-west-2" fullauth = boto3.session.Session(profile_name='<YourProfile>') ec2 = fullauth.client('ec2' , region_name=AWS_REGION) group_name = '*<YourSearchString>' response = ec2.describe_security_groups(     Filters=[         dict(Name='group-name', Values=[group_name])     ] ) for securityGroup in response['SecurityGroups']:    print("SG ID: {}, Name: {}".format(securityGroup['GroupId'], securityGroup['GroupName'])) #This will list Security Group Name along with Security Group ID     print("SG ID: {}".format(securityGroup['GroupId'])) # This will List Only Security Group IDS