Skip to content

Instantly share code, notes, and snippets.

Avatar

Rohit Singh geekrohit

  • Mumbai
View GitHub Profile
@geekrohit
geekrohit / ec2resize.py
Created Dec 23, 2020
Script for resizing an EC2 instance, and restarting it. Prints the new IP at the end.
View ec2resize.py
#!/usr/bin/env python3
import boto3
import argparse
from time import sleep
def getInstanceIP(id):
resp = client.describe_instances(InstanceIds=[args.ec2])
try:
return resp['Reservations'][0]['Instances'][0]['PublicIpAddress']
except:
View test_job.py
import tasks
from time import sleep
print("add 3+5")
ret = tasks.add.delay(3,5)
print("Task ID:")
print(ret)
sleep(10) #give SQS and your worker time enough to execute, so that you can check status on next line
print(ret.status) #this should say SUCCESS
View user-data.sh
#!/bin/bash
export AWS_ACCESS_KEY_ID=""
export AWS_SECRET_ACCESS_KEY=""
export AWS_REGION=""
export SQS_URL=""
apt-get update -qq
apt-get -qq -y install python3 git
curl -fsSL https://get.docker.com | bash -
curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
View docker-compose.yml
version: '2.0'
services:
worker:
image: python:3.8-alpine
command: ash -c "apk add --no-cache build-base libcurl curl-dev; pip3 install -r requirements.txt; celery -A tasks worker --loglevel=info"
volumes:
- "./app:/app"
working_dir: /app
restart: on-failure
environment:
View tasks.py
from celery import Celery
app = Celery('tasks')
app.config_from_object('celeryconfig')
@app.task
def add(x, y):
#we're taking the example task you'd see first in celery docs.
return x + y
View celeryconfig.py
import os
#env vars used:
#AWS_REGION: specify which region you want to access SQS from.
#SQS_URL: The url of the sqs queue to listen for. The AWS keys are picked up
# from the environment vars that we'll add later while running the worker.
CELERY_BROKER_TRANSPORT_OPTIONS = {
'region': os.getenv('AWS_REGION','us-east-1'),
'predefined_queues': {
'default': {
'url': os.getenv('SQS_URL')
You can’t perform that action at this time.