Skip to content

added aws examples #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lab-07/ec-create-instance.py
Original file line number Diff line number Diff line change
@@ -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()
5 changes: 5 additions & 0 deletions lab-07/ec2-client-list-instances.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import boto3

ec2_client = boto3.client('ec2')
response = ec2_client.describe_instances()
print(response)
9 changes: 9 additions & 0 deletions lab-07/ec2-create-snapshot.py
Original file line number Diff line number Diff line change
@@ -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()
21 changes: 21 additions & 0 deletions lab-07/ec2-resource-list-instances.py
Original file line number Diff line number Diff line change
@@ -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


25 changes: 25 additions & 0 deletions lab-08/s3-examples.py
Original file line number Diff line number Diff line change
@@ -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)