Skip to content

Instantly share code, notes, and snippets.

View alastairhm's full-sized avatar

Alastair Montgomery alastairhm

View GitHub Profile
@alastairhm
alastairhm / glacier_restore.sh
Created September 7, 2018 13:47
Restore S3 objects which have been moved to glacier storage.
export MYBUCKET=mybucket
export MYDIR=mydir/1/
aws s3 ls s3://$MYBUCKET/$MYDIR/ | awk '{if ($4) print $4}' > myFiles.txt
for x in `cat myFiles.txt`
do
echo "restoring $x"
aws s3api restore-object \
--bucket $MYBUCKET \
--key "${MYDIR}/$x" \
--restore-request '{"Days":7}'
@alastairhm
alastairhm / ssm_test.py
Created November 20, 2018 14:17
Get SSM parameter with boto3
import os
import sys
import json
import boto3
import logging
from botocore.vendored import requests
from botocore.exceptions import ClientError
ssm = boto3.client('ssm')
parameter = ssm.get_parameter(Name='/jenkins/jenkins_api_user', WithDecryption=True)
@alastairhm
alastairhm / latest_ami_id.sh
Last active November 22, 2018 09:41
Return most recent AMI ID
aws ec2 describe-images \
--owners 'amazon' \
--filters 'Name=description,Values=Amazon Linux AMI*' \
--query 'sort_by(Images, &CreationDate)[-1].[ImageId]' \
--output 'text'
@alastairhm
alastairhm / vpc_tags.py
Last active March 6, 2019 10:52
Get AWS VPC tags using Boto3, for use as an external Terraform data source
import json
import boto3
import sys
data = json.load(sys.stdin)
ec2 = boto3.resource('ec2', region_name=data["region"])
client = boto3.client('ec2', region_name=data["region"])
filters = [{'Name':'tag:Name', 'Values':[data["vpc_name"]]}]
vpcs = list(ec2.vpcs.filter(Filters=filters))
@alastairhm
alastairhm / vpc_tags_role.py
Created March 6, 2019 15:56
Get AWS VPC tags external data source when you need to assume a role.
import json
import boto3
import sys
input_json = sys.stdin.read()
try:
data = json.loads(input_json)
except ValueError as value_error:
sys.exit(value_error)
@alastairhm
alastairhm / bumpme
Created August 23, 2019 14:12
bumpme
0
@alastairhm
alastairhm / accounts.py
Created March 6, 2020 16:37
Return members' AccountID and Email of AWS Organization account
import boto3
client = boto3.client('organizations')
response = client.list_accounts(
)
if "Accounts" in response:
for account in response['Accounts']:
print("\"%s\",\"%s\"" %(account['Id'],account['Email']))
@alastairhm
alastairhm / sg_limits.py
Created June 11, 2020 07:45
AWS Boto3 get Security Group Rule Limit
import boto3
def get_limit_value(service_code, quota_name):
client = boto3.client('service-quotas')
paginator = client.get_paginator('list_service_quotas')
page_iterator = paginator.paginate(ServiceCode=service_code)
for page in page_iterator:
for quota in page['Quotas']:
if quota['QuotaName'] == quota_name:
@alastairhm
alastairhm / freebsd.txt
Created October 19, 2020 16:03
FreeBSD install XFCE on Virtual Box
pkg update
pkg install xorg
echo 'kern.vty=vt' >> /boot/loader.conf
pkg install virtualbox-ose-additions
echo 'vboxguest_enable="YES"' >> /etc/rc.conf
echo 'vboxservice_enable="YES"' >> /etc/rc.conf
echo 'dbus_enable="YES"' >> /etc/rc.conf
service vboxguest start
service vboxservice start
service dbus start
@alastairhm
alastairhm / thumbprint.py
Created December 2, 2020 13:24
Python Class to get CA thumbprint from the root certificate, useful for AWS OIDC EKS cluters providers.
import socket
import certifi
from OpenSSL import SSL
from eks_oidc.logger import Logger
logger = Logger(__name__).get_logger()
class ThumbNail: