Skip to content

Commit 5ec5c51

Browse files
authored
Merge pull request #1 from kchandan/master
added aws examples
2 parents 0a8264b + 86eaa5e commit 5ec5c51

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

lab-07/ec-create-instance.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import boto3
2+
3+
4+
def create_instance():
5+
ec2_resource = boto3.resource('ec2')
6+
instances = ec2_resource.create_instances(ImageId='ami-49f0762d',
7+
MinCount=1, MaxCount=1,InstanceType='t2.micro',
8+
SecurityGroupIds=['ansible-node'],KeyName='ansible')
9+
for instance in instances:
10+
print instance
11+
ec2_client = boto3.client('ec2')
12+
waiter=ec2_client.get_waiter('instance_running')
13+
waiter.wait(InstanceIds=[instances[0].id])
14+
print ("Instance is Running now!")
15+
16+
create_instance()

lab-07/ec2-client-list-instances.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import boto3
2+
3+
ec2_client = boto3.client('ec2')
4+
response = ec2_client.describe_instances()
5+
print(response)

lab-07/ec2-create-snapshot.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import boto3
2+
import sys
3+
4+
5+
def create_snapshot():
6+
ec2_resource = boto3.resource('ec2')
7+
snapshot = ec2_resource.create_snapshot(VolumeId=sys.argv[1], Description="Taking backup")
8+
9+
create_snapshot()

lab-07/ec2-resource-list-instances.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import boto3
2+
3+
4+
ec2_resource = boto3.resource('ec2')
5+
6+
instances = ec2_resource.instances.filter(
7+
Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
8+
for instance in instances:
9+
print(instance.state)
10+
for item in instance.volumes.all():
11+
print item.id
12+
13+
14+
# Mac installation tip:
15+
# sudo pip install --ignore-installed six boto3
16+
# Assignment : Create 3 instances in ec2 using ansible playbooked from last week.
17+
# One the instance are created.
18+
# Use Python boto3 library to Parse the ouput of boto reponse and find
19+
# 1. instance ids, ip address, privateDns, Public DNS
20+
21+

lab-08/s3-examples.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import boto3
2+
3+
my_bucket_name = "bcrbucket"
4+
5+
s3 = boto3.resource('s3')
6+
bucket = s3.Bucket(my_bucket_name)
7+
for obj in bucket.objects.all():
8+
print obj.key,obj.last_modified
9+
10+
11+
s3 = boto3.client('s3')
12+
13+
# Get a paginator and iterate through each page
14+
paginator = s3.get_paginator('list_objects')
15+
for page in paginator.paginate(Bucket=my_bucket_name):
16+
for obj in page['Contents']:
17+
print(obj['Key'])
18+
19+
20+
21+
s3 = boto3.resource('s3')
22+
obj = s3.Object(my_bucket_name,'ny-census.csv')
23+
print(obj.content_type)
24+
print(obj.last_modified)
25+

0 commit comments

Comments
 (0)