Proper version of boto for Eucalyptus cloud - python

I'm writing some code to interact with an HP Helion Eucalyptus 4.2 cloud server.
At the moment I'm using boto 2.38.0, but I discovered that also exists
the boto3 version.
Which version should I use in order to keep the code up with the times?
I mean, It seems that boto3's proposal is a ground-up rewrite more focused
on the "official" Amazon Web Services (AWS).

You can easilly use boto3. Here is the article clearly explaining that.
I tried it myself with Eucalyptus 4.2.1. So I defined the config and credentials file.
$ cat .aws/config
[profile admin]
output = json
region = region1
$ cat .aws/credentials
[admin]
aws_access_key_id = XXXXXXXXXXXXXXXXXXXX
aws_secret_access_key = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Then I used the interactive Ipython shell, so I listed my running instances.
In [1]: from boto3.session import Session
In [2]: session = Session(region_name='region1', profile_name="admin")
In [3]: client = session.client('ec2', endpoint_url='http://compute.region1.cloud.mydomain.net:8000/')
In [4]: for reservation in client.describe_instances()['Reservations']:
...: for instance in reservation['Instances']:
...: print instance['InstanceId']
...:
i-c706792f
i-af613099
i-fc0c55ee
i-4f493764
i-943d0ce3
I don't use verify argument in session.client(), because my testing EC2 endpoint doesn't use HTTPS but plain HTTP.

2.38 is the right version. boto3 is something totally different and I don't have experience with it.

Related

boto3 eks client how to generate presigned url

I'm trying to update a docker image within a deployment in EKS. I'm running a python code from a lambda function. However, I don't know how to use generate_presigned_url(). What should I pass as ClientMethod parameter???
import boto3
client = boto3.client("eks")
url = client.generate_presigned_url()
These are the clientMethods that you could perform in case of EKS.
'associate_encryption_config'
'associate_identity_provider_config'
'can_paginate'
'create_addon'
'create_cluster'
'create_fargate_profile'
'create_nodegroup'
'delete_addon'
'delete_cluster'
'delete_fargate_profile'
'delete_nodegroup'
'describe_addon'
'describe_addon_versions'
'describe_cluster'
'describe_fargate_profile'
'describe_identity_provider_config'
'describe_nodegroup'
'describe_update'
'disassociate_identity_provider_config'
'generate_presigned_url'
'get_paginator'
'get_waiter'
'list_addons'
'list_clusters'
'list_fargate_profiles'
'list_identity_provider_configs'
'list_nodegroups'
'list_tags_for_resource'
'list_updates'
'tag_resource'
'untag_resource'
'update_addon'
'update_cluster_config'
'update_cluster_version'
'update_nodegroup_config'
'update_nodegroup_version'
You can get more information about these method in the documentation here: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/eks.html#client
After over two weeks I suppose you've found your answer, anyway the ClientMethod mentioned (and, not really well explained on the boto3 docs) is just one of the methods you can use with the EKS client itself. I honestly think this is what KnowledgeGainer was trying to say by listing all the methods, basically you can just pick one. This would give you the presigned URL.
For example, here I'm using one method that isn't requiring any additional arguments, list_clusters:
>>> import boto3
>>> client = boto3.client("eks")
>>> client.generate_presigned_url("list_clusters")
'https://eks.eu-west-1.amazonaws.com/clusters?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAQKOXLHHBFT756PNG%2F20210528%2Feu-west-1%2Feks%2Faws4_request&X-Amz-Date=20210528T014603Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=d25dNCC17013ad9bc75c04b6e067105c23199c23cbadbbbeForExample'
If the method requires any additional arguments, you add those into Params as a dictionary:
>>> method_params = {'name': <your_cluster_name>}
>>> client.generate_presigned_url('describe_cluster', Params=method_params)

get the Alarm object of CloudWatch using boto 2

I created an alarm and want to delete it afterward...
The documentation for boto 2 doesn't show how to do that.
Any help ?
Thanks
If you want to delete alarms, the API you need is DeleteAlarms. The link you have in your question is mentioning it (search for delete_alarms).
Also, boto 3 is the recommended version to use and here is the API you need: https://boto3.readthedocs.io/en/latest/reference/services/cloudwatch.html#CloudWatch.Client.delete_alarms
Example of how to do it with Boto 3:
import boto3
client = boto3.client('cloudwatch')
client.delete_alarms(AlarmNames=['SomeAlarmName'])
Boto 2 example:
import boto
client = boto.connect_cloudwatch()
client.delete_alarms('SomeAlarmName')
If you don't know the name, you can get a list of alarms with (the same for boto 2 and 3):
client.describe_alarms()
You should use Boto3. But if you are tied to Boto2, then:
import boto
cw = boto.connect_cloudwatch()
alarms= cw.describe_alarms()
for alarm in alarms:
print alarm.name
Check if the alarm you want to delete is listed. Then use that name:
cw.delete_alarms([<alarm_to_be_deleted>])

Who created an Amazon EC2 instance using Boto and Python?

I want to know who created a particular instance. I am using Cloud Trail to find out the statistics, but I am not able to get a particular statistics of who created that instance. I am using Python and Boto3 for finding out the details.
I am using this code- Lookup events() from Cloud trail in boto3, to extract the information about an instance.
ct_conn = sess.client(service_name='cloudtrail',region_name='us-east-1')
events=ct_conn.lookup_events()
I found out the solution to the above problem using lookup_events() function.
ct_conn = boto3.client(service_name='cloudtrail',region_name='us-east-1')
events_dict= ct_conn.lookup_events(LookupAttributes=[{'AttributeKey':'ResourceName', 'AttributeValue':'i-xxxxxx'}])
for data in events_dict['Events']:
json_file= json.loads(data['CloudTrailEvent'])
print json_file['userIdentity']['userName']
#Karthik - Here is the sample of creating session
import boto3
import json
import os
session = boto3.Session(region_name='us-east-1',aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'],aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'])
ct_conn = session.client(service_name='cloudtrail',region_name='us-east-1')
events_dict= ct_conn.lookup_events(LookupAttributes=[{'AttributeKey':'ResourceName', 'AttributeValue':'i-xxx'}])
for data in events_dict['Events']:
json_file= json.loads(data['CloudTrailEvent'])
print (json_file['userIdentity']['userName'])

Python Net-SNMP, Linux: gather usernames by IP

I would like to define usernames (or uids) of users currently logged into the remote hosts with static IP addresses. Of course there's a lot of ways to work it out (for instance - using PKI and sshd), but i prefer SNMP service.
I have seen some examples of this problem, something like this (from here, for ArubaOS):
snmpbulkwalk -v 2c -c secure 10.1.30.9 .1.3.6.1.4.1.14823.2.2.1.4.1.2.1.3
I am trying to implement this approach on Python and getting an error:
In [33]: import netsnmp
In [34]: oid = netsnmp.Varbind("nUserName")
In [35]: result = netsnmp.snmpget(oid, Version = 2, DestHost="localhost", Community="public")
error: get: unknown object ID (nUserName)
What am I doing wrong?
Using RHEL 6.4, Python 2.6.
Thank you.
That SNMP variable is enterprise-specific (.1.3.6.1.4.1) and specifically for an Aruba SNMP agent (hence the OID 14823).
To do this for your particular scenario you'd need your Linux SNMP agent to return the same type of data, and you should inspect the corresponding MIB.

Create and download an AWS ec2 keypair using python boto

I'm having difficulty figuring out a way (if possible) to create a new AWS keypair with the Python Boto library and then download that keypair.
The Key object returned by the create_keypair method in boto has a "save" method. So, basically you can do something like this:
>>> import boto
>>> ec2 = boto.connect_ec2()
>>> key = ec2.create_key_pair('mynewkey')
>>> key.save('/path/to/keypair/dir')
If you want a more detailed example, check out https://github.com/garnaat/paws/blob/master/ec2_launch_instance.py.
Does that help? If not, provide some specifics about the problems you are encountering.
Same for Boto3:
ec2 = boto3.resource('ec2')
keypair_name = 'my_key'
new_keypair = ec2.create_key_pair(KeyName=keypair_name)
with open('./my_key.pem', 'w') as file:
file.write(new_keypair.key_material)
print(new_keypair.key_fingerprint)

Categories

Resources