Coder Social home page Coder Social logo

lambda-aws-ses-send-email's People

Contributors

thigley986 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

lambda-aws-ses-send-email's Issues

Passing variable

import boto3   
import collections     
import datetime     
import time     
import sys 

ses = boto3.client('ses')

ec = boto3.client('ec2', 'eu-west-1')     
ec2 = boto3.resource('ec2', 'eu-west-1')     
from datetime import datetime
from dateutil.relativedelta import relativedelta

#create date variables 

date_after_month = datetime.now()+ relativedelta(days=7)
#date_after_month.strftime('%d/%m/%Y')
today=datetime.now().strftime('%d/%m/%Y')


def lambda_handler(event, context): 
  #Get instances with Owner Taggs and values Unknown/known
    instance_ids = []
    reservations = ec.describe_instances(     
        Filters=[     
            {'Name': 'tag:Owner', 'Values': ['Unknown', 'unknown']},     
        ]     
    ).get('Reservations', []) 

    for reservation in reservations:
          instances = reservation['Instances']
          for instance in instances:
              instance_ids.append(instance['InstanceId'])
              inst=format(','.join(instance_ids)) 
              email_body=format(','.join(instance_ids))
              
              tags = {}
              for tag in instance['Tags']:
                tags[tag['Key']] = tag['Value']  
                
                #Check if "TerminateOn" tag exists:
                
              if 'TerminateOn' in tags:  
                  #compare TerminteOn value with current date
                    if tags["TerminateOn"]==today:
                    
                    #Check if termination protection is enabled
                     terminate_protection=ec.describe_instance_attribute(InstanceId =instance['InstanceId'] ,Attribute = 'disableApiTermination')
                     protection_value=(terminate_protection['DisableApiTermination']['Value'])
                     #if enabled disable it
                     if protection_value == True:
                      ec.modify_instance_attribute(InstanceId=instance['InstanceId'],Attribute="disableApiTermination",Value= "False" )
                    #terminate instance 
                      ec.terminate_instances(InstanceIds=instance_ids)
                    #Send an email to engineering that the instance has been terminated. (as many emails as number of instances  
                      #body=format(','.join(instance_ids))
                      response = ses.send_email(
                      Source =  '[email protected]',
                      Destination={
                      'ToAddresses': [
                      '[email protected]',
                      ],
                      },
                      Message={
                      'Subject': {
                      'Data': 'Instance will be removed'
                      },
                      'Body': {
                      'Text': {
                     'Data': email_body + ' Has been terminated'
                      }
                      }
                      }
                      )
                    else: 
                    #Send an email to engineering that this instance will be removed X amount of days (calculate the date based on today's date and the termination date."
                      print email_body
                      
                      response = ses.send_email(
                      Source =  '[email protected]',
                      Destination={
                      'ToAddresses': [
                      '[email protected]',
                      ],
                      },
                      Message={
                      'Subject': {
                      'Data': 'Instance will be removed'
                      },
                      'Body': {
                      'Text': {
                     'Data': email_body + "will be removed after 7 days"
                      }
                      }
                      }
                      )
                
              else: 
                 if not 'TerminateOn' in tags:#, create it  
                  ec2.create_tags(Resources=instance_ids,Tags=[{'Key':'TerminateOn','Value':date_after_month.strftime('%d/%m/%Y')}])
                  ec.stop_instances(InstanceIds=instance_ids)
                  print "last "+format(','.join(instance_ids))
   

i want to pass email_body=format(','.join(instance_ids)) as variable to your function but it's not happening

here is what i'm getting:

'Data': email_body + "will be removed after 7 days"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.