Advertisement Header

Thursday, 16 February 2017

How to Delete Multiple AWS S3 buckets which are in different region

We usually can delete multiple buckets in same region, but S3 is not region centric and sometimes we may need to delete buckets from different regions at same time, So I consider to have below online shell script to help for the same:

Step 1: Place required buckets names to deleted in a file e.g. list.txt, you can get the same using aws s3 ls --output text using AWSCLI

Step 2: You can use below one liner to delete buckets which are mentioned in above file from shell command prompt where you installed AWSCLI for linux.


for i in `cat list.txt`;do j="$(aws s3api get-bucket-location --bucket $i --output text)"; aws s3 rb s3://$i --region $j --force;done

Above liner explanation:

Shell script related:
Declared 2 variables 
i = To store bucket name from file called list.txt
j = region name of the bucket
Usual for loop

AWS related commands:
aws s3api get-bucket-location  -- will help us to get bucket location(aka region)
aws s3 rb                                   -- will help us to delete bucket even though bucket is not
                                                      empty with --force option


            

Monday, 30 January 2017

What's the difference between list, tuples and dictionaries in Python?

Lists

Lists are what they seem - a list of values. Each one of them is numbered, starting from zero - the first one is numbered zero, the second 1, the third 2, etc. You can remove values from the list, and add new values to the end.

Example: Your many cats' names.

[code]
"A list"
cats = ['Tom', 'Snappy', 'Kitty', 'Jessie', 'Chester']
"If you want to print for example the first name on the list you would do the following"
print cats[0]
"Also if you want to add a name to the list you would do this"
cats.append('Catherine')
"To remove a name you would do this"
del cats[0]
[/code]

Tuples

Tuples are just like lists, but you can't change their values. The values that you give it first up, are the values that you are stuck with for the rest of the program. Again, each value is numbered starting from zero, for easy reference.

Example: the names of the months of the year.

[code]
"Here is an example of a tuple"
months = ('January','February','March','April','May','June',\
'July','August','September','October','November',' December')
[/code]

"So that’s the difference between lists and tuples. A list can be modified but a tuple cannot be modified in anyway(unless changed in the source code of the program)"

Dictionaries:

Dictionaries are similar to what their name suggests - a dictionary. In a dictionary, you have an 'index' of words, and for each of them a definition. In python, the word is called a 'key', and the definition a 'value'. The values in a dictionary aren't numbered - tare similar to what their name suggests - a dictionary. In a dictionary, you have an 'index' of words, and for each of them a definition. In python, the word is called a 'key', and the definition a 'value'. The values in a dictionary aren't numbered - they aren't in any specific order, either - the key does the same thing. You can add, remove, and modify the values in dictionaries.

Example: telephone book.

[code]
"Here is a simple phonebook"
phonebook = {'Andrew Parson':8806336, \
'Emily Everett':6784346, 'Peter Power':7658344, \
'Lewis Lame':1122345}
"To add a name to the phonebook you would do the following"
phonebook['Gingerbread Man'] = 1234567
"If you want to remove a name you would do the same as you would do in a list"
del phonebook['Andrew Parson']
[/code]

How to describe instances from all regions using AWS CLI

for region in `aws ec2 describe-regions --output text | cut -f3`
do
echo -e "\nListing Instances in region:'$region'..."
aws ec2 describe-instances --region $region
done
As of Today (30-01-2017), You will get list of instances from below 14 regions are available in AWS
ap-south-1
eu-west-2
eu-west-1
ap-northeast-2
ap-northeast-1
sa-east-1
ca-central-1
ap-southeast-1
ap-southeast-2
eu-central-1
us-east-1
us-east-2
us-west-1
us-west-2