Skip to content

Instantly share code, notes, and snippets.

@chloegrace94
chloegrace94 / decrypt_environment_variable.py
Created October 2, 2018 15:14
How to decrypt an AWS Lambda environment variable
import boto3 # AWS SDK for Python
import os
from base64 import b64decode
# Retrieve environment variables
encrypted_environment_variable_a = os.environ['a']
encrypted_environment_variable_b = os.environ['b']
# KMS Client
@chloegrace94
chloegrace94 / verify_request.py
Created October 2, 2018 15:07
Function verifies a request from slack using both the Slack Signing secret and Verification token methods
# Verify the Slack Signature and Verification token
def verify(raw_body, token, headers):
try:
# Define variables
version_no = "v0"
# Verify Signing Secret
slack_signature = headers['X-Slack-Signature']
slack_request_timestamp = headers['X-Slack-Request-Timestamp']
@chloegrace94
chloegrace94 / example_event.json
Created October 2, 2018 15:02
Example event received by immediate response Lambda
{
"body": "payload=%7B%22type%22%3A%22interactive_message%22%2C%22actions%22%3A%5B%7B%22name%22...",
"headers": {
"Accept": "application/json,*/*",
...
"Content-Type": "application/x-www-form-urlencoded",
"X-Slack-Request-Timestamp": "1537...",
"X-Slack-Signature": "v0=..."
...
},
@chloegrace94
chloegrace94 / post_to_slack_simple.py
Created October 2, 2018 14:45
Posts non-interactive message to Slack
# Post to Slack
def post_to_slack(message, instance_info):
# Slack users
# cyoung = @cyoung
# user_2 = @user_2
owner_dict = {
"cyoung" : "xxxxxxxxx",
"user_2" : "zzzzzzzzz"
@chloegrace94
chloegrace94 / RDS_instance_candidate_finder.py
Created October 2, 2018 14:40
Function identifies RDS instances - messages instance owners when all criteria are met
# RDS instance fact finder
def rds_fact_and_candidate_finder(session, rds_agelimit, rds_running_agelimit, reserved_til, nowdatetime):
resource_type = 'RDS'
client = session.client('rds')
db_instances = client.describe_db_instances()
for inst in db_instances['DBInstances']:
start_time = None
# Get instance owner & static value from tags
@chloegrace94
chloegrace94 / EC2_instance_candidate_finder.py
Created October 2, 2018 14:36
Function identifies EC2 instances - messages instance owners when all criteria are met
# EC2 instance candidate finder
def ec2_candidate_finder(response, agelimit, reserved_til, nowdatetime):
resource_type = 'EC2'
for instance in (response['Reservations']):
InstanceId = (instance['Instances'][0]['InstanceId'])
launch_time = (instance['Instances'][0]['LaunchTime'])
tags = (instance['Instances'][0]['Tags'])
inst_owner = None