Recently, there was a blog entry posted on the AWS Developer Blog discussing how to migrate to boto3. Since HPE Helion Eucalyptus strives to provide 100% AWS-compatible APIs for implemented services, AWS SDKs – such as the AWS SDK for Python – works solidly. This blog entry will demonstrate how to use boto3 – the latest version of AWS SDK for Python – with HPE Helion Eucalyptus 4.2.
At the time of the posting of this blog entry, the following AWS service APIs are supported by HPE Helion Eucalyptus 4.2:
As mentioned on the boto3 documentation, install boto3 using pip:
# pip install boto3
Again, as mentioned in the boto3 documentation, configuration can be done by using AWS CLI, or manually creating the config and credentials file under the .aws directory. For example, here are the contents of the .aws/config and .aws/credentials files that will be used for this demonstration:
# cat .aws/config [profile devops-admin] output = json region = us-east-1 # cat .aws/credentials [devops-admin] aws_access_key_id = XXXXXXXXXXXXXXXXXXXX aws_secret_access_key = XXXXXXXXXXXXXXXXXXXXXXXX
If these files do not want to be used, as an alternative, you can pass the AWS Access Key ID and AWS Secret Key programmatically. This will be referenced later on in this blog entry.
To demonstrate how to use boto3, ipython will be utilized. To get started, the Session class will be imported from the boto3 library:
# ipython Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36) Type "copyright", "credits" or "license" for more information. IPython 0.13.2 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In : from boto3.session import Session
Next invoke the session:
In : session = Session(region_name='us-east-1', profile_name="devops-admin")
As mentioned earlier, alternatively, if we want to programmatically pass the AWS Access Key ID and the AWS Secret Key, it can be done when the session is invoked:
In : session = Session(aws_access_key_id='XXXXXXXXXXXXXX', aws_secret_access_key='XXXXXXXXXXXXXXXXXXXXXXXX', region_name='us-east-1')
Even though region_name has a value here, when the client connection is created, the service endpoint will be a HPE Helion Eucalyptus service endpoint. Any valid AWS region name can be used with HPE Helion Eucalyptus. The important piece will be the endpoint URL.
From here, we can use the session to establish a client connection with a given HPE Helion Eucalyptus service endpoint. Since the HPE Helion Eucalyptus cloud used in this example contains HTTPS endpoints, the trusted root certificate for the cloud subdomain will be passed as well.
Here is an example connecting to the EC2 service endpoint provided by the HPE Helion Eucalyptus Compute service to discover what instances as associated with the authenticated user account:
In : client = session.client('ec2', endpoint_url='https://ec2.c-05.autoqa.qa1.eucalyptus-systems.com/', verify='/root/euca-ca-0.crt')
In : for reservation in client.describe_instances()['Reservations']: for instance in reservation['Instances']: print instance['InstanceId'] ...: i-4064f4e7 i-1c8515dd i-79e96bc1 i-d43f50f1 i-b4adc06b i-c4025e42
Below is another example connecting to the S3 service endpoint provided by the HPE Helion Eucalyptus Object Storage Gateway (OSG) service to list the buckets owned by the authenticated user account:
In : client = session.client('s3', endpoint_url='https://s3.c-05.autoqa.qa1.eucalyptus-systems.com/', verify='/root/euca-ca-0.crt')
In : for bucket in client.list_buckets()['Buckets']: print bucket['Name'] ...: cfn-templates ubuntu-trusty-x86_64-hvm-20151218 ubuntu-xenial-x86_64-hvm-20151217
Another example connecting to the Cloudformation service endpoint provided by the HPE Helion Eucalyptus Cloudformation service:
In : client = session.client('cloudformation', endpoint_url='https://cloudformation.c-05.autoqa.qa1.eucalyptus-systems.com/', verify='/root/euca-ca-0.crt')
In : for stack in client.describe_stacks()['Stacks']: print "Stack Name: " + stack['StackName'] print "Status: " + stack['StackStatus'] print "ID: " + stack['StackId'] ...: Stack Name: CoreOSCluster Status: CREATE_COMPLETE ID: arn:aws:cloudformation::001520216600:stack/CoreOSCluster/12437fe7-8a03-4920-9e34-270764450fa0
And for the last example, connecting to the AutoScaling service endpoint provided by the HPE Helion Eucalyptus AutoScaling service:
In : client = session.client('autoscaling', endpoint_url='https://autoscaling.c-05.autoqa.qa1.eucalyptus-systems.com/', verify='/root/euca-ca-0.crt')
In : for asg in client.describe_auto_scaling_groups()['AutoScalingGroups']: print "AutoScaling Group Name: " + asg['AutoScalingGroupName'] print "Launch Config: " + asg['LaunchConfigurationName'] print "Availability Zones:" for az in asg['AvailabilityZones']: print "\t" + az print "AutoScaling Group Instances:" for instance in asg['Instances']: print "\t" + instance['InstanceId'] ....: AutoScaling Group Name: CoreOSCluster-CoreOsGroup-JTKMRINKKMYDI Launch Config: CoreOSCluster-CoreOsLaunchConfig-LAWHOT5X5K5PX Availability Zones: us-east-1c us-east-1b us-east-1a AutoScaling Group Instances: i-79e96bc1 i-4064f4e7 i-c4025e42 i-d43f50f1 i-1c8515dd
As mentioned earlier, boto3 can be used with any AWS compatible service implemented by HPE Helion Eucalyptus. If your team isn’t ready to use boto3 yet, boto can still be used with HPE Helion Eucalyptus.
As always, I hope you enjoyed this entry. Please let me know if there are any questions/suggestion/ideas regarding this blog topic.