Boto3 clients
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed boto3 resources.
https://cloudaffaire.com/boto3-resources/
In this blog post, we will discuss boto3 clients.
What are clients?
Clients provide a low-level interface to AWS whose methods map close to 1:1 with service APIs. All service operations are supported by clients. Clients are generated from a JSON service definition file.
Client components:
- Service Operations: Service operations map to client methods of the same name and provide access to the same operation parameters via keyword arguments.
- Service Responses: Responses are returned as python dictionaries. It is up to you to traverse or otherwise process the response for the data you need, keeping in mind that responses may not always include all of the expected data.
- Service Waiters: Waiters use a client’s service operations to poll the status of an AWS resource and suspend execution until the AWS resource reaches the state that the waiter is polling for or a failure occurs while polling.
Let us explain the different component of a client with a demo.
Prerequisites for this demo:
- One EC2 AWS Linux 2 instance with boto3 installed and configured.
Demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
## Login to your EC2 instance ## Create a file named create_vpc.py vi create_vpc.py --------------------------- #!/usr/bin/env python #creates a vpc and two subnets #import python boto3 import boto3 #variables aws_region = "ap-south-1" aws_az1 = "ap-south-1a" aws_az2 = "ap-south-1b" vpc_cidr = "10.0.0.0/16" subnet1_cidr = "10.0.1.0/24" subnet2_cidr = "10.0.2.0/24" #initialize ec2 client client = boto3.client('ec2',region_name=aws_region) #create a vpc vpc_id = client.create_vpc(CidrBlock=vpc_cidr).get('Vpc').get('VpcId') #wait untill the vpc is available waiter = client.get_waiter('vpc_available') waiter.wait(VpcIds=[vpc_id]) #create two subnets subnet1_id = client.create_subnet(AvailabilityZone=aws_az1,CidrBlock=subnet1_cidr,VpcId=vpc_id).get('Subnet').get('SubnetId') subnet2_id = client.create_subnet(AvailabilityZone=aws_az2,CidrBlock=subnet2_cidr,VpcId=vpc_id).get('Subnet').get('SubnetId') #create tags response1 = client.create_tags(Resources=[vpc_id],Tags=[{'Key': 'Name','Value': 'myvpc'}]) response2 = client.create_tags(Resources=[subnet1_id],Tags=[{'Key': 'Name','Value': 'myvpc_subnet1'}]) response3 = client.create_tags(Resources=[subnet2_id],Tags=[{'Key': 'Name','Value': 'myvpc_subnet2'}]) #print resource id's print('VPC ID: ' + vpc_id) print('SUBNET ONE ID: ' + subnet1_id) print('SUBNET TWO ID: ' + subnet2_id) #cleanup response4 = client.delete_subnet(SubnetId=subnet1_id) response5 = client.delete_subnet(SubnetId=subnet2_id) response6 = client.delete_vpc(VpcId=vpc_id) --------------------------- :wq ## Execute the script python create_vpc.py |
Hope you have enjoyed this article. In the next blog post, we will create a LAMP stack in AWS using Python Boto3.
To get more details on Python Boto3, please refer below AWS documentation
https://boto3.amazonaws.com/v1/documentation/api/latest/index.html