Reputation: 74
I'm makeing a simple proxy with Flask to mock the call describe_regions() of AWS.
The Flask server has de following code:
from __future__ import unicode_literals
from flask import Flask
from flask import Response
from flask import stream_with_context
# from httpretty import HTTPretty, register_uri
import httpretty
import requests
from flask import request
import time
RESPONSE = u"""<DescribeRegionsResponse xmlns="http://ec2.amazonaws.com/doc/2015-10-01/">
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
<regionInfo>
<item>
<regionName>us-east-1</regionName>
<regionEndpoint>ec2.us-east-1.amazonaws.com</regionEndpoint>
</item>
<item>
<regionName>eu-west-1</regionName>
<regionEndpoint>ec2.eu-west-1amazonaws.com</regionEndpoint>
</item>
</regionInfo>
</DescribeRegionsResponse>"""
app = Flask(__name__)
@app.route('/<path:url>', methods=['GET', 'PUT', 'POST', 'DELETE', 'HEAD', 'PATCH', 'OPTIONS', 'CONNECT'])
def home(url):
return Response(RESPONSE, mimetype='text/xml')
if __name__ == '__main__':
app.run(debug=True)
Then I has the following code to test it. I use Boto3 to call the API for AWS.
from boto3.session import Session
import os
credentials = {
'aws_access_key_id': 'sadasdasda',
'aws_secret_access_key': 'dasdasdasd'
}
os.environ["HTTP_PROXY"] = 'http://localhost:5000/'
os.environ["HTTPS_PROXY"] = 'http://localhost:5000/'
session_boto3 = Session(**credentials)
ec2 = session_boto3.client('ec2', 'eu-west-1', verify=False)
regions = ec2.describe_regions()
print regions
The problem is: the Flask server get the petition, but the Response doesn't like to Boto3 and I get the following traceback error:
Traceback (most recent call last):
File "/pruebas_mock/prueba.py", line 82, in <module>
regions = ec2.describe_regions()
File "/mock_aws/local/lib/python2.7/site-packages/botocore/client.py", line 228, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/client.py", line 475, in _make_api_call
operation_model, request_dict)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/endpoint.py", line 117, in make_request
return self._send_request(request_dict, operation_model)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/endpoint.py", line 146, in _send_request
success_response, exception):
File "/mock_aws/local/lib/python2.7/site-packages/botocore/endpoint.py", line 219, in _needs_retry
caught_exception=caught_exception)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/hooks.py", line 226, in emit
return self._emit(event_name, kwargs)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/hooks.py", line 209, in _emit
response = handler(**kwargs)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/retryhandler.py", line 183, in __call__
if self._checker(attempts, response, caught_exception):
File "/mock_aws/local/lib/python2.7/site-packages/botocore/retryhandler.py", line 250, in __call__
caught_exception)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/retryhandler.py", line 273, in _should_retry
return self._checker(attempt_number, response, caught_exception)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/retryhandler.py", line 313, in __call__
caught_exception)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/retryhandler.py", line 222, in __call__
return self._check_caught_exception(attempt_number, caught_exception)
File "/mock_aws/local/lib/python2.7/site-packages/botocore/retryhandler.py", line 355, in _check_caught_exception
raise caught_exception
botocore.vendored.requests.exceptions.SSLError: [Errno 1] _ssl.c:510: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
How should I make the response with Flask?
Thanks.
Upvotes: 1
Views: 441
Reputation: 45906
By default, boto3 (and all of the other AWS SDK's) will connect to services using SSL. Your proxy Flask server does not appear to be using SSL so you can either use SSL in your proxy or tell boto3 not to use SSL for your proxy server:
ec2 = session_boto3.client('ec2', 'eu-west-1', use_ssl=False, verify=False)
The verify
parameter tells boto3 not to try to validate the SSL cert but it will still try to connect via SSL. the use_ssl=False
tells it to use plain HTTP to talk to your endpoint.
Upvotes: 1