diff --git a/lab-07/createbackupterminate.py b/lab-07/createbackupterminate.py new file mode 100644 index 0000000..db0f0fe --- /dev/null +++ b/lab-07/createbackupterminate.py @@ -0,0 +1,37 @@ +import sys + +import boto3 +from botocore.exceptions import ClientError + + +def create_instance(): + ec2_resource = boto3.resource('ec2') + instances = ec2_resource.create_instances(ImageId='ami-6871a115', + MinCount=1, MaxCount=3,InstanceType='t2.micro', + SecurityGroupIds=['sep06'],KeyName='fullstack') + instance_ids = [] + for instance in instances: + instance_ids.append(instance.id) + ec2_client = boto3.client('ec2') + waiter=ec2_client.get_waiter('instance_running') + waiter.wait(InstanceIds=instance_ids) + print ("Instance is Running now!") + +# create_instance() + +def prepare_volume_list(): + mylist = [] + ec2 = boto3.resource('ec2') + for volume in ec2.volumes.all(): + print(volume.volume_id) + mylist.append(volume.volume_id) + return mylist + + +def create_snapshot(): + ec2_resource = boto3.resource('ec2') + list = prepare_volume_list() + for l in list: + ec2_resource.create_snapshot(VolumeId=l, Description="Taking backup") + +create_snapshot() \ No newline at end of file diff --git a/lab-07/createterminateec2.py b/lab-07/createterminateec2.py new file mode 100644 index 0000000..3f44f95 --- /dev/null +++ b/lab-07/createterminateec2.py @@ -0,0 +1,51 @@ +import boto3 + + +def create_instance(): + ec2_resource = boto3.resource('ec2') + instances = ec2_resource.create_instances(ImageId='ami-6871a115', + MinCount=1, MaxCount=3,InstanceType='t2.micro', + SecurityGroupIds=['sep06'],KeyName='fullstack') + instance_ids = [] + for instance in instances: + instance_ids.append(instance.id) + ec2_client = boto3.client('ec2') + waiter=ec2_client.get_waiter('instance_running') + waiter.wait(InstanceIds=instance_ids) + print ( "Created Instances", instance_ids) + +create_instance() + + + + +import boto3 +from botocore.exceptions import ClientError + + +def prepare_instance_list(): + mylist = [] + ec2_resource = boto3.resource('ec2') + instances = ec2_resource.instances.filter( + Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]) + for instance in instances: + print(instance.id, instance.instance_type) + mylist.append(instance.id) + return mylist + +def delete_instances(instance_list): + ec2_client = boto3.client('ec2') + try: + ec2_client.terminate_instances(InstanceIds=instance_list,DryRun=True) + except ClientError as e: + if 'DryRunOperation' not in str(e): + raise + + ec2_client.terminate_instances(InstanceIds=instance_list,DryRun=False) + +instance_list = prepare_instance_list() +if len(instance_list) !=0: + print ( "Deleting Instances", instance_list) + delete_instances(instance_list) +else: + print ("No Instances to delete ") \ No newline at end of file diff --git a/lab-07/ec-create-instance.py b/lab-07/ec-create-instance.py index 03f5ca1..ee70e5e 100644 --- a/lab-07/ec-create-instance.py +++ b/lab-07/ec-create-instance.py @@ -3,9 +3,9 @@ def create_instance(): ec2_resource = boto3.resource('ec2') - instances = ec2_resource.create_instances(ImageId='ami-49f0762d', + instances = ec2_resource.create_instances(ImageId='ami-6871a115', MinCount=1, MaxCount=3,InstanceType='t2.micro', - SecurityGroupIds=['ansible-node'],KeyName='ansible') + SecurityGroupIds=['sep06'],KeyName='fullstack') instance_ids = [] for instance in instances: instance_ids.append(instance.id) @@ -15,3 +15,4 @@ def create_instance(): 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 index 18c54ae..89942a8 100644 --- a/lab-07/ec2-client-list-instances.py +++ b/lab-07/ec2-client-list-instances.py @@ -10,5 +10,5 @@ for i,vv in instance.items(): if i == 'Instances': for ii in vv: - print ii['PublicDnsName'] + print ii['PublicDnsName'] diff --git a/lab-07/ec2-resource-list-instances.py b/lab-07/ec2-resource-list-instances.py index fb4640e..8ea1ed7 100644 --- a/lab-07/ec2-resource-list-instances.py +++ b/lab-07/ec2-resource-list-instances.py @@ -10,6 +10,8 @@ +list_volume() + # Mac installation tip: # sudo pip install --ignore-installed six boto3 # Assignment : Create 3 instances in ec2 using ansible playbooked from last week. diff --git a/lab-07/ec2-terminate-instances.py b/lab-07/ec2-terminate-instances.py index 65358d3..18018a9 100644 --- a/lab-07/ec2-terminate-instances.py +++ b/lab-07/ec2-terminate-instances.py @@ -27,4 +27,4 @@ def delete_instances(instance_list): print ( "Deleting Instances", instance_list) delete_instances(instance_list) else: - print ( "No Instances to delete ") \ No newline at end of file + print ("No Instances to delete ") \ No newline at end of file