Skip to content

Instantly share code, notes, and snippets.

View seventhskye's full-sized avatar

Seventh Skye Limited seventhskye

View GitHub Profile
@seventhskye
seventhskye / delete_all_objects.py
Last active February 14, 2023 12:08
A script to delete all objects, versions and delete markers from an s3 bucket.
#!/usr/bin/env python
import boto3
client = boto3.client('s3')
Bucket = 'a-bucket'
Prefix = 'a-prefix' # leave blank to delete the entire contents
IsTruncated = True
MaxKeys = 1000
KeyMarker = None
@seventhskye
seventhskye / userdata.d.sh
Last active December 10, 2018 05:02
Userdata script to download and execute a set of bash scripts from Amazon S3. Useful if you wanted to have a predefined set of scripts to share amongst EC2 instance, e.g. userdata.
#!/bin/bash -x
# The bucket containing the userdata.d
export USERDATAD_BUCKET=userdata.example.com
# Required to set the endpoint of the aws-cli
export AWS_DEFAULT_REGION=eu-west-1
# Install Prerequisites
yum install -y aws-cli
# Download all .sh scripts from the bucket and pipe to bash
SCRIPTS=$(aws s3 ls s3://$USERDATAD_BUCKET/userdata.d/ | awk '{ print $4 }')
for S in $SCRIPTS; do
@seventhskye
seventhskye / lights
Created August 1, 2016 10:10
A python script to shutdown amazon instances, and autoscaling groups. Requires a tag called ScheduledUptime=True on resources this script need to apply to.
#!/usr/bin/env python
import sys
import boto3
def main(argv):
ec2 = boto3.resource('ec2')
instances = ec2.instances.all()
elb = boto3.client('elb')
autoscaling = boto3.client('autoscaling')
auto_scaling_groups = autoscaling.describe_auto_scaling_groups()['AutoScalingGroups']
@seventhskye
seventhskye / functions
Created March 16, 2016 09:20
A library of bash functions to use for Amazon Web Services CloudFormation deployments.
#!/bin/bash
# Abort with exit and message
# Used for testing
function abort()
{
echo " *** ERROR $@"
exit 1
}
@seventhskye
seventhskye / aws-cloudwatch-event
Created July 13, 2017 11:42
A CloudWatch event JSON string for testing within lambda functions.
{
"Records": [
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:eu-west-1:000000000000:cloudwatch-alarms:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"Sns": {
"Type": "Notification",
"MessageId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"TopicArn": "arn:aws:sns:eu-west-1:000000000000:cloudwatch-alarms",
#!/bin/sh
### BEGIN INIT INFO
# Provides: dockercompose
# Required-Start: $docker
# Required-Stop: $docker
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Docker Services
### END INIT INFO
@seventhskye
seventhskye / lambda_cron.py
Last active October 6, 2016 13:34
A lambda function to perform cron tasks, this one deletes cloudformation stacks. Fill in REGEX with a regular expression for the CF stack name you wish to delete.
import sys
import boto3
import re
def lambda_handler(event, context):
cloudformation = boto3.client('cloudformation')
stacks = cloudformation.describe_stacks()['Stacks']
for s in stacks:
if re.match(REGEX, s['StackName'], flags=0):
@seventhskye
seventhskye / associate_eip_address.py
Created September 28, 2016 13:23
Script to update an EC2 instance with an elastic IP.
#!/usr/bin/env python
import boto3
import requests
def main(argv):
instance_id = requests.get('http://169.254.169.254/latest/meta-data/instance-id').text
allocation_id = argv[1]
client = boto3.client('ec2')
response = client.associate_address(InstanceId=instance_id,AllocationId=allocation_id)
@seventhskye
seventhskye / s3_object_acl.py
Created September 16, 2016 09:37
A script to iterate through a bucket and change object acl's.
#!/usr/bin/env python
import boto3
client = boto3.client('s3')
bucket = 'your-bucket'
prefix = 'a-path-in-s3'
NextContinuationToken = True
while NextContinuationToken:
if NextContinuationToken == True:
@seventhskye
seventhskye / s3_continuation.py
Created September 15, 2016 11:39
A script to loop through a list of bucket objects that would exceed the max-keys.
#!/usr/bin/env python
import boto3
client = boto3.client('s3')
bucket = 'you-s3-bucket'
prefix = 'a-prefix'
NextContinuationToken = True
while NextContinuationToken:
if NextContinuationToken == True: