Exemple #1
0
def lambda_deploy(project_dir, aws_region, stack_name):
    # if the stack name exists and the state is in rollback_complete or
    # other 'bad' state, we will delete the stack first, and then deploy
    # it
    logger.debug('Ensure stack "%s" is ready to deploy', stack_name)
    ensure_is_ready_to_deploy_to_cloud_formation(stack_name, aws_region)
    logger.debug('Stack "%s"is ready to deploy', stack_name)

    template_file = os.path.join(project_dir, ".aws-sam", "build", "packaged.yaml")
    return_code, stdout, stderr = call_sam_command(
        [
            "deploy",
            "--stack-name",
            stack_name,
            "--capabilities",
            "CAPABILITY_IAM",
            "--template-file",
            template_file,
            "--region",
            aws_region,
        ],
        project_dir=project_dir,
        region=aws_region,
    )
    if return_code != 0:
        error_message = stderr
        if not error_message:
            error_message = stdout
        raise BentoMLException(
            "Failed to deploy lambda function. {}".format(error_message)
        )
    else:
        return stdout
Exemple #2
0
def lambda_package(project_dir, aws_region, s3_bucket_name, deployment_prefix):
    prefix_path = os.path.join(deployment_prefix, "lambda-functions")
    build_dir = os.path.join(project_dir, ".aws-sam", "build")

    return_code, stdout, stderr = call_sam_command(
        [
            "package",
            "--force-upload",
            "--s3-bucket",
            s3_bucket_name,
            "--s3-prefix",
            prefix_path,
            "--template-file",
            "template.yaml",
            "--output-template-file",
            "packaged.yaml",
            "--region",
            aws_region,
        ],
        project_dir=build_dir,
        region=aws_region,
    )
    if return_code != 0:
        error_message = stderr
        if not error_message:
            error_message = stdout
        raise BentoMLException(
            "Failed to package lambda function. {}".format(error_message)
        )
    else:
        return stdout
Exemple #3
0
def build_template(template_file_path, project_directory, region):
    status_code, stdout, stderr = call_sam_command(
        ["build", "-t", template_file_path], project_directory, region)

    if status_code != 0:
        error_message = stderr if stderr else stdout
        raise BentoMLException(
            "Failed to build ec2 service {}".format(error_message))

    return status_code, stdout, stderr
Exemple #4
0
def package_template(s3_bucket_name, project_directory, region):
    status_code, stdout, stderr = call_sam_command(
        [
            "package",
            "--output-template-file",
            "packaged.yaml",
            "--s3-bucket",
            s3_bucket_name,
        ],
        project_directory,
        region,
    )
    if status_code != 0:
        error_message = stderr if stderr else stdout
        raise BentoMLException(
            "Failed to package ec2 service {}".format(error_message))
    return status_code, stdout, stderr
Exemple #5
0
def deploy_template(stack_name, s3_bucket_name, project_directory, region):
    status_code, stdout, stderr = call_sam_command(
        [
            "deploy",
            "--template-file",
            "packaged.yaml",
            "--stack-name",
            stack_name,
            "--capabilities",
            "CAPABILITY_IAM",
            "--s3-bucket",
            s3_bucket_name,
        ],
        project_directory,
        region,
    )
    if status_code != 0:
        error_message = stderr if stderr else stdout
        raise BentoMLException(
            "Failed to deploy ec2 service {}".format(error_message))
    return status_code, stdout, stderr
Exemple #6
0
def init_sam_project(
    sam_project_path,
    bento_service_bundle_path,
    deployment_name,
    bento_name,
    api_names,
    aws_region,
):
    function_path = os.path.join(sam_project_path, deployment_name)
    os.mkdir(function_path)
    # Copy requirements.txt
    logger.debug("Copying requirements.txt")
    requirement_txt_path = os.path.join(bento_service_bundle_path, "requirements.txt")
    shutil.copy(requirement_txt_path, function_path)

    bundled_dep_path = os.path.join(
        bento_service_bundle_path, "bundled_pip_dependencies"
    )
    if os.path.isdir(bundled_dep_path):
        # Copy bundled pip dependencies
        logger.debug("Copying bundled_dependencies")
        shutil.copytree(
            bundled_dep_path, os.path.join(function_path, "bundled_pip_dependencies")
        )
        bundled_files = os.listdir(
            os.path.join(function_path, "bundled_pip_dependencies")
        )
        for index, bundled_file_name in enumerate(bundled_files):
            bundled_files[index] = "./bundled_pip_dependencies/{}\n".format(
                bundled_file_name
            )

        logger.debug("Updating requirements.txt")
        with open(
            os.path.join(function_path, "requirements.txt"), "r+"
        ) as requirement_file:
            required_modules = bundled_files + requirement_file.readlines()
            # Write from beginning of the file, instead of appending to the end.
            requirement_file.seek(0)
            requirement_file.writelines(required_modules)

    # Copy bento_service_model
    logger.debug("Copying model directory")
    model_path = os.path.join(bento_service_bundle_path, bento_name)
    shutil.copytree(model_path, os.path.join(function_path, bento_name))

    logger.debug("Creating python files for lambda function")
    # Create empty __init__.py
    open(os.path.join(function_path, "__init__.py"), "w").close()

    app_py_path = os.path.join(os.path.dirname(__file__), "lambda_app.py")
    shutil.copy(app_py_path, os.path.join(function_path, "app.py"))
    unzip_requirement_py_path = os.path.join(
        os.path.dirname(__file__), "download_extra_resources.py"
    )
    shutil.copy(
        unzip_requirement_py_path,
        os.path.join(function_path, "download_extra_resources.py"),
    )

    logger.info("Building lambda project")
    return_code, stdout, stderr = call_sam_command(
        ["build", "--use-container", "--region", aws_region],
        project_dir=sam_project_path,
        region=aws_region,
    )
    if return_code != 0:
        error_message = stderr
        if not error_message:
            error_message = stdout
        raise BentoMLException(
            "Failed to build lambda function. {}".format(error_message)
        )
    logger.debug("Removing unnecessary files to free up space")
    for api_name in api_names:
        cleanup_build_files(sam_project_path, api_name)