Create and download an AWS ec2 keypair using python boto - python

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)

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>])

How can I use versioning in S3 with boto3?

I am using a versioned S3 bucket, with boto3. How can I retrieve all versions for a given key (or even all versions for all keys) ? I can do this:
for os in b.objects.filter(Prefix=pref):
print("os.key")
but that gives me only the most recent version for each key.
import boto3
bucket = 'bucket name'
key = 'key'
s3 = boto3.resource('s3')
versions = s3.Bucket(bucket).object_versions.filter(Prefix=key)
for version in versions:
obj = version.get()
print(obj.get('VersionId'), obj.get('ContentLength'), obj.get('LastModified'))
I can't take credit as I had the same question but I found this here
boto3 s3 client has a list_object_versions method.
resp = client.list_object_versions(Prefix=prefix, Bucket=bucket)
for obj in [*resp['Versions'], *resp.get('DeleteMarkers', [])]:
print(f"Key: {obj['Key']}")
print(f"VersionId: {obj['VersionId']}")
print(f"LastModified: {obj['LastModified']}")
print(f"IsLatest: {obj['IsLatest']}")
print(f"Size: {obj.get('Size', 0)/1e6}")
supposing you wanted to delete all but the current version, you could do so by adding the objects where not IsLatest to a to_delete list, then running the following:
for obj in to_delete:
print(client.delete_object(Bucket=bucket, Key=obj['Key'], VersionId=obj['VersionId']))
The existing answers describe how to get the version ids of the objects. We're interested in using versions of the object(s) this usually involves accessing part, or all of the objects.
First get the version ids. I'll use al76's answer here for getting version ids to illustrate both boto3.resource and boto3.client examples.
import boto3
from collections import deque
bucket = 'bucket name'
key = 'key'
s3 = boto3.resource('s3')
versions = s3.Bucket(bucket).object_versions.filter(Prefix=key)
s3_object_versions = deque()
for version in versions:
obj = version.get()
s3_object_versions.appendleft(obj.get('VersionId'))
Next we need to get the actual versioned objects, there are a couple ways to do this, depending on whether you have a s3 resource or the s3 client. Given that we got the version ids with the resource we'll start there.
# using the first version of the object
obj_version = s3.ObjectVersion(bucket, key, s3_object_versions[0]).get()
# if this object is really large this loop might execute for awhile
for event in obj_version['Body']:
print(event)
Note that each event is a botocore.response.StreamingBody object and may not correspond to a row if your object is a csv file (etc).
Now as an alternate approach, you might have an s3 client rather than an s3 resource object. In the s3 resource you can access the client via .meta. Here is code:
response = s3.meta.client.get_object(Bucket=bucket, Key=key, VersionId=s3_object_versions[-1])
# same caveat as above, for loop may run for awhile if object is large
for event in response['Body']:
print(event)
A third option which is very similar to the first one, directly using the resource, bucket and object.
object = s3.Bucket(bucket).Object(key).get()
object_version = object.Version(s3_object_versions[0]).get()
# again careful if object is large-will run awhile and fill stdout
for event in object_version['Body']:
print(event)
A fourth option-also using the s3 client is to download the file directly.
object_original_version = s3.meta.client.download_file(bucket, key, '/tmp/' + key, ExtraArgs={'VersionId': s3_object_versions[0]})
Then you can read the file from /tmp via open() call or something more specialized for the file format.
In your code you'll likely want to wrap these with try/except etc. to handle errors unless this is prototyping/experimental work.

Proper version of boto for Eucalyptus cloud

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.

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'])

Categories

Resources