Skip to content

Instantly share code, notes, and snippets.

View Burekasim's full-sized avatar

Avi Keinan Burekasim

  • DoiT International
  • Israel
  • 05:57 (UTC +03:00)
View GitHub Profile
@Burekasim
Burekasim / ec2.py
Created April 4, 2021 11:37
ec2.py
#!/usr/local/bin/python3
import os
import sys
import boto3
import time
import platform
import subprocess
from botocore.exceptions import ClientError
from queue import Queue
from threading import Thread
@Burekasim
Burekasim / lambda_eip_runinstances.py
Created March 14, 2021 06:03
lambda function that automatically attach EIP on instance launch
from botocore.exceptions import ClientError
from random import randint
from time import sleep
import logging
import boto3
import json
import os
logger = logging.getLogger()
logger.setLevel(logging.INFO)
@Burekasim
Burekasim / eip_iam_permissions.json
Created March 14, 2021 06:00
The IAM permissions required for the lambda function
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "ec2:AssociateAddress",
"Resource": "arn:aws:ec2:*:*:instance/*"
},
{
from botocore.exceptions import ClientError
from random import randint
from time import sleep
import logging
import boto3
import json
import os
'''
import os
import json
import boto3
from botocore.exceptions import ClientError
from base64 import b64encode
def lambda_handler(event, context):
# use 'AWS_REGION' environment variable from lambda built-in variables
aws_region = os.environ['AWS_REGION']
import os
import boto3
import string
import random
import logging
import json
from botocore.exceptions import ClientError
def hours_to_seconds(hour: int):
@Burekasim
Burekasim / s3_lifecycle_lambda.py
Created December 16, 2020 07:00
lambda function that creates a lifecycle rule once a bucket is created
import boto3
def put_lifecycle_policy(region: str, bucket: str, policy):
s3 = boto3.client('s3', region_name=region)
try:
response = s3.put_bucket_lifecycle_configuration(Bucket=bucket, LifecycleConfiguration=policy)
except Exception as e:
print(str(e))
return response
@Burekasim
Burekasim / s3_stats.py
Created December 6, 2020 17:10
s3 sum objects size with multipart upload listing
import boto3
from datetime import timedelta, datetime
### Logic:
# this script will list all aws buckets and will retrieve each bucket size per storage type,
# output will be displayed in Mb, if the output is 0Mb, the specific storage size is less than 1Mb.
# output will only display used storage types
# Written by Avi Keinan, 2019.
###
@Burekasim
Burekasim / s3-to-alb-with-lambda.py
Created October 7, 2020 15:01
access s3 objects with ALB using lambda
import os
import json
import boto3
from botocore.exceptions import ClientError
def lambda_handler(event, context):
# use 'AWS_REGION' environment variable from lambda built-in variables
aws_region = os.environ['AWS_REGION']
bucket_name = os.environ['BUCKET_NAME']
@Burekasim
Burekasim / ec2-instances.py
Created September 21, 2020 05:57
List all the ec2 instances from all the regions
import json
import os
import sys
import boto3
from queue import Queue
from threading import Thread
from tabulate import tabulate
class Aws: