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') |
View requirements.txt
celery[sqs]==4.4 |