From e75b09bfcd9681df44301ba545ca8d3b23a6b695 Mon Sep 17 00:00:00 2001 From: darcyjwood Date: Mon, 18 Jul 2022 00:15:16 +0000 Subject: [PATCH 1/3] changes --- Untitled1.py | 0 ec2_create.py | 21 +++++++++++++------- ec2_project.py | 40 ++++++++++++++++++++++---------------- ec2_start_tags.py | 32 +++++++++++++++++++++++++++++++ ec2_stop.py | 19 ++++++++++++++++++ ec2stop_reg_tag.py | 12 ++++++++++++ ec2teststop.py | 10 ++++++++++ fizzbuzz.py | 13 ------------- lambda_ec2_stop.py | 4 ++-- sqs_create.py | 13 +++++++++++++ sqssendmessage.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 174 insertions(+), 38 deletions(-) create mode 100644 Untitled1.py create mode 100644 ec2_start_tags.py create mode 100644 ec2_stop.py create mode 100644 ec2stop_reg_tag.py create mode 100644 ec2teststop.py delete mode 100644 fizzbuzz.py create mode 100644 sqs_create.py create mode 100644 sqssendmessage.py diff --git a/Untitled1.py b/Untitled1.py new file mode 100644 index 0000000..e69de29 diff --git a/ec2_create.py b/ec2_create.py index 1ea8523..e82c9d8 100644 --- a/ec2_create.py +++ b/ec2_create.py @@ -1,12 +1,19 @@ -#create ec2 +#create 3 ec2s import boto3 ec2_resource = boto3.resource("ec2") +#this will generate instances based on type and ammount and will tag them +result = ec2_resource.create_instances(ImageId = 'ami-0cff7528ff583bf9a', + InstanceType = 't2.micro', + MaxCount = 3, + MinCount = 3, + TagSpecifications = [ + { + 'ResourceType': 'instance', + 'Tags': [{'Key': 'Environment','Value': 'Dev'}], + }, + ], + ) -ec2_resource.create_instances(ImageId = 'ami-0cff7528ff583bf9a', - InstanceType = 't2.micro', - MaxCount = 3, - MinCount = 3) - - \ No newline at end of file +print(result) \ No newline at end of file diff --git a/ec2_project.py b/ec2_project.py index f60d3cf..c656da6 100644 --- a/ec2_project.py +++ b/ec2_project.py @@ -1,19 +1,27 @@ -#create 3 ec2s - import boto3 -ec2_resource = boto3.resource("ec2") -#this will generate instances based on type and ammount and will tag them -result = ec2_resource.create_instances(ImageId = 'ami-0cff7528ff583bf9a', - InstanceType = 't2.micro', - MaxCount = 3, - MinCount = 3, - TagSpecifications = [ - { - 'ResourceType': 'instance', - 'Tags': [{'Key': 'Environment','Value': 'Dev'}], - }, - ], - ) -print(result) +def lambda_handler(event, context): + ec2_client = boto3.client('ec2') + regions = [region['RegionName'] + for region in ec2_client.describe_regions()['Regions']] + + for region in regions: + ec2 = boto3.resource('ec2', region_name=region) + + print("Region:", region) + + running_instances = ec2.instances.filter( + Filters=[{'Name': 'instance-state-name', + 'Values': ['running']}]) + tagged_instances = ec2.instances.filter( + Filters=[{'ResourceType': 'instance', + 'Tags': [{'Key': 'Environment','Value': 'Dev'}] + }]) + + instances = ('running_instances' + 'tagged_instances') + + for instance in instances: + instance.stop() + print('Stopped instance: ', instance.id) + diff --git a/ec2_start_tags.py b/ec2_start_tags.py new file mode 100644 index 0000000..5be49fa --- /dev/null +++ b/ec2_start_tags.py @@ -0,0 +1,32 @@ +import boto3 + +ec2_resource = boto3.resource("ec2") + +x = ec2_resource.create_instances(ImageId = 'ami-0cff7528ff583bf9a', + InstanceType = 't2.micro', + MaxCount = 1, + MinCount = 1,#change counts to add multiple + TagSpecifications = [ + { + 'ResourceType': 'instance', + 'Tags': [{'Key': 'Test1','Value': 'Test1pair'}] + }, + ], + ) + +print(x) + +x = ec2_resource.create_instances(ImageId = 'ami-0cff7528ff583bf9a', + InstanceType = 't2.micro', + MaxCount = 1, + MinCount = 1,#change counts to add multiple + TagSpecifications = [ + { + 'ResourceType': 'instance', + 'Tags': [{'Key': 'Test2','Value': 'Test2pair'}] + }, + ], + ) + +for i in resonse['Instances']: +print("Instance ID Created is :{} Instance Type Created is : {}" .format(i['InstanceId'],i['InstanceType'])) \ No newline at end of file diff --git a/ec2_stop.py b/ec2_stop.py new file mode 100644 index 0000000..785bade --- /dev/null +++ b/ec2_stop.py @@ -0,0 +1,19 @@ +#create 3 ec2s + +import boto3 + +ec2_resource = boto3.resource("ec2") +#this will generate instances based on type and ammount and will tag them +result = ec2_resource.create_instances(ImageId = 'ami-0cff7528ff583bf9a', + InstanceType = 't2.micro', + MaxCount = 3, + MinCount = 3, + TagSpecifications = [ + { + 'ResourceType': 'instance', + 'Tags': [{'Key': 'Environment','Value': 'Dev'}], + }, + ], + ) + +print(result) \ No newline at end of file diff --git a/ec2stop_reg_tag.py b/ec2stop_reg_tag.py new file mode 100644 index 0000000..4c08231 --- /dev/null +++ b/ec2stop_reg_tag.py @@ -0,0 +1,12 @@ +import json +import boto3 + +ec2 = boto3.resource('ec2', region_name='us-east-1') + +#def lambda_handler(event, context): +instances = ec2.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['Running']},{'Name': 'tag:Enironment','Values':['Dev']}])#enter your EC2 tag key and id where I have "test1" and "test1pair" +for instance in instances: + id=instance.id + ec2.instances.filter(InstanceIds=[id]).stop() + print("Instance ID is started :- "+instance.id) + #return "success" \ No newline at end of file diff --git a/ec2teststop.py b/ec2teststop.py new file mode 100644 index 0000000..e494493 --- /dev/null +++ b/ec2teststop.py @@ -0,0 +1,10 @@ +import boto3 +region = 'us-east-1' + +ec2 = boto3.client('ec2', region_name='region') + +instances = ec2.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['Running']},{'Name': 'Environment','Values':['Dev']}]) + +def lambda_handler(event, context): + ec2.stop_instances(InstanceIds=instances) + print('stopped your instances: ' + str(instances)) \ No newline at end of file diff --git a/fizzbuzz.py b/fizzbuzz.py deleted file mode 100644 index 6f72d43..0000000 --- a/fizzbuzz.py +++ /dev/null @@ -1,13 +0,0 @@ -#fizzbuzz -upper_number = int(input('How many values should we process: ')) - -for number in range(1, upper_number + 1): - if number % 3 == 0 and number % 5 == 0: - print("FizzBuzz") - elif number % 3 == 0: - print("Fizz") - elif number % 5 == 0: - print("Buzz") - else: - print(number) - diff --git a/lambda_ec2_stop.py b/lambda_ec2_stop.py index 6dedcd3..d1e8924 100644 --- a/lambda_ec2_stop.py +++ b/lambda_ec2_stop.py @@ -16,8 +16,8 @@ def lambda_handler(event, context): # Get only running instances instances = ec2.instances.filter( - Filters=[{'Name': 'instance-state-name', - 'Values': ['running']}]) + Filters=[{'Name': 'instance-state-name', 'Values': ['running']}, + {'Tags': 'Key': 'Environment','Value': ['Dev']}]) # Stop the instances for instance in instances: diff --git a/sqs_create.py b/sqs_create.py new file mode 100644 index 0000000..e6ba6a5 --- /dev/null +++ b/sqs_create.py @@ -0,0 +1,13 @@ +#creates a basic SQS Queue + +import boto3 +import time + +#get the service resource +sqs = boto3.resource('sqs') + +#get the queue and give name and attributes +queue = sqs.create_queue(QueueName = 'MySQSQueue', Attributes = {'DelaySeconds': '5'}) + +print(queue.url) +print(queue.attributes.get('DelaySeconds')) \ No newline at end of file diff --git a/sqssendmessage.py b/sqssendmessage.py new file mode 100644 index 0000000..80797d2 --- /dev/null +++ b/sqssendmessage.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3.7 +# -*- coding: utf-8 -*- +import argparse +import logging +import sys +from time import sleep + +import boto3 +from faker import Faker + +parser = argparse.ArgumentParser() +parser.add_argument("--queue-name", "-q", required=True, + help="SQS queue name") +parser.add_argument("--interval", "-i", required=True, + help="timer interval", type=float) +parser.add_argument("--message", "-m", help="message to send") +parser.add_argument("--log", "-l", default="INFO", + help="logging level") +args = parser.parse_args() + +if args.log: + logging.basicConfig( + format='[%(levelname)s] %(message)s', level=args.log) + +else: + parser.print_help(sys.stderr) + +sqs = boto3.client('sqs') + +response = sqs.get_queue_url(QueueName=args.queue_name) + +queue_url = response['QueueUrl'] + +logging.info(queue_url) + +while True: + message = args.message + if not args.message: + fake = Faker() + message = fake.text() + + logging.info('Sending message: ' + message) + + response = sqs.send_message( + QueueUrl=queue_url, MessageBody=message) + + logging.info('MessageId: ' + response['MessageId']) + sleep(args.interval) From 63ff431939338abde8e4c24023041ec714476797 Mon Sep 17 00:00:00 2001 From: darcyjwood Date: Mon, 18 Jul 2022 05:39:58 +0000 Subject: [PATCH 2/3] final --- sqs_create.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sqs_create.py b/sqs_create.py index e6ba6a5..6e12760 100644 --- a/sqs_create.py +++ b/sqs_create.py @@ -10,4 +10,5 @@ queue = sqs.create_queue(QueueName = 'MySQSQueue', Attributes = {'DelaySeconds': '5'}) print(queue.url) -print(queue.attributes.get('DelaySeconds')) \ No newline at end of file +print(queue.attributes.get('DelaySeconds')) + From c5ff1946367d0201a0878fed45e0e6bb675646b3 Mon Sep 17 00:00:00 2001 From: darcyjwood Date: Mon, 18 Jul 2022 05:43:03 +0000 Subject: [PATCH 3/3] lambda function --- lambda_sqs_date_time.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 lambda_sqs_date_time.py diff --git a/lambda_sqs_date_time.py b/lambda_sqs_date_time.py new file mode 100644 index 0000000..469a78e --- /dev/null +++ b/lambda_sqs_date_time.py @@ -0,0 +1,21 @@ +import json +import boto3 + +from datetime import datetime + +def lambda_handler(event, context): + + now = datetime.now() + date_time = now.strftime('%m/%d/%Y, %H:%M:%S:%p') + + sqs = boto3.client("sqs") + sqs.send_message( + QueueUrl = 'https://sqs.us-east-1.amazonaws.com/xxxxxxxxxxxx/MySQSQueue', + MessageBody = date_time + ) + + return { + 'statusCode': 200, + 'body': json.dumps(date_time) + } + \ No newline at end of file