-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathec2_plugin.py
32 lines (24 loc) · 906 Bytes
/
ec2_plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import logging
from future.standard_library import install_aliases
install_aliases()
from urllib.request import urlopen
log = logging.getLogger(__name__)
SERVICE_NAME = 'ec2'
ORIGIN = 'AWS::EC2::Instance'
def initialize():
"""
Try to get EC2 instance-id and AZ if running on EC2
by querying http://169.254.169.254/latest/meta-data/.
If not continue.
"""
global runtime_context
try:
runtime_context = {}
r = urlopen('http://169.254.169.254/latest/meta-data/instance-id', timeout=1)
runtime_context['instance_id'] = r.read().decode('utf-8')
r = urlopen('http://169.254.169.254/latest/meta-data/placement/availability-zone',
timeout=1)
runtime_context['availability_zone'] = r.read().decode('utf-8')
except Exception:
runtime_context = None
log.warning("failed to get ec2 instance metadata.")