[PYTHON] read the tag assigned to you on ec2 with boto3

Tag function provided in ec2. It is convenient to use it like an environment variable. Look at a specific tag and change the configuration file read by the application at runtime. Toka Toka. ec2 has API to get your own metadata. Let's try it with boto3.

First of all, as a preliminary preparation, make access_key_id and secret_access_key visible from the application side. It's okay to write solid in the source code, but it's not secure, so export it to the shell.

$ export AWS_KEY='some-key'
$ export AWS_SECRET='some-secret'
$ export AWS_REGION='ap-northeast-1'

Try to get this from python.

console


>>> import os
>>> os.environ.get('AWS_KEY')
'some-key'
>>> os.environ.get('AWS_SECRET')
'some-secret'
>>> os.environ.get('AWS_REGION')
'ap-northeast-1'

If you can confirm that you can get it, try to get the tag actually assigned to you.

console


>>> import os
>>> import urllib2
>>> from boto3.session import Session
>>> 
#My instance from the metadata API-Fetch the id.
>>> instance_id = urllib2.urlopen('http://169.254.169.254/latest/meta-data/instance-id').read()
>>> 
#Create a session for aws api.
>>> aws = Session(aws_access_key_id=os.environ.get('AWS_KEY'),
...               aws_secret_access_key=os.environ.get('AWS_SECRET'),
...               region_name=os.environ.get('AWS_REGION'))
>>> 
#Create an ec2 object.
>>> ec2 = aws.resource('ec2')
# instance-Create an instance object by specifying id. So to speak, this is myself.
>>> instance = ec2.Instance(id=instance_id)
>>> 
#Get the tag completely. It's a little difficult to use, so I'm shaping it.
>>> tags = dict([(tag['Key'], tag['Value']) for tag in instance.tags])
#It is listed here. Should be.
>>> tags
{'Name': 'API_PROD', ...}

Personally, I always add an Env tag, and I try to decide the configuration file to read by looking at it. If you do this, you can change the settings around db and the bucket of s3 at runtime even with the same ami, which is convenient and convenient :-)

Recommended Posts

read the tag assigned to you on ec2 with boto3
Introduction to Python with Atom (on the way)
Describe ec2 with boto3 and retrieve the value
How to get the key on Amazon S3 with Boto 3, implementation example, notes
Visualize the timeline of the number of issues on GitHub assigned to you in Python
You can read the analog meter with the example MNIST.
Transit to the update screen with the Django a tag
You can read the analog meter with the example MNIST.
I wrote you to watch the signal with Go
I tried to analyze the whole novel "Weathering with You" ☔️
Memo to get the value on the html-javascript side with jupyter
Move what you installed with pip to the conda environment
[AWS EC2] Settings you want to do on Amazon Linux 2
Save images on the web to Drive with Python (Colab)
You there who want to launch Rails on both the host and the client with Docker Compose!
Read the coordinates of the plot on the graph with Python-matplotlib (super beginner)
A note on what you did to use Flycheck with Python
How to make a command to read the configuration file with pyramid
How to install pandas on EC2 (How to deal with MemoryError and PermissionError)
I tried to get started with Bitcoin Systre on the weekend
What to do if the inode is exhausted on EC2 Linux
When you want to adjust the axis scale interval with APLpy
Nice to meet you with python
Preparing to run Flask on EC2
How to read the SNLI dataset
Connect to Elastic MQ with boto
Use boto3 to mess with S3
[Python] I tried to visualize the night on the Galactic Railroad with WordCloud!
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
Connect to VPN with your smartphone and turn off / on the server
[Introduction to machine learning] Until you run the sample code with chainer
If you want to include awsebcli with CircleCI, specify the python version
18 beautiful Python terms you want to read aloud. R18 with example sentences
Add 95% confidence intervals on both sides to the diagram with Python / Matplotlib
Use python on Raspberry Pi 3 to light the LED with switch control!
[Image recognition] How to read the result of automatic annotation with VoTT
I tried with the top 100 PyPI packages> I tried to graph the packages installed on Python
Read the file with python and delete the line breaks [Notes on reading the file]
I tried to visualize the text of the novel "Weathering with You" with WordCloud
Prepare the environment of Chainer on EC2 spot instance with AWS Lambda
# 1 Until you deploy Django's web application (instance construction with EC2 on AWS)
The story of trying to push SSH_AUTH_SOCK obsolete on screen with LD_PRELOAD
[Cloudian # 5] Try to list the objects stored in the bucket with Python (boto3)
Move the turtle to the place where you click the mouse with turtle in Python
How to deal with "^ [[A ^ [[B ^ [[C ^ [[D"] when you press the arrow keys when executing python on mac
How to read pydoc on python interpreter
Until you can read the error log
[C] [python] Read with AquesTalk on Linux
Let's read the RINEX file with Python ①
Try server-side encryption on S3 with boto3
Download files on the web with Python
Tag jump to PyPI module with ctags
Match the colorbar to the figure with matplotlib
You search commandlinefu on the command line
How to read problem data with paiza
The road to compiling to Python 3 with Thrift
Books on data science to read in 2020
How to publish a blog on Amazon S3 with the static Blog engine'Pelican'for Pythonista
I made you to express the end of the IP address with L Chika
[OpenCV] When you want to check if it is read properly with imread
[Python] How to save images on the Web at once with Beautiful Soup