This is another simple example that help to access aws api using python and boto3. The python is most popular scripting language.I will use python flask micro rest framework to access amazon api.

The amazon provides different api packages based on programming languages.I am using boto3 libs which is based on python3 and provide interface to communicate with aws api.

we will use python 3+, flask micro-framework and boto3 libs. We will manage environment variable using python-dotenv package.

How to Consume Amazon API Using Python

We will create API that return availability zones using boto3.I am assuming you have created sample python flask app, if not please create app using my previous article Consuming a RESTful API with Python and Flask.

Create .env file Into Python

We will install boto3 and python-dotenv using below pip command.

Install python-dotenv Using pip

pip install boto3
pip install python-dotenv

The pip command is help to install package as like npm, composer etc.

Let’s create .env file into root of the application added below configuration params-

Added AWS aws_access_key_id and aws_secret_access_key


Above configuration parameters will found into the aws account.

Route Entry Into Python App

We will create route entry into the main file.

api.add_resource(getAvailabilityZones, "/get_availability_zones")

Create HTTP Get service Call using AWS HTTP Client

We will create HTTP client to get data from aws api, Now we will create service request to get data from aws server.We will create file and added below code.

import boto3
from botocore.config import Config
import botocore
from botocore.exceptions import ClientError, BotoCoreError
import logging
import os
class AWSService:
def getAvailabilityZones():
ec2 = boto3.client('ec2', aws_access_key_id=os.getenv('aws_access_key_id'),aws_secret_access_key=os.getenv('aws_secret_access_key'), verify=False, api_version ='2016-11-15')
response = ec2.describe_availability_zones()
return Helper.jsonSuccess(results=response)
except (ValueError, ClientError, BotoCoreError) as e:
return Helper.jsonError(message=str(e))
return Helper.jsonError(message=str(e))

Genislab Technologies

NexGeneration complete end-2-end software testing & modern development operations tooling & solutions

Do you want to discuss your testing requirements with us? please don’t hesitate to hit the contact us button below, and we will get back to you at our earliest..