未加星标

Utilizing Python3 to manage AWS

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二04 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

So, here we are. Deep into cloud architecture, getting our feet wet in application deployment/distributed systems, and we find ourselves doing a ton of manual tasks. These manual tasks could be point/click, or they could be simply re-running the same aws cli commands over and over again. We're going to use another way to hit the AWS API, and that's with python.

PLEASE NOTE : The code is pasted at the bottom. Feel free to copy/paste it while we're going over it. I have screenshots for a visual as well.

Python has an amazing module (one of it's best in my opinion) called Boto3. Boto3 is a library/module to AWS's API. Similar to AWS CLI, all you need is a secret key/access key.

For this blog, we're going to list EC2 instances.

You will need the following to follow along:

PyCharm for the IDE An internet connection An AWS account that has access to EC2 (please don't experiment in your prod environment). A coffee, or a beer Prior knowledge of functions, if statements, booleans, and variables in Python. If you know them in another language, you should still be able to follow along.

The first thing we will do is spin up an EC2 instance, as shown below.


Utilizing Python3 to manage AWS

This can be any AMI/Distro. That won't matter in this read.

Next, we're going to create an IAM role. This IAM role for PURELY testing purpose, will have access to admin.


Utilizing Python3 to manage AWS
Utilizing Python3 to manage AWS
Utilizing Python3 to manage AWS

After your IAM role is configured, let's open up PyCharm.

You'll want to create a new project by going to File > New Project > Name it whatever you'd like > pick a location to store your code.


Utilizing Python3 to manage AWS

Next, go ahead and go to File > New File > Python > AWSDemo.py (feel free to name it whatever you'd like. Just ensure to put the .py at the end of the name).


Utilizing Python3 to manage AWS

Once the file is open, go to File > Default Settings > Your project interpreter > Click the plus (+) sign at the bottom > search for Boto3.


Utilizing Python3 to manage AWS
Utilizing Python3 to manage AWS

Now that we're all ready, you should have a blank file ready to go.


Utilizing Python3 to manage AWS

The first thing we are going to do is import Boto3


Utilizing Python3 to manage AWS

Next, let's define our first function.


Utilizing Python3 to manage AWS

Our first function will do the following:

Be named AWS_Access_Key(): It will have one parameter, the access key. It will have an if statement that if accesskey is not None (meaning if it's not null), it'll print 'Access key accepted' Next, we will return the parameter value. Returning the parameter value allows us to use the functions input in another function.

You might be wondering why I have an input function inside our defining parameter. This is for security purposes. The access key/secret key is very powerful. If anyone has that, and you have admin access, they can literally do whatever they want in your AWS environment. Some folks prefer not to do it this way, but this is the way I choose to do it. If you don't want to do it this way, you can define your parameter like so:


Utilizing Python3 to manage AWS

For the secret key, we're going to do the same exact steps.


Utilizing Python3 to manage AWS

Next, we're going to get into the fun.

Let's give a break down of what's happening in the below screenshot.

We're creating a new function called listService(): Be given two parameters that need to be filled in. awsService and region (we will break these down together). Call the boto3 module's client parameter, fill in the appropriate parameters needed, and assign the values to the "service" variable. Let's break down the parameters in boto3.client: the service_name represents what AWS service we want to use. In our case, it's ec2 the region_name represents what region we want to scan. In my case, it's us-east-1 aws_access_key_id is our access key that we put in for the first parameter (notice that it shows the function name as the value because the function has the value due to the return key) aws_secret_access_key is our secret key that we put in for the second parameter (notice that it shows the function name as the value because the function has the value due to the return key)

4. Next, we're going to call our "service" variable that is holding the values for the boto3.client parameter and call another parameter called describe_instances (this is another Boto3 parameter).

5. We're now going to add our filters. I want you to go back to your AWS console, click on your EC2 instance, and take a look at the description. Do you see those values? Those are the values that we can pull. In our case, we will be pulling the instance state name and if it's running, give us value.

6. Next, we're going to print the variable output so we can get out instances.

7. Finally, we're going to define our parameter with it's 'ec2' and 'us-east-1' value.


Utilizing Python3 to manage AWS

Now, we're ready to run! Get your access key and secret key ready.

Go ahead and run the function. You will see the following:


Utilizing Python3 to manage AWS

After you enter your access key, go ahead and enter your secret key


Utilizing Python3 to manage AWS

If all goes well, your keys are accepted, you have the proper rights to ec2, and you have an EC2 instance running, you will see something similar to the follow!


Utilizing Python3 to manage AWS

There you have it! You are now on your way to being a Boto3 expert. Thank you for reading, and I hope you found this both insightful and entertaining!

As promised, below is the code:

import boto3 def AWS_Access_Key(accesskey=input('Please enter access key: ')): if accesskey is not None: print('Access key accepted') return accesskey def AWS_Secret_Key(secretkey=input('Please enter access key: ')): if secretkey is not None: print('Access key accepted') return secretkey def listService(awsService, region): service = boto3.client(service_name = awsService, region_name = region, aws_access_key_id = AWS_Access_Key(), aws_secret_access_key = AWS_Secret_Key() ) instances = service.describe_instances( Filters=[ {'Name': 'instance-state-name', 'Values': ['running']} ]) print(instances) listService('ec2', 'us-east-1')

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

代码区博客精选文章
分页:12
转载请注明
本文标题:Utilizing Python3 to manage AWS
本站链接:https://www.codesec.net/view/611195.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(44)