Last active
December 6, 2022 15:42
-
-
Save matiasba/b0ebc1a08b990976656b025c231ff986 to your computer and use it in GitHub Desktop.
This lambda function allows to deregister a host based on a "EC2 Instance Terminate Successful" event on AWS. It requires an EventBridge with the following configuration as trigger Event pattern: { "source": [ "aws.autoscaling" ], "detail-type": [ "EC2 Instance Terminate Successful" ] }
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import urllib3 | |
from datetime import date | |
def lambda_handler(event, context): | |
today = date.today().strftime("%Y-%m-%d") | |
# Parse the EC2 instance ID from the event data | |
instance_id = event['detail']['EC2InstanceId'] | |
# create urllib3 pool to make HTTP requests | |
http = urllib3.PoolManager() | |
# Set up Zabbix API endpoint and credentials | |
zabbix_api_url = "http://<zabbix-url>/zabbix/api_jsonrpc.php" | |
zabbix_username = "<user>" | |
zabbix_password = "<pass>" | |
# Set up Zabbix API login payload | |
login_payload = { | |
"jsonrpc": "2.0", | |
"method": "user.login", | |
"params": { | |
"user": zabbix_username, | |
"password": zabbix_password | |
}, | |
"id": 1, | |
"auth": None | |
} | |
# Call Zabbix API to login and get auth token | |
login_response = http.request( | |
"POST", | |
zabbix_api_url, | |
headers={"Content-Type": "application/json"}, | |
body=json.dumps(login_payload) | |
) | |
login_response_data = json.loads(login_response.data.decode('utf8')) | |
auth_token = login_response_data["result"] | |
get_host_id_request_body = { | |
"jsonrpc": "2.0", | |
"method": "host.get", | |
"params": { | |
"search": { | |
"host": instance_id | |
}, | |
"output": "hostid" | |
}, | |
"id": 2, | |
"auth": auth_token | |
} | |
# Send the request to get the host ID | |
get_host_id_response = http.request( | |
"POST", | |
zabbix_api_url, | |
headers={"Content-Type": "application/json"}, | |
body=json.dumps(get_host_id_request_body).encode('utf8') | |
) | |
host_id = json.loads(get_host_id_response.data.decode('utf8'))["result"][0]["hostid"] | |
# Set up Zabbix API update host payload | |
update_host_payload = { | |
"jsonrpc": "2.0", | |
"method": "host.update", | |
"params": [ | |
{ | |
"hostid": host_id, | |
"status": 1, | |
"tags": [ | |
{ | |
"tag": "disabled_date", | |
"value": today | |
}, | |
{ | |
"tag": "deregister_by", | |
"value": "lambda" | |
} | |
] | |
} | |
], | |
"id": 2, | |
"auth": auth_token | |
} | |
# Call Zabbix API to update host | |
update_host_response = http.request( | |
"POST", | |
zabbix_api_url, | |
headers={"Content-Type": "application/json"}, | |
body=json.dumps(update_host_payload) | |
) | |
update_host_response_data = json.loads(update_host_response.data.decode('utf8')) | |
return update_host_response_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment