Manage AWS nicely with the Python library Boto

Perhaps the number of places using AWS for Web services etc. is increasing. And, as is well known, you know that AWS also exposes console commands and APIs that can be controlled by libraries in various languages.

Recently, at the startup I work for, for example, when I launch a new instance, there is a procedure to put some instance in the security group, but I want to automate this! Because it's annoying and I forget! That's why I wrote the script in Python.

Use Boto

So, using the Python library Boto, it's easier to launch these instances and add them to security groups. So I would like to give you a brief overview of this Boto library.

Things necessary

Let's issue an Auth key for AWS. Ask them to issue a Key while following the help procedure below.

Try to connect

First of all, let's connect to AWS using Boto. In my case, I wrote as follows.

connection = boto.ec2.connect_to_region(
    REGION,
    aws_access_key_id=YOUR_ACCESS_KEY,
    aws_secret_access_key=YOUR_SECRET_KEY)

One of the things I'm a little addicted to is how to specify the region. When specifying region, it is necessary to specify, for example, ʻap-northeast-1`.

Also, if you use connect_to_region, the instance that will be the entrance to the connection will be issued. Future operations are basically done from here.

Try setting each IP in the security group

For example, let's get a list of currently existing security groups.

security = connection.get_all_security_groups(groupnames=['foo', 'bar'])

By doing this, you can get each security group. I haven't looked into it in detail, but if you just specify it with groupnames, an instance will be issued to connect to each security group. For example, if you want to use the first group, it will look like the one below.

security[0].authorize(
    ip_protocol="tcp",
    from_ports=ps[1],
    to_port=ps[1],
    cidr_ip="YOUR.IP/32")

By the way, it's easy to forget that if you don't add / 32 etc. when you specify cidr_ip, an error will be returned. Also, if the corresponding IP already exists in the security group, an error will be returned. Therefore, you need to try as shown below.

try:
    # do it
except EC2ResponseError, e:
    if e.status == 400:
        print("No Problem!!")
    else:
        raise e

By the way, if dry_run succeeds, it seems that the status code of 412 is returned.

Finally

Automation has come to the fore as a recent keyword. Also, if you are a venture company, you may be using AWS in anticipation of sudden scale. However, it is troublesome to hit AWS one by one. But thankfully, AWS has a lot of APIs like this. Previously, I also set up an instance.

If you use such a library and make the usual procedure into one command, AWS business will be accelerated. Why don't you try one in your own language?

Recommended Posts

Manage AWS nicely with the Python library Boto
AWS CDK with Python
Boto3 (manipulate AWS resources with Python library) API that is often used privately
[Cloudian # 8] Try setting the bucket versioning with Python (boto3)
Manage cron jobs with python
S3 operation with python boto3
Call the API with python3.
I tried using the Python library from Ruby with PyCall
Display only the resources created when acquiring AWS resources with Boto3
[AWS] Try adding Python library to Layer with SAM + Lambda (Python)
Run the intellisense of your own python library with VScode.
What you can do with the Python standard library statistics
[Package cloud] Manage python packages with package cloud
Extract the xz file with python
Get AWS account ID with boto3
Get the weather with Python requests
Find the Levenshtein Distance with python
Hit the Etherpad-lite API with Python
[Python] How to import the library
boto3 (AWS SDK for Python) Note
I liked the tweet with python. ..
[Hyperledger Iroha] Query with Python library
Manage each Python version with Homebrew
Master the type with Python [Python 3.9 compatible]
Notify HipChat with AWS Lambda (Python)
Manage your data with AWS RDS
[Cloudian # 6] Try deleting the object stored in the bucket with Python (boto3)
Make the Python console covered with UNKO
[AWS] Using ini files with Lambda [Python]
[Python] Set the graph range with matplotlib
Behind the flyer: Using Docker with Python
Check the existence of the file with python
[Python] Get the variable name with str
[Python] Round up with just the operator
Display Python 3 in the browser with MAMP
Search the maze with the python A * algorithm
Let's read the RINEX file with Python ①
Use cryptography library cryptography with Docker Python image
Working with OpenStack using the Python SDK
[Cloudian # 1] Try to access object storage with AWS SDK for Python (boto3)
What is "mahjong" in the Python library? ??
I want to play with aws with python
Download files on the web with Python
Getting started with Dynamo from Python boto
I get a Python No module named'encodings' error with the aws command
[AWS] Link Lambda and S3 with boto3
Try HTML scraping with a Python library
Connect to s3 with AWS Lambda Python
Publish your own Python library with Homebrew
Learn the design pattern "Singleton" with Python
[Python] Automatically operate the browser with Selenium
S3 server-side encryption SSE with Python boto3
[Cloudian # 5] Try to list the objects stored in the bucket with Python (boto3)
Manage multiple Python versions with update-alternatives (Ubuntu)
Touch AWS with Serverless Framework and Python
Learn the design pattern "Facade" with Python
The road to compiling to Python 3 with Thrift
Python + Selenium + Headless Chromium with aws lambda
Make the library created by Eigen in C ++ available from Python with Boost.Numpy.
Location information data display in Python --Try plotting with the map display library (folium)-
I tried "smoothing" the image with Python + OpenCV