Skip to content

Commit

Permalink
feat: adding sns integration to lambda function (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian-Soares authored Jul 17, 2024
1 parent ed1dfc0 commit 783b51e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 45 deletions.
13 changes: 10 additions & 3 deletions infrastructure/ebs-checker/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ module "complete" {
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:policy/ebs-modify-policy",
]
lambda_function_env = {
"REGION" = var.default_aws_region
"LOG_LEVEL" = "INFO"
"MODIFY_EBS" = true
"REGION" = var.default_aws_region
"LOG_LEVEL" = "INFO"
"MODIFY_EBS" = true
SNS_TOPIC_ARN = module.complete.sns_topic_arn
}
lambda_function_tags = {
"Environment" = "PRD"
Expand All @@ -24,6 +25,12 @@ module "complete" {

create_sns_topic = true
sns_topic_name = "ebs-checker-topic"
sns_topic_subscriptions = [
{
protocol = "email"
endpoint = "[email protected]"
}
]

depends_on = [aws_iam_policy.ebs_modify_policy]
}
Expand Down
2 changes: 2 additions & 0 deletions infrastructure/modules/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ module "sns_topic" {
create = try(var.create_sns_topic, true)
name = var.sns_topic_name

subscriptions = try(var.sns_topic_subscriptions, [])

tags = merge(var.sns_topic_tags, var.default_tags)
}
7 changes: 7 additions & 0 deletions infrastructure/modules/complete/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "lambda_function_arn" {
value = module.lambda_function.lambda_function_arn
}

output "sns_topic_arn" {
value = module.sns_topic.topic_arn
}
9 changes: 9 additions & 0 deletions infrastructure/modules/complete/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ variable "sns_topic_name" {
type = string
}

variable "sns_topic_subscriptions" {
description = "The subscriptions for the SNS topic"
type = list(object({
protocol = string
endpoint = string
}))
default = []
}

variable "sns_topic_tags" {
description = "The tags to apply to the SNS topic"
type = map(string)
Expand Down
79 changes: 37 additions & 42 deletions lambdas/ebs-checker/lambda_function.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
# Inspiration: https://saitejamakani.medium.com/automated-migration-of-amazon-ebs-volumes-from-gp2-to-gp3-using-python-and-boto3-65ec51d2ba91
import csv
import boto3
import os
import logging
import json

REGION = os.environ.get('REGION', 'us-east-2')
LOG_LEVEL = os.environ.get('LOG_LEVEL', 'INFO')
MODIFY_EBS = os.environ.get('MODIFY_EBS', True)
SNS_TOPIC_ARN = os.environ.get('SNS_TOPIC_ARN')

def get_ec2_volumes(file_name, filter, REGION):
ec2 = boto3.client('ec2', REGION)
ec2 = boto3.client('ec2', REGION)
sns = boto3.client('sns', REGION)

def get_ec2_volumes(filter, REGION):
paginator = ec2.get_paginator('describe_volumes')
paginationConfig_ = {'MaxItems': 500, 'PageSize': 500}
response_iterator = paginator.paginate(
Expand All @@ -23,63 +26,55 @@ def get_ec2_volumes(file_name, filter, REGION):
else:
throughput_ = None

volumes.append([volume['VolumeId'],
volume['VolumeType'],
volume['CreateTime'],
volume['Iops'],
volume['State'],
volume['Size'],
throughput_])
volumes.append({
"VolumeId": volume['VolumeId'],
"VolumeType": volume['VolumeType'],
"AWS-CreateTime": volume['CreateTime'].strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
"Iops": volume['Iops'],
"State": volume['State'],
"Size": volume['Size'],
"Throughput": throughput_
})

message = {
"Volumes": volumes,
"Count": len(volumes)
}

sns.publish(
TopicArn=SNS_TOPIC_ARN,
Message=json.dumps(message),
Subject="EC2 gp2 Volumes List"
)

with open(file_name, "w", newline="") as file:
writer = csv.writer(file)
# Write Header
writer.writerow(
[
"VolumeId",
"VolumeType",
"AWS-CreateTime",
"Iops",
"State",
"Size",
"Throughput"
]
)
# Write volumes list
writer.writerows(volumes)
logging.info("EC2 gp2 resources size: " + str(len(volumes)))
return volumes

def modify_volume_gp3(volume_ids, REGION):
ec2 = boto3.client('ec2', REGION)
modify_response = {}
for volume_id in volume_ids:
try:
response = ec2.modify_volume(VolumeId=volume_id, VolumeType='gp3')
response = []
modify_response[volume_id] = response
except Exception as e:
logging.error(f"Exception to modify volume: {volume_id} -> {str(e)}")

if modify_response != {}:
logging.info(f"Modified volumes: {modify_response}" )
with open("./modified_volumes.json", "w", newline="") as mod_file:
writer = csv.writer(mod_file)
writer.writerows(modify_response)
return
if modify_response:
sns.publish(
TopicArn=SNS_TOPIC_ARN,
Message=json.dumps(modify_response),
Subject="Modified Volumes"
)
logging.info(f"Modified volumes: {modify_response}")

def lambda_handler(event, context):
try:
filter = [{'Name': 'volume-type', 'Values': ['gp2']}]
# File to save list of gp2 volumes
file_name = "./gp2_volumes_list.csv"
# get gp2 columes list
volumes = get_ec2_volumes(file_name, filter, REGION)
gp2_volume_ids = []
for volumn in volumes:
gp2_volume_ids.append(volumn[0])
# get gp2 volumes list
volumes = get_ec2_volumes(filter, REGION)
gp2_volume_ids = [volume['VolumeId'] for volume in volumes]
# Modify gp2 volumes to gp3
if bool(MODIFY_EBS): modify_volume_gp3(gp2_volume_ids, REGION)
except Exception as e:
logging.error("Exception: " + str(e))
raise e
raise e

0 comments on commit 783b51e

Please sign in to comment.