Skip to content
Open
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
Empty file added Untitled1.py
Empty file.
21 changes: 14 additions & 7 deletions ec2_create.py
Original file line number Diff line number Diff line change
@@ -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)


print(result)
40 changes: 24 additions & 16 deletions ec2_project.py
Original file line number Diff line number Diff line change
@@ -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)

32 changes: 32 additions & 0 deletions ec2_start_tags.py
Original file line number Diff line number Diff line change
@@ -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']))
19 changes: 19 additions & 0 deletions ec2_stop.py
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions ec2stop_reg_tag.py
Original file line number Diff line number Diff line change
@@ -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"
10 changes: 10 additions & 0 deletions ec2teststop.py
Original file line number Diff line number Diff line change
@@ -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))
13 changes: 0 additions & 13 deletions fizzbuzz.py

This file was deleted.

4 changes: 2 additions & 2 deletions lambda_ec2_stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 21 additions & 0 deletions lambda_sqs_date_time.py
Original file line number Diff line number Diff line change
@@ -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)
}

14 changes: 14 additions & 0 deletions sqs_create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#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'))

48 changes: 48 additions & 0 deletions sqssendmessage.py
Original file line number Diff line number Diff line change
@@ -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)