1

I'm using Windows 10 OS.

I want to count the number of IP Address of AWS.

I use python 2.7.14 and boto 2.6.0

I add a file which name is boto.config locate C:\Users\Administrator folder

The content of the boto.config is:

[Credentials]

aws_access_key_id=******

aws_secret_access_key=*****

The script is :

#!/usr/bin/env python

# -*- encoding: utf8 -*-

import boto.ec2

from pprint import pprint

import ssh

import requests

import urllib3

import certifi

import ssl

conn = boto.ec2.connect_to_region('cn-north-1')

reservations = conn.get_all_instances()

InstanceMap=[]

for reservation in reservations:

    for instance in reservation.instances:

        if 'env' in instance.tags and instance.tags['env'] == 'test':

            InstanceMap.append(instance.ip_address)

f = open('F:\ip.txt','w')

pprint(InstanceMap, f)

When I run this script, it show the error formation:

SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)

What's the method can I solve this problem ?

Mohamed Thasin ah
  • 10,754
  • 11
  • 52
  • 111
Andrew
  • 602
  • 10
  • 23
  • Possible duplicate of [Boto \[SSL: CERTIFICATE\_VERIFY\_FAILED\] certificate verify failed while connecting to S3](https://stackoverflow.com/questions/28115250/boto-ssl-certificate-verify-failed-certificate-verify-failed-while-connecting) – Jorge Leitao Mar 03 '18 at 08:44
  • https://stackoverflow.com/questions/28115250/boto-ssl-certificate-verify-failed-certificate-verify-failed-while-connecting . My question is different. – Andrew Mar 03 '18 at 10:23

2 Answers2

5

I was having same issue with boto3 and Python 3.7 on Windows 10 machine. As it turned out, since I was using corporate device with Proxy installed, *.amazonaws.com certificate was getting replaced by the Proxy certificate. This Proxy certificate chain needed to be trusted by Python certifi module. Whether or not, you have a proxy, below method should resolve SSL: CERTIFICATE_VERIFY_FAILED error.

Here is what I did, to resolve the issue -

  1. Find the path where cacert.pem is located -

Install certifi, if you don't have. Command: pip install certifi

import certifi
certifi.where()
C:\\Users\\[UserID]\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\certifi\\cacert.pem
  1. Set AWS_CA_BUNDLE environment variable to the cacert.pem path -

    AWS_CA_BUNDLE=C:\Users\[UserID]\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\certifi\cacert.pem

  2. Download the chain of certificates from amazonaws.com URL. For example: Go to https://sts.amazonaws.com/xyz on a browser and export Root, all the intermediate certificates, domain cert and save as base64 encoded .cer file. Open the certificates in notepad, copy all the contents.

  3. Now open the cacert.pem in a notepad and just add every downloaded certificate contents (---Begin Certificate--- *** ---End Certificate---) at the end.

Restart the command line prompt or PowerShell, SSL verification error should be resolved.

Do not use is_secure = False in your organization's envrionments. This is essentially disabling SSL verification.

Indranil
  • 1,776
  • 1
  • 17
  • 22
0

Try adding is_secure = False like below, in order to skip ssl verification,

conn = boto.ec2.connect_to_region('cn-north-1',is_secure=False)

Try providing the credentials as so, that way you would know if the keys in boto config are old if this works, and if this returns the same issue then you need to check your api-key and secret on aws.

API_KEY = 'Actual API_KEY'
API_SECRET = 'Actual Secret'
conn = boto.ec2.connect_to_region('us-east-2',aws_access_key_id=API_KEY,aws_secret_access_key=API_SECRET,is_secure=False)
Omi Harjani
  • 737
  • 1
  • 8
  • 20
  • When I adding "is_secure = False", the error show : conn = boto.ec2.connect_to_region('cn-north-1',is_secure=False) File "C:\Python27\lib\site-packages\boto-2.6.0- EC2ResponseError: EC2ResponseError: 401 Unauthorized AuthFailureAWS was not able to validate the provided access credentials**************************** – Andrew Mar 03 '18 at 08:23
  • Hi @Andrew I have edited the answer to help you validate your keys. hope it helps – Omi Harjani Mar 03 '18 at 08:57
  • Omi Harjani, thanks very much, this returns the same issue . I will check my api-key – Andrew Mar 03 '18 at 09:31
  • Omi Harjani, I check my api-key and secret . It's right . Does this need to allow my access_key_id and secret_id corresponding users to give the appropriate permissions? At present, this account is the authority of the administrator . – Andrew Mar 03 '18 at 09:49
  • Maybe this is the reason: https://stackoverflow.com/questions/30648236/ec2responseerror-401-unauthorized-using-saltstack-boto-vpc-module – Andrew Mar 03 '18 at 10:11
  • @Andrew Make sure the time on your machine is set correctly. People have faced the same issue when the time on the server and your local machine are not in sync. – Omi Harjani Mar 03 '18 at 10:24
  • @Andrew Yup if you switch your api key and secret that could be the reason too :) – Omi Harjani Mar 03 '18 at 10:32
  • Omi Harjani, The time zone of My EC2 instance is UTC. The python script is run in the bastion server(it is EC2 instance too) which can ssh Login these ec2 instance. The time zone of bastion server is UTC . My script is want to get ec2 ip information from the url https://console.amazonaws.cn/ec2 , the Availability Zone is cn-north-1, the time zone is UTC+8 . How can I set the time in which server can solve the problem: EC2ResponseError: 401 Unauthorized – Andrew Mar 03 '18 at 13:51
  • 1
    I solve this problem. The reason of this problem : EC2ResponseError: EC2ResponseError: 401 Unauthorized . It's the role of IAM has not Sufficient authority. I change another IAM role ,it's OK now. – Andrew Mar 04 '18 at 02:52
  • According to this article : https://github.com/boto/boto/issues/2885 , I degrade the boto from 2.6.0 to 2.32.1. It' s OK – Andrew Mar 04 '18 at 05:40
  • Omi Harjani , thank you very much. Your advice helped me a lot – Andrew Mar 04 '18 at 05:42
  • @Andrew I was using boto v 2.48.0 on which the same code worked like a breeze. I guess that's why I just couldn't replicate the same error on my local. Great that you finally solved it :) – Omi Harjani Mar 05 '18 at 03:30