Skip to content

Instantly share code, notes, and snippets.

View polomarcus's full-sized avatar
🦕
Go vegan

Paul Leclercq polomarcus

🦕
Go vegan
View GitHub Profile
@polomarcus
polomarcus / ssm_command.py
Created January 10, 2020 14:43
Send bash command to our ASG's instances
def ssm_command(instances):
ssm.send_command(
InstanceIds=instances,
DocumentName='AWS-RunShellScript',
Parameters= { 'commands': ['sudo systemctl stop service'] }
)
@polomarcus
polomarcus / lambda_handler.py
Created January 10, 2020 14:40
Graceful shutdown using Simple Sytems Manager and Terraform on AWS
def lambda_handler(event, context):
for record in event['Records']:
parsed_event = (json.loads(record["body"]))
msg_receipt_handle = str(record['receiptHandle'])
if parsed_event.get("LifecycleTransition") == "autoscaling:EC2_INSTANCE_TERMINATING" and parsed_event.get("LifecycleHookName") == "my-service":
auto_scaling_group = get_env_variable('ASG_NAME').split()[0]
ec2_instance_id = parsed_event.get("EC2InstanceId")
delete_sqs_message(msg_receipt_handle) # delete first to avoid infinite loop on unvalid instance ID
ssm_command([ec2_instance_id]) # must be an array
@polomarcus
polomarcus / popularity_percentage.sql
Created March 2, 2017 17:00
SQL - Percentage and GROUP BY
SELECT ROUND( (COUNT(t.*) / subTotal.total_radio * 100),2) AS percentage_of_songs,
subTotal.total_radio,
ROUND(popularity / 10) * 10 AS popularity,
t.radio
FROM AudioFeatureArtistTrackRadios t
JOIN (
SELECT COUNT(*) AS total_radio, radio
FROM AudioFeatureArtistTrackRadios
GROUP BY radio
) AS subTotal
@polomarcus
polomarcus / Song.scala
Created March 2, 2017 16:00
Song Class
Song(timestamp:Int, humanDate:Long, year:Int, month:Int, day:Int, hour:Int, minute: Int, artist:String, allArtists: String, title:String)