Skip to content

Instantly share code, notes, and snippets.

View toshke's full-sized avatar

Nikola Tosic toshke

  • Buffer Overflow
  • Melbourne, Australia
  • X @thetoske
View GitHub Profile
@toshke
toshke / validate_all.sh
Created February 21, 2020 05:37
Validate all cloudformation templates
#!/usr/bin/env bash
for f in *.yaml; do
printf "Check ${f}..."
aws cloudformation validate-template --template-body "file://${f}" > /dev/null 2>&1
if [ "$?" != "0" ]; then
echo "\n${f} failed, run following for details"
echo " aws cloudformation validate-template --template-body file://${f}\n\n"
exit 1
fi
printf " [OK]\n"
@toshke
toshke / cloudwatch_metrics_batch.py
Created March 3, 2020 03:58
batch put cloudwatch metrics
pushed = 0
namespace = 'my cw space'
while pushed < len(metric_data):
left = pushed
if len(metric_data) - pushed >= 20:
right = left + 20
else:
right = len(metric_data)
pushed += (right - left)
to_push = metric_data[left:right]
@toshke
toshke / aws_friendly_region_names.json
Created April 13, 2020 01:00
Friendly AWS region names JSON
{
"ap-northeast-1": "Asia Pacific (Tokyo)",
"ap-northeast-2": "Asia Pacific (Seoul)",
"ap-southeast-1": "Asia Pacific (Singapore)",
"ap-southeast-2": "Asia Pacific (Sydney)",
"ap-south-1": "Asia Pacific (Mumbai)",
"eu-central-1": "EU (Frankfurt)",
"eu-north-1": "Europe (Stockholm)",
"eu-west-1": "EU (Ireland)",
"eu-west-2": "Europe (London)",
@toshke
toshke / chroot_binary.sh
Created May 6, 2020 06:55
Isolate binary using chroot
function isolate_binary() {
binary=$(which $1)
libs=$(ldd -v $binary | grep -o '/.*[[:space:]]')
libs=($libs)
mkdir -p ${2}$(dirname $binary)
cp -vn ${binary} ${2}${binary}
for l in "${libs[@]}"; do mkdir -p ${2}$(dirname $l) && cp -vn ${l} ${2}${l}; done
}
@toshke
toshke / cr_errors.py
Created November 23, 2020 11:07
Custom Resource error handling
import logging
import json
def handler(payload, context):
try:
response = CustomResourceResponse(payload)
# follow rule 2 - alway log the paylod
logging.info(json.dumps(payload))
## TODO: handle request from CFN
@toshke
toshke / physical_id.py
Last active December 22, 2020 09:15
physical id
def get_physical_id(r_properties):
""" Generated resource id """
bucket = r_properties['Bucket']
key = r_properties['Key']
return f's3://{bucket}/{key}'
def cr_handler(event, context):
"""
Create, Update or Remove S3 object
as Custom Resource for AWS CloudFormation
@toshke
toshke / crupdate.py
Last active December 22, 2020 09:15
crupdate custom resource
physical_id = get_physical_id(event)
if request_type == 'Create' or request_type == 'Update:
if resource_exists(physical_id):
print(f'{physical_id} will be overwritten')
create_resource()
response.success(physical_id)
@toshke
toshke / condition_cr.yaml
Created November 30, 2020 09:48
Conditional Custom Resource
Parameters:
DeployCustomResource:
Type: String
Default: false
AllowedValues: [true, false]
Conditions:
DeployCustomResource: !Equals [ !Ref DeployCustomResource, 'true' ]
Resources:
# .. other definitions including backing lambda
MyCustomResource:
@toshke
toshke / cr_log.py
Created November 30, 2020 09:58
log custom resource payload
import json
def handler(event, payload):
print(json.dumps(event))
@toshke
toshke / cresponse.py
Created December 22, 2020 09:12
cresponse.py
import logging
from urllib.request import urlopen, Request, HTTPError, URLError
import json
logger = logging.getLogger()
class CustomResourceResponse:
def __init__(self, event):
self.event = event
self.response = {