How to delete queue using boto - boto

I need to delete the queue using boto. I not sure about how to delete a queue. I tried this but it didn't work:
queue = conn.create_queue(sqs) #sqs is the queue name
conn.delete_queue(queue)

In boto v2
>>> import boto.sqs
>>> conn=boto.sqs.connect_to_region('ap-southeast-2')
>>> q = conn.create_queue('foo')
>>> q
Queue(https://ap-southeast-2.queue.amazonaws.com/123456789012/foo)
>>> q.__dict__
{'ResponseMetadata': '', 'url': u'https://ap-southeast-2.queue.amazonaws.com/123456789012/foo', 'CreateQueueResponse': '', 'message_class': <class 'boto.sqs.message.Message'>, 'connection': SQSConnection:ap-southeast-2.queue.amazonaws.com, 'RequestId': u'9bfb9b6d-d9b5-5a29-9ea5-d4dbd5e3ef5a', 'CreateQueueResult': '', 'visibility_timeout': None}
>>> conn.delete_queue(q)
True
In boto3
>>> import boto3
>>> client = boto3.client('sqs')
>>> q = client.create_queue(QueueName='foo')
>>> q
{u'QueueUrl': 'https://ap-southeast-2.queue.amazonaws.com/123456789012/foo', 'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': 'f6531b80-4387-57a2-98fa-54364841c158'}}
>>> client.delete_queue(QueueUrl=q['QueueUrl'])
{'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': 'f9d3853c-12f2-598b-ad3c-ca8a1f68754a'}}

First, you have to get the queue --
queue = conn.create_queue('myqueue') // asuming your queue name 'myqueue'
Then you can simply delete the queue --
conn.delete_queue(queue)

In Boto3, use the Queue resource. Here's some code that's part of a larger example on GitHub:
import logging
import boto3
from botocore.exceptions import ClientError
logger = logging.getLogger(__name__)
sqs = boto3.resource('sqs')
def remove_queue(queue):
try:
queue.delete()
logger.info("Deleted queue with URL=%s.", queue.url)
except ClientError as error:
logger.exception("Couldn't delete queue with URL=%s!", queue.url)
raise error
queue = sqs.create_queue(QueueName=name, Attributes=attributes)
remove_queue(queue)

Related

Snowflake Account must be specified error, but it is specified

I have the below code, I have the account, username, pw, etc, but I'm still seeing the below error:
raise error_class( sqlalchemy.exc.ProgrammingError:
(snowflake.connector.errors.ProgrammingError) 251001: Account must be
specified
I've also tried by changing the engine variable in my created_db_engine function like below, but I see the same error:
engine = snowflake.connector.connect(
user='USER',
password='PASSWORD',
account='ACCOUNT',
warehouse='WAREHOUSE',
database='DATABASE',
schema='SCHEMA'
)
here is my code
import pandas as pd
from snowflake.sqlalchemy import URL
from sqlalchemy import create_engine
import snowflake.connector
from snowflake.connector.pandas_tools import write_pandas, pd_writer
from pandas import json_normalize
import requests
df = 'my_dataframe'
def create_db_engine(db_name, schema_name):
engine = URL(
account="ab12345.us-west-2.snowflakecomputing.com",
user="my_user",
password="my_pw",
database="DB",
schema="PUBLIC",
warehouse="WH1",
role="DEV"
)
return engine
def create_table(out_df, table_name, idx=False):
url = create_db_engine(db_name="db", schema_name="skm")
engine = create_engine(url)
connection = engine.connect()
try:
out_df.to_sql(
table_name, connection, if_exists="append", index=idx, method=pd_writer
)
except ConnectionError:
print("Unable to connect to database!")
finally:
connection.close()
engine.dispose()
return True
print(df.head)
create_table(df, "reporting")
Given the Snowflake documentation for SqlAlchemy, your account parameter should not include snowflakecomputing.com.
So you should try with ab12345.us-west-2 and connector will append the domain part automatically for you.

Python: request json gets error - If using all scalar values, you must pass an index

When attempting to request a json file from API, i have an error after get the first result.
Does anyone have any idea why is requested an index from a list ?
Best regards :)
import requests
import json
import pandas as pd
import time
import datetime
### OCs List ids
OCs = ['1003473-1116-SE21','1003473-1128-AG21','1031866-12-CC21','1057440-3184-AG21','1070620-1832-CM21', '1070620-2219-SE21', '1070620-2499-CM21']
for i in OCs:
link ="http://api.mercadopublico.cl/servicios/v1/publico/ordenesdecompra.json?codigo="+ str(i) +"&ticket=F8537A18-6766-4DEF-9E59-426B4FEE2844"
response = requests.get(link, [])
data = response.json()
df = pd.DataFrame.from_dict(data)
### remove unnecessary columns
df.drop(df.columns[[0,1,2]],axis=1, inplace=True)
### flat json to pandas dataframe
df_detail = pd.json_normalize(df['Listado'])
ValueError: If using all scalar values, you must pass an index
The server detects too many requests and sends error response (and then your script throws an error). Solution is to wait for correct response, for example:
import requests
import json
import pandas as pd
import time
import datetime
### OCs List ids
OCs = [
"1003473-1116-SE21",
"1003473-1128-AG21",
"1031866-12-CC21",
"1057440-3184-AG21",
"1070620-1832-CM21",
"1070620-2219-SE21",
"1070620-2499-CM21",
]
for i in OCs:
link = (
"http://api.mercadopublico.cl/servicios/v1/publico/ordenesdecompra.json?codigo="
+ str(i)
+ "&ticket=F8537A18-6766-4DEF-9E59-426B4FEE2844"
)
while True: # <--- repeat until we get correct response
print(link)
response = requests.get(link, [])
data = response.json()
if "Listado" in data:
break
time.sleep(3) # <--- wait 3 seconds and try again
df = pd.DataFrame.from_dict(data)
### remove unnecessary columns
df.drop(df.columns[[0, 1, 2]], axis=1, inplace=True)
### flat json to pandas dataframe
df_detail = pd.json_normalize(df["Listado"])
# ...

AWS Lambda verify requests from Slack

I have the following Python code in AWS Lambda to verify if an event received is indeed from Slack:
import hmac
import json
def verifySignature(header,body):
h = hmac.new(key=os.getenv('sign_secret').encode(), \
msg=f'v0:{header.get("X-Slack-Request-Timestamp")}:{body}'.encode(), \
digestmod="sha256")
result = hmac.compare_digest('v0='+h.hexdigest(),header.get('X-Slack-Signature'))
print('v0='+h.hexdigest(),header.get('X-Slack-Signature'))
return result
def lambda_handler(event, context):
body = json.loads(event.get('body'))
if verifySignature(event.get('headers'),body):
do_something()
Slack's authentication protocol is outlined here. However, I keep getting mismatching signatures (result == False). Does anyone know why?
There is a high chance the issue is coming from the encoding / decoding. There is pip package to verify the slack signature.
But the verification code is simple:
import hashlib
import hmac
def verify_slack_signature(slack_post_request, slack_signing_secret):
slack_signing_secret = bytes(slack_signing_secret, 'utf-8')
slack_signature = slack_post_request['headers']['X-Slack-Signature']
slack_request_timestamp = slack_post_request['headers']['X-Slack-Request-Timestamp']
request_body = slack_post_request["body"]
basestring = f"v0:{slack_request_timestamp}:{request_body}".encode('utf-8')
my_signature = 'v0=' + hmac.new(slack_signing_secret, basestring, hashlib.sha256).hexdigest()
return hmac.compare_digest(my_signature, slack_signature)

"errorMessage": "Decimal('1') is not JSON serializable"

import boto3
import json
import os
s3 = boto3.client('s3')
ddb = boto3.resource('dynamodb')
table = ddb.Table('test_table')
def lambda_handler(event, context):
response = table.scan()
body = json.dumps(response['Items'])
response = s3.put_object(Bucket='s3-testing',
Key = 's3-testing.json' ,
Body=body,
ContentType='application/json')
lambda function
Hi guys I am trying to run this code but I keep receiving this TypeError: Decimal('1') is not JSON serializable. When I play around and import Decimal, it gives me this "errorMessage": "Unable to import module 'lambda_function'"
I am assuming I have to add simplejson but do not know how to go around it.
execution error

How can I get the addresses of all the EC2 instances tied to a LoadBalancer using Boto?

I'd like to get all the instances that are on a LoadBalancer with boto, how can I accomplish that?
This is what I've got so far:
import boto
from boto.regioninfo import RegionInfo
from boto import ec2
ACCESS_KEY_ID = '*****'
SECRET_ACCESS_KEY = '********'
reg = RegionInfo(
name='ap-southeast-1',
endpoint='elasticloadbalancing.ap-southeast-1.amazonaws.com')
conn = boto.connect_elb(
aws_access_key_id=ACCESS_KEY_ID,
aws_secret_access_key=SECRET_ACCESS_KEY,
region=reg)
ec2_connection = boto.ec2.connection.EC2Connection(
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_ACCESS_KEY,
region=reg)
instances = [ instance.id for instance in conn.get_all_load_balancers()[3].instances ]
# instances is now [u'i-62448d36'], so far so good.
ec2_connection.get_all_instances(instances)
Which ends with:
<ErrorResponse xmlns="http://webservices.amazon.com/AWSFault/2005-15-09">
<Error>
<Type>Sender</Type>
<Code>InvalidAction</Code>
<Message>Could not find operation DescribeInstances for version 2012-03-01</Message>
</Error>
<RequestId>c6aab70d-b22b-11e1-a990-a747bbde9f63</RequestId>
</ErrorResponse>
I'm using boto 2.4.1.
This is what I ended up with:
import boto
from boto import regioninfo
from boto import ec2
ACCESS_KEY_ID = '***********'
SECRET_ACCESS_KEY = '***********'
elb_region = boto.regioninfo.RegionInfo(
name='ap-southeast-1',
endpoint='elasticloadbalancing.ap-southeast-1.amazonaws.com')
elb_connection = boto.connect_elb(
aws_access_key_id=ACCESS_KEY_ID,
aws_secret_access_key=SECRET_ACCESS_KEY,
region=elb_region)
ec2_region = ec2.get_region(
aws_access_key_id=ACCESS_KEY_ID,
aws_secret_access_key=SECRET_ACCESS_KEY,
region_name='ap-southeast-1')
ec2_connection = boto.ec2.connection.EC2Connection(
aws_access_key_id=ACCESS_KEY_ID,
aws_secret_access_key=SECRET_ACCESS_KEY,
region=ec2_region)
load_balancer = elb_connection.get_all_load_balancers(load_balancer_names=['MediaPopClients'])[0]
instance_ids = [ instance.id for instance in load_balancer.instances ]
reservations = ec2_connection.get_all_instances(instance_ids)
instance_addresses = [ i.public_dns_name for r in reservations for i in r.instances ]
Gives:
[u'ec2-46-137-194-58.ap-southeast-1.compute.amazonaws.com']
I think something like this should work:
>>> import boto
>>> elb = boto.connect_elb()
>>> load_balancer = elb.get_all_load_balancers(['my_lb_name'])[0]
>>> for instance_info in load_balancer.instances:
... print instance_info.id
The instances attribute of the LoadBalancer object is a list of InstanceInfo objects. These are not actual Instance object but it does have the instance-id so you can then look up the Instance object associated with it.