Ejemplo n.º 1
0
def update_code_action(name,
                       dry_run=False,
                       publish=False,
                       no_pub=False,
                       bucket_name=False):
    '''
    Updates the code of a given lambda function, options to dry run, publish a version, or NOT publish a version
    First thing we do is check to see if the lambda is there..

    args:
        name:
        dry_run:
        publish:
        no_pub:
        bucket_name: 
    '''
    print('Checking to see if the lambda exists')
    if check(name):
        print('Found lambda %s! Attempting to update code' % name)

        update = update_code(lambda_name=name,
                             dry_run=dry_run,
                             publish=publish,
                             no_pub=no_pub)

        #Check to see if they want to backup
        if bucket_name:
            s3_backup(lambda_name=name,
                      bucket_name=bucket_name,
                      dry_run=dry_run)
        else:
            pass
    else:
        print('Lambda not found!')
        sys.exit(1)
Ejemplo n.º 2
0
def import_action(filename):
    '''
    Calls the import module for AWS provider to collect the configuration
    of the lambda specified in the args. Alias by default is false.

    args:
        lambda_name: the name of the lambda of which we want to import
        alias: the name of the specific alias we want to import
    '''
    #Prompt the user to input the lambda and (optionally) alias they'd like the import
    lambda_name = input(
        "What is the name of the lambda you'd like to import? ")
    alias_prompt = input(
        "Are you importing a configuration from an alias? (y/n): ")

    if alias_prompt in lambda_config.ACCEPTED_PROMPT_ACTIONS:
        if alias_prompt == 'y':
            alias = input("What alias would you like to import from? ")
            print("Importing from alias %s" % alias)
        else:
            alias = False
    else:
        print("No valid answer found, exiting")
        sys.exit(1)

    #to do: add check to make sure lambda and alias exist prior to attempting to import
    check_exist = check(lambda_name)

    if alias:
        alias_check = check_alias(lambda_name, alias)
    else:
        pass

    if check:
        #Call the import function
        configuration = import_lambda(lambda_name=lambda_name, alias=alias)

        #Create a file, we'll call it the name of the lambda
        cwd = os.getcwd()
        full_path = os.path.join(cwd, filename)

        #Dump the config we retrieved from AWS into the file
        f = open(full_path, 'w')
        f.write(json.dumps(configuration, indent=4))
        f.close()
    else:
        print(
            "No lambda found, please check the name you're using and try again"
        )
        sys.exit(1)

    if os.path.exists(full_path):
        print("Import successful!")
        sys.exit(0)
    else:
        print("Import unsuccessful! Exiting")
        sys.exit(1)
Ejemplo n.º 3
0
def delete_action(name, dry_run):
    '''
    Deletes the specified lambda function, has a dry run option just to make sure it will work before actually
    going for it. The first thing we do is check if the specified lambda exists

    args:
        name: name of the lambda we're deleting
        dry_run: boolean, if true only prints what would happen
    '''
    print('Checking to see if lambda exists')

    if check(name):
        print('Lambda exists, attempting to delete %s' % name)

        delete(lambda_name=name, dry_run=dry_run)
    else:
        print('No lambda found, exiting')
        sys.exit(1)
Ejemplo n.º 4
0
def delete_alias_action(name, alias, dry_run):
    '''
    Deletes an alias for the given function, first we check to see if the lambda exists, then we delete alias

    args:
        name: name of the lambda
        alias: name of the alias we're deleting
        dry_run: boolean, if yes no action occurs, just printing
    '''
    print('Checking to see if Lambda exists')
    if check(name):
        print('Lambda found! Attempting to delete alias %s' % alias)

        #Run the destroy function
        alias_destroy(lambda_name=name, del_alias=alias, dry_run=dry_run)

    else:
        print('Lambda not found!')
        sys.exit(1)
Ejemplo n.º 5
0
def update_alias_action(name, alias, dry_run=False, publish=False, weight=False):
    '''
    Updates an alias for the given function, first we check to see if the lambda exists, then we update alias

    args:
        name: name of the lambda
        alias: name of the alias we're updating
        dry_run: boolean, if true no action occurs, just printing
        weight: boolean, if true we're weight shifting an alias across two versions
    '''
    print('Checking to see if Lambda exists')
    if check(name):
        print('Lambda found! Attempting to update alias %s' % alias)

        #Run the update function
        alias_update(lambda_name=name, update_alias=alias, dry_run=dry_run, publish=publish, weight=weight)

    else:
        print('Lambda not found!')
        sys.exit(1)
Ejemplo n.º 6
0
def publish_action(name, version_description):
    '''
    Publishes a new version of the specified lambda. If the version description doesn't exist from the CLI
    it defaults to printing the date + time in UTC as the version description. First thing we do is check to
    see if the lambda exists

    args:
        name: name of the lambda we're publishing a new version of 
        version_description: description of the version (datetime or user specified)
    '''
    print('Checking to see if lambda exists')

    if check(name):
        print(
            'Attempting to publish new version of lambda %s with descriptiong %s'
            % (name, version_description))

        publish(lambda_name=name, version_description=version_description)
    else:
        print(
            'Lambda does not exists, please check your configuration and try again'
        )
        sys.exit(1)
Ejemplo n.º 7
0
def invoke_action(name, invoke_type, payload, version=False, alias=False):
    '''
    Invokes a lambda with the given JSON payload. payload and invoke_type are specified by the CLI.
    First thing we do is check to make sure the lambda exists

    args:
        name: name of the lambda
        version: version we're invoking (if applicable)
        alias: alias we're invoking (if applicable)
        invoke_type: taken from the cli, options are Event, RequestResponse, and DryRun
        payload: json payload from a file
    '''
    print('Checking to see if the lambda exists')
    if check(name):
        print('Found lambda %s attempting to invoke' % name)

        invoke(lambda_name=name,
               version=version,
               alias=alias,
               invoke_type=invoke_type,
               payload=payload)
    else:
        print('No lambda found, please check your configuration and try again')
        sys.exit(1)
Ejemplo n.º 8
0
def create_action(name, runtime, region, role, handler, description, timeout, mem_size, invoke_method=False, invoke_source=False, 
                    alias=False, vpc_setting=False, config_vpc_name=False, config_security_groups=False, dry_run=False, 
                    publish=False, variables=False, logging=False, dead_letter_config=False, dlq_type=False, dlq_name=False, 
                    dest_lambda=False, dest_alias=False, event_type=False, tags=False, tracing_mode=False, bucket_name=False, 
                    log_expire=False, event_source=False, event_source_name=False, event_batch_size=False, event_enabled_status=False,
                    event_start_position=False, concurrency_setting=False):
    '''
    Creates a lambda function, first checks to see if it exists, if yes, exit, else, create lambda

    args:
        too many to list, but everything we collect gets passed into this
    '''
    print("Checking to see if lambda already exists")
    if check(name):
      print("This function already exists, please use action 'update'")
      sys.exit(1)
    else:
        #The core action of this function
        create_function = create(
                                lambda_name=name, 
                                runtime=runtime, 
                                role=role, 
                                handler=handler, 
                                description=description, 
                                timeout=timeout, 
                                mem_size=mem_size, 
                                vpc_setting=vpc_setting, 
                                config_vpc_name=config_vpc_name, 
                                config_security_groups=config_security_groups,
                                user_tags=tags,
                                publish=publish,
                                variables=variables,
                                dead_letter_config=dead_letter_config,
                                dlq_type=dlq_type,
                                dlq_name=dlq_name,
                                tracing_mode=tracing_mode,
                                dry_run=dry_run
                                )

        #Check if alias is true, if it is create an alias
        if alias:
            alias_creation(
                            lambda_name=name,
                            new_alias=alias,
                            dry_run=dry_run,
                            publish=publish
                            )
        else:
            pass

        #Check to see if they have any triggers set up 
        if invoke_method:
            create_trigger(
                            lambda_name=name,  
                            invoke_method=invoke_method, 
                            invoke_source=invoke_source, 
                            alias=alias, 
                            event_type=event_type, 
                            dry_run=dry_run
                        )
        else:
            pass

        #Check to see if they're creating an event source mapping
        if event_source:
            create_event_source(
                                source_type=event_source, 
                                source_name=event_source_name, 
                                lambda_name=name, 
                                batch_size=event_batch_size, 
                                enabled=event_enabled_status, 
                                starting_position=event_start_position,
                                alias=alias
                                )
        else:
            pass

        #Check to see if they have logging set up
        if logging:
            cloudwatchSubscription(
                                    newLambdaName=name, 
                                    destLambdaName=dest_lambda, 
                                    destLambdaAlias=dest_alias, 
                                    region=region, 
                                    role=role
                                    )
        else:
            pass
        
        #Check to see if they're setting an expiration on their logs
        if log_expire:
            set_cloudwatch_log_expiration(name=name, retention_time=log_expire)
        else:
            pass

        #Check to see if they're setting up custom concurrency limits
        if concurrency_setting:
            putFunctionConcurrency(functionName=name, reservedCapacity=concurrency_setting)
        else:
            pass

        #Check to see if they want to backup
        if bucket_name:
            s3_backup(
                        lambda_name=name,
                        bucket_name=bucket_name,
                        dry_run=dry_run
                    )
        else:
            pass

        print('Function creation complete!')
Ejemplo n.º 9
0
def delete(lambda_name, dry_run=False):
    '''
    Handles the deletion of lambdas

    args:
        lambda_name: name of lambda, retrieved from config file
        dry_run: boolean, taken from CLI args
    '''
    if dry_run:
        #Print out all of the things the user would delete if they were to delete
        try:
            print(color.PURPLE +
                  "***Dry run option enabled, dry running a deletion***" +
                  color.END)
            alias = client.list_aliases(FunctionName='%s' % lambda_name, )

            dump_json = json.dumps(alias, indent=4)
            load = json.loads(dump_json)

            #set up an array of aliases, letting the user know this is FOR REAL FOR REAL
            aliases = []

            for names in load['Aliases']:
                aliases.append(names['Name'])

            print(color.PURPLE + "Would delete:" + color.END)
            print(color.PURPLE + "Lambda: %s" % lambda_name + color.END)
            for item in aliases:
                print(color.PURPLE + "Alias: %s" % item + color.END)

            versions = client.list_versions_by_function(FunctionName='%s' %
                                                        lambda_name, )

            version_json = json.dumps(versions, indent=4)
            load_json = json.loads(version_json)
            versions = load_json['Versions']

            #set up an array of versions, letting the user know this is again, for really real
            avail_versions = []

            for version in versions:
                avail_versions.append(version['Version'])

            for vers in avail_versions:
                print(color.PURPLE + "Version: %s" % vers + color.END)
        except ClientError as error:
            print(color.RED + error.response['Error']['Message'] + color.END)
    else:
        #just to be extra cautious, let's double check with the user
        double_check = input(
            "Are you SURE you want to delete this lambda (y/n)? ")
        lowered_checked = double_check.lower()

        #validate their answer then do the deed
        if lowered_checked == 'y':
            try:
                delete = client.delete_function(FunctionName='%s' %
                                                lambda_name)
                if check(lambda_name):
                    print(color.RED + "Failed to delete Lambda" + color.END)
                    return False
                else:
                    print(color.CYAN +
                          "Lambda %s deleted successfully" % lambda_name +
                          color.END)
                    return True
            except ClientError as error:
                print(color.RED + error.response['Error']['Message'] +
                      color.END)
                sys.exit(1)
        else:
            print(color.RED + "Exiting" + color.END)
            sys.exit(1)
Ejemplo n.º 10
0
def update_config_action(name,
                         handler,
                         description,
                         timeout,
                         mem_size,
                         runtime,
                         role,
                         alias=False,
                         vpc_setting=False,
                         vpc_name=False,
                         vpc_security_groups=False,
                         tags=False,
                         variables=False,
                         dlq=False,
                         dlq_type=False,
                         dlq_name=False,
                         tracing_mode=False,
                         create_trigger_bool=False,
                         remove_trigger_bool=False,
                         invoke_method=False,
                         invoke_source=False,
                         event_type=False,
                         dry_run=False,
                         log_expire=False,
                         event_source=False,
                         event_source_name=False,
                         event_batch_size=False,
                         event_enabled_status=False,
                         event_start_position=False,
                         concurrency_setting=False):
    '''
    Updates the configuration of the given lambda, this has all the same parameters as the create function with the
    exception of updating code. Every single parameter (except code) can be changed using this. 

    This module is also used to add or delete triggers from the CLI.

    args:
        too many to list, but all of them except for updating code
    '''
    if check(name):

        update_config(lambda_name=name,
                      handler=handler,
                      description=description,
                      timeout=timeout,
                      mem_size=mem_size,
                      runtime=runtime,
                      role=role,
                      vpc_setting=vpc_setting,
                      config_vpc_name=vpc_name,
                      config_security_groups=vpc_security_groups,
                      new_tags=tags,
                      variables=variables,
                      dead_letter_config=dlq,
                      dlq_type=dlq_type,
                      dlq_name=dlq_name,
                      tracing_mode=tracing_mode)

        if remove_trigger_bool:
            remove_trigger(lambda_name=name,
                           alias=alias,
                           invoke_source=invoke_source)
            sys.exit(0)
        else:
            pass

        #Check to see if they're setting an expiration on their logs
        if log_expire:
            set_cloudwatch_log_expiration(name=name, retention_time=log_expire)
        else:
            pass

        #Check to see if they have a trigger in their config
        if create_trigger_bool or invoke_method:

            #Grab the current policy, we'll validate against this to make sure the policy doesn't exit already
            current_trigger_list = get_lambda_policy(name=name, alias=alias)

            if current_trigger_list:
                if all(item in current_trigger_list
                       for item in [alias, invoke_method, invoke_source]):
                    print('Trigger exists already, exiting gracefully')
                    sys.exit(0)
            create_trigger(lambda_name=name,
                           invoke_method=invoke_method,
                           invoke_source=invoke_source,
                           alias=alias,
                           event_type=event_type,
                           dry_run=dry_run)

        #Check to see if they're creating/updating an event source mapping
        if event_source:
            if check_source_mapping(source_type=event_source,
                                    source_name=event_source_name,
                                    lambda_name=name,
                                    alias=alias):
                #If check_source_mapping returns true it means we have confirmed we've got the event stream already setup

                update_event_source(source_type=event_source,
                                    source_name=event_source_name,
                                    lambda_name=name,
                                    batch_size=event_batch_size,
                                    enabled=event_enabled_status,
                                    alias=alias)
            else:
                #If check source mapping returns False it means we need create the mapping

                create_event_source(source_type=event_source,
                                    source_name=event_source_name,
                                    lambda_name=name,
                                    batch_size=event_batch_size,
                                    enabled=event_enabled_status,
                                    starting_position=event_start_position,
                                    alias=alias)
        else:
            pass

        #Check to see if they're setting up custom concurrency limits
        if concurrency_setting:
            putFunctionConcurrency(functionName=name,
                                   reservedCapacity=concurrency_setting)
        else:
            putFunctionConcurrency(functionName=name)

        print('Function configuration update complete!')