From 86eaa5e0fe0b4187f8b68bf8f48beb9f10ce7ed7 Mon Sep 17 00:00:00 2001 From: Chandan Kumar Date: Mon, 10 Sep 2018 22:59:54 -0500 Subject: [PATCH] added aws examples --- lab-07/ec-create-instance.py | 16 ++++++++++++++++ lab-07/ec2-client-list-instances.py | 5 +++++ lab-07/ec2-create-snapshot.py | 9 +++++++++ lab-07/ec2-resource-list-instances.py | 21 +++++++++++++++++++++ lab-08/s3-examples.py | 25 +++++++++++++++++++++++++ 5 files changed, 76 insertions(+) create mode 100644 lab-07/ec-create-instance.py create mode 100644 lab-07/ec2-client-list-instances.py create mode 100644 lab-07/ec2-create-snapshot.py create mode 100644 lab-07/ec2-resource-list-instances.py create mode 100644 lab-08/s3-examples.py diff --git a/lab-07/ec-create-instance.py b/lab-07/ec-create-instance.py new file mode 100644 index 0000000..733e299 --- /dev/null +++ b/lab-07/ec-create-instance.py @@ -0,0 +1,16 @@ +import boto3 + + +def create_instance(): + ec2_resource = boto3.resource('ec2') + instances = ec2_resource.create_instances(ImageId='ami-49f0762d', + MinCount=1, MaxCount=1,InstanceType='t2.micro', + SecurityGroupIds=['ansible-node'],KeyName='ansible') + for instance in instances: + print instance + ec2_client = boto3.client('ec2') + waiter=ec2_client.get_waiter('instance_running') + waiter.wait(InstanceIds=[instances[0].id]) + print ("Instance is Running now!") + +create_instance() diff --git a/lab-07/ec2-client-list-instances.py b/lab-07/ec2-client-list-instances.py new file mode 100644 index 0000000..a07c88e --- /dev/null +++ b/lab-07/ec2-client-list-instances.py @@ -0,0 +1,5 @@ +import boto3 + +ec2_client = boto3.client('ec2') +response = ec2_client.describe_instances() +print(response) \ No newline at end of file diff --git a/lab-07/ec2-create-snapshot.py b/lab-07/ec2-create-snapshot.py new file mode 100644 index 0000000..a79ba1b --- /dev/null +++ b/lab-07/ec2-create-snapshot.py @@ -0,0 +1,9 @@ +import boto3 +import sys + + +def create_snapshot(): + ec2_resource = boto3.resource('ec2') + snapshot = ec2_resource.create_snapshot(VolumeId=sys.argv[1], Description="Taking backup") + +create_snapshot() \ No newline at end of file diff --git a/lab-07/ec2-resource-list-instances.py b/lab-07/ec2-resource-list-instances.py new file mode 100644 index 0000000..5e20645 --- /dev/null +++ b/lab-07/ec2-resource-list-instances.py @@ -0,0 +1,21 @@ +import boto3 + + +ec2_resource = boto3.resource('ec2') + +instances = ec2_resource.instances.filter( + Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]) +for instance in instances: + print(instance.state) + for item in instance.volumes.all(): + print item.id + + +# Mac installation tip: +# sudo pip install --ignore-installed six boto3 +# Assignment : Create 3 instances in ec2 using ansible playbooked from last week. +# One the instance are created. +# Use Python boto3 library to Parse the ouput of boto reponse and find +# 1. instance ids, ip address, privateDns, Public DNS + + diff --git a/lab-08/s3-examples.py b/lab-08/s3-examples.py new file mode 100644 index 0000000..292dfff --- /dev/null +++ b/lab-08/s3-examples.py @@ -0,0 +1,25 @@ +import boto3 + +my_bucket_name = "bcrbucket" + +s3 = boto3.resource('s3') +bucket = s3.Bucket(my_bucket_name) +for obj in bucket.objects.all(): + print obj.key,obj.last_modified + + +s3 = boto3.client('s3') + +# Get a paginator and iterate through each page +paginator = s3.get_paginator('list_objects') +for page in paginator.paginate(Bucket=my_bucket_name): + for obj in page['Contents']: + print(obj['Key']) + + + +s3 = boto3.resource('s3') +obj = s3.Object(my_bucket_name,'ny-census.csv') +print(obj.content_type) +print(obj.last_modified) +