Skip to content
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
63 changes: 32 additions & 31 deletions aws-tools/lambda/stopinstances.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,55 @@
import boto3

# Array of regions we want to scan for running instances
regions=['us-west-1', 'us-west-2', 'us-east-1', 'us-east-2']
regions = ["us-west-1", "us-west-2", "us-east-1", "us-east-2"]

# Dictionary that holds the key/value pair for each region
# key = key e.g. 'us-west-1'
# value = array e.g. ['i-003cf2aedc58219bf', 'i-0852da9f9b599e873', 'i-06b7b658e21e5e726', 'i-00fa6d35fea24c33b', 'i-0f5746bb760cab75b', 'i-021ec39d1d4bf6c7e']
allInstances={}
allInstances = {}

# Array of instances that are running for a region and we want to stop
instances = []

# Go through each of the US regions
for i in regions:
# Connect to the region
region = i
ec2 = boto3.client('ec2', region_name=region)

# Find the instances that are part of our clusters
# The instances tag are tagged as 'master' and 'worker'
# so we filter on that.
response = ec2.describe_instances(
# Connect to the region
region = i
ec2 = boto3.client("ec2", region_name=region)

# Find the instances that are part of our clusters
# The instances tag are tagged as 'master' and 'worker'
# so we filter on that.
response = ec2.describe_instances(
Filters=[
{
'Name': 'tag:Name',
'Values': [
'*master*',
'*worker*',
"Name": "tag:Name",
"Values": [
"*master*",
"*worker*",
],
},
],
)

# Parse the Reservations response and find the instances queried
for r in response['Reservations']:
for i in r['Instances']:
# Append to the array of instances to be stopped.
if i['State']['Name'] == 'running':
instances.append(i['InstanceId'])
# Add the instance array to the dictionary for the region
allInstances[region] = instances
# Reset the array
instances=[]

# Parse the Reservations response and find the instances queried
for r in response["Reservations"]:
for i in r["Instances"]:
# Append to the array of instances to be stopped.
if i["State"]["Name"] == "running":
instances.append(i["InstanceId"])
# Add the instance array to the dictionary for the region
allInstances[region] = instances
# Reset the array
instances = []


# Lambda function to stop the instances in each region
def lambda_handler(event, context):
for key, value in allInstances.items():
ec2 = boto3.client('ec2', key)
if len(value):
ec2.stop_instances(InstanceIds=value)
print('In Region [' + key + '] stopped your instances: ' + str(value))
else:
print('No instances to stop in region: ' + key)
ec2 = boto3.client("ec2", key)
if len(value):
ec2.stop_instances(InstanceIds=value)
print("In Region [" + key + "] stopped your instances: " + str(value))
else:
print("No instances to stop in region: " + key)
68 changes: 38 additions & 30 deletions aws-tools/s3-create.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,78 +8,84 @@
import os
import json

regions = ['us-west-1', 'us-west-2', 'us-east-1', 'us-east-2']
regions = ["us-west-1", "us-west-2", "us-east-1", "us-east-2"]

try:
aws_access_key_id = os.environ['AWS_ACCESS_KEY_ID']
aws_secret_access_key = os.environ['AWS_SECRET_ACCESS_KEY']
aws_access_key_id = os.environ["AWS_ACCESS_KEY_ID"]
aws_secret_access_key = os.environ["AWS_SECRET_ACCESS_KEY"]
except KeyError:
print("Please set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables for AWS access.")
print(
"Please set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables for AWS access."
)
exit()

if not aws_access_key_id or not aws_secret_access_key:
print("AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables cannot be empty.")
print(
"AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables cannot be empty."
)
exit()

argumentList = sys.argv[1:]
options = "hpb:r:"
long_options = ["help", "bucket", "region", "public"]


def usage():
msg = ("Usage: python " + sys.argv[0] + "\n"
"Options: \n"
" -b or --bucket \n"
" S3 bucket name to create \n"
" Example: -b my-bucket \n"
" -r or --region \n"
" AWS region for bucket \n"
" Example: -r us-west-1 \n"
" -h or --help \n"
" Usage message")
msg = (
"Usage: python " + sys.argv[0] + "\n"
"Options: \n"
" -b or --bucket \n"
" S3 bucket name to create \n"
" Example: -b my-bucket \n"
" -r or --region \n"
" AWS region for bucket \n"
" Example: -r us-west-1 \n"
" -h or --help \n"
" Usage message"
)
print(msg)
exit()


def get_s3_client(region):
endpoint_url = f"https://s3.{region}.amazonaws.com"
try:
s3 = boto3.client(
's3',
"s3",
endpoint_url=endpoint_url,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
region_name=region,
config=botocore.client.Config(signature_version='s3v4')
config=botocore.client.Config(signature_version="s3v4"),
)
return s3
except ClientError:
print("Could not retrieve S3 client.")
raise


def create_bucket(s3_client, bucket_name, region, public=None):
try:
# For us-east-1, do not include CreateBucketConfiguration
if region == 'us-east-1':
if region == "us-east-1":
result = s3_client.create_bucket(Bucket=bucket_name)
else:
result = s3_client.create_bucket(
Bucket=bucket_name,
CreateBucketConfiguration={'LocationConstraint': region}
CreateBucketConfiguration={"LocationConstraint": region},
)

if public:
s3_client.put_public_access_block(
Bucket=bucket_name,
PublicAccessBlockConfiguration={
'BlockPublicAcls': False,
'IgnorePublicAcls': False,
'BlockPublicPolicy': False,
'RestrictPublicBuckets': False
}
"BlockPublicAcls": False,
"IgnorePublicAcls": False,
"BlockPublicPolicy": False,
"RestrictPublicBuckets": False,
},
)
arn = [
f"arn:aws:s3:::{bucket_name}",
f"arn:aws:s3:::{bucket_name}/*"
]
arn = [f"arn:aws:s3:::{bucket_name}", f"arn:aws:s3:::{bucket_name}/*"]
policy = {
"Version": "2012-10-17",
"Statement": [
Expand All @@ -90,9 +96,9 @@ def create_bucket(s3_client, bucket_name, region, public=None):
"Action": [
"s3:*",
],
"Resource": arn
"Resource": arn,
}
]
],
}
s3_client.put_bucket_policy(Bucket=bucket_name, Policy=json.dumps(policy))

Expand All @@ -103,6 +109,7 @@ def create_bucket(s3_client, bucket_name, region, public=None):
print(e)
raise


def main():
bucket = ""
region = ""
Expand Down Expand Up @@ -145,5 +152,6 @@ def main():
if result:
print(f"Bucket [{bucket}] created successfully.")


if __name__ == "__main__":
main()
Loading