Пример #1
0
def main(event, context):
    """
    Gets layer arns for each region and publish to S3
    """

    packages = get_config.get_packages()

    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table(os.environ['REQS_DB'])
    bucket = os.environ['BUCKET_NAME']

    for package in packages:

        response = table.query(
            IndexName="packageHistory",
            Select="SPECIFIC_ATTRIBUTES",
            ProjectionExpression="package, requirements",
            KeyConditionExpression=Key('package').eq(package),
            Limit=1,
            ScanIndexForward=False
        )  # takes the package with the latest created date

        logger.info(f"Found {len(response['Items'])}")
        requirements_txt = response['Items'][0]['requirements']

        logger.info(f"Requirements.txt for {package} is {requirements_txt}")
        logger.info(f"Uploading to S3 Bucket")
        client = boto3.client('s3')
        client.put_object(Body=requirements_txt.encode('utf-8'),
                          Bucket=bucket,
                          Key=f'packages/{package}/requirements.txt')

    return {"status": "Done"}
Пример #2
0
def main(event, context):
    """
    Args:
      package: Python Package to build and deploy
    return:
      execution_arn: ARN of the state machine execution that is building the package
    """

    packages = get_config.get_packages()

    execution_arns = []

    for package in packages:

        client = boto3.client('stepfunctions')
        execution_time = datetime.now().isoformat().replace('-', '').replace(
            ':', '')[:14]
        response = client.start_execution(
            stateMachineArn=os.environ['PIPELINE_ARN'],
            name=f"{package}_{execution_time}",
            input=json.dumps({"package": package}))

        execution_arns.append(response['executionArn'])

    return {"arns": execution_arns}
Пример #3
0
def main(event, context):
    """
    Args:
      package: Python Package to build and deploy
    return:
      response: Entries in EventBridge for processing
    """

    packages = get_config.get_packages()
    client = boto3.client('events')
    stage = os.environ['STAGE']

    entries = []
    logger.info(f"Preparing {len(packages)} packages")

    # post message to EventBridge to trigger step functions
    for package in packages:

        entry = {
            'Source': f"Klayers.invoke.{stage}",
            'Resources': [],
            'DetailType': "invoke_pipeline",
            'Detail': json.dumps({"package": package}),
            'EventBusName': 'default'
        }
        entries.append(entry)
    # maximum 10 entries per put_events API call
    chunk_10 = [entries[i:i + 10] for i in range(0, len(entries), 10)]
    eventbridge_errors = 0
    for chunk in chunk_10:
        response = client.put_events(Entries=chunk)
        eventbridge_errors += log_eventbridge_errors(response, logger)

    # Post Status to Slack
    message = f"Started build on {len(packages)} packages, with {eventbridge_errors} eventbridge errors"
    entry = {
        'Source': f"Klayers.invoke.{stage}",
        'Resources': [],
        'DetailType': "post_to_slack",
        'Detail': json.dumps({"message": message}),
        'EventBusName': 'default'
    }
    slack_response = client.put_events(Entries=[entry])
    log_eventbridge_errors(slack_response, logger)

    return {
        "num_packages": len(packages),
        "eventbridge_errors": eventbridge_errors
    }
Пример #4
0
def main(event, context):

    region = event['region']
    packages = get_packages()
    lambda_prefix = os.environ['LAMBDA_PREFIX']

    client = boto3.client('lambda', region_name=region)
    deleted_arns = []

    for package in packages:
        arns = delete_old_layer_versions(client=client,
                                         table=table,
                                         region=region,
                                         package=package,
                                         prefix=lambda_prefix)
        deleted_arns.extend(arns)

    return {"deleted_arns": deleted_arns}