Exemplo n.º 1
0
def create_and_push_docker_image_to_ecr(region, bento_name, bento_version,
                                        snapshot_path):
    """Create BentoService sagemaker image and push to AWS ECR

    Example: https://github.com/awslabs/amazon-sagemaker-examples/blob/\
        master/advanced_functionality/scikit_bring_your_own/container/build_and_push.sh
    1. get aws account info and login ecr
    2. create ecr repository, if not exist
    3. build tag and push docker image

    Args:
        region(String)
        bento_name(String)
        bento_version(String)
        snapshot_path(Path)

    Returns:
        str: AWS ECR Tag
    """
    repository_id = create_ecr_repository_if_not_exists(
        region, f'{bento_name}-sagemaker'.lower())
    logger.debug("Getting docker login info from AWS")
    registry_url, username, password = get_ecr_login_info(
        region, repository_id=repository_id)

    ecr_tag = generate_docker_image_tag(f'{bento_name}-sagemaker',
                                        bento_version, registry_url)

    logger.debug("Building docker image: %s", ecr_tag)
    build_docker_image(context_path=snapshot_path,
                       dockerfile='Dockerfile-sagemaker',
                       image_tag=ecr_tag)

    logger.debug("Pushing image to AWS ECR at %s", ecr_tag)
    push_docker_image_to_repository(repository=ecr_tag,
                                    username=username,
                                    password=password)
    logger.debug("Finished pushing image: %s", ecr_tag)
    return ecr_tag
Exemplo n.º 2
0
def test_get_ecr_login_info():
    def mock_ecr_client_auth_token(
        self, op_name, args
    ):  # pylint: disable=unused-argument
        if op_name == "GetAuthorizationToken":
            return {
                "authorizationData": [
                    {
                        "authorizationToken": mock_repository_auth_token,
                        "proxyEndpoint": mock_repository_endpoint,
                    }
                ]
            }

    with patch(
        "botocore.client.BaseClient._make_api_call", new=mock_ecr_client_auth_token
    ):
        registry_url, username, password = get_ecr_login_info(
            mock_region, mock_repository_id
        )
        assert registry_url == mock_repository_endpoint
        assert username == mock_repository_username
        assert password == mock_repository_password
Exemplo n.º 3
0
def deploy_ec2_service(
    deployment_pb,
    deployment_spec,
    bento_path,
    aws_ec2_deployment_config,
    s3_bucket_name,
    region,
):
    (
        sam_template_name,
        deployment_stack_name,
        repo_name,
        elb_name,
    ) = generate_ec2_resource_names(deployment_pb.namespace,
                                    deployment_pb.name)

    with TempDirectory() as project_path:
        repository_id = create_ecr_repository_if_not_exists(region, repo_name)
        registry_url, username, password = get_ecr_login_info(
            region, repository_id)
        ecr_tag = generate_docker_image_tag(repo_name,
                                            deployment_spec.bento_version,
                                            registry_url)
        build_docker_image(context_path=bento_path, image_tag=ecr_tag)
        push_docker_image_to_repository(repository=ecr_tag,
                                        username=username,
                                        password=password)

        logger.info("Generating user data")
        encoded_user_data = _make_user_data(registry_url, ecr_tag, region)

        logger.info("Making template")
        template_file_path = _make_cloudformation_template(
            project_path,
            encoded_user_data,
            s3_bucket_name,
            sam_template_name,
            elb_name,
            aws_ec2_deployment_config.ami_id,
            aws_ec2_deployment_config.instance_type,
            aws_ec2_deployment_config.autoscale_min_size,
            aws_ec2_deployment_config.autoscale_desired_capacity,
            aws_ec2_deployment_config.autoscale_max_size,
        )
        validate_sam_template(sam_template_name,
                              aws_ec2_deployment_config.region, project_path)

        logger.info("Building service")
        build_template(template_file_path, project_path,
                       aws_ec2_deployment_config.region)

        logger.info("Packaging service")
        package_template(s3_bucket_name, project_path,
                         aws_ec2_deployment_config.region)

        logger.info("Deploying service")
        deploy_template(
            deployment_stack_name,
            s3_bucket_name,
            project_path,
            aws_ec2_deployment_config.region,
        )
Exemplo n.º 4
0
def deploy_ec2_service(
    deployment_pb,
    deployment_spec,
    bento_path,
    aws_ec2_deployment_config,
    s3_bucket_name,
    region,
):
    (
        sam_template_name,
        deployment_stack_name,
        repo_name,
        elb_name,
    ) = generate_ec2_resource_names(deployment_pb.namespace,
                                    deployment_pb.name)

    with TempDirectory() as project_path:
        repository_id = create_ecr_repository_if_not_exists(region, repo_name)
        repository_url, username, password = get_ecr_login_info(
            region, repository_id)

        registry_domain = repository_url.replace("https://", "")
        push_tag = f"{registry_domain}/{repo_name}"
        pull_tag = push_tag + f":{deployment_spec.bento_version}"

        logger.info("Containerizing service")
        containerize_bento_service(
            bento_name=deployment_spec.bento_name,
            bento_version=deployment_spec.bento_version,
            saved_bundle_path=bento_path,
            push=True,
            tag=push_tag,
            build_arg={},
            username=username,
            password=password,
        )

        logger.info("Generating user data")
        encoded_user_data = _make_user_data(repository_url, pull_tag, region)

        logger.info("Making template")
        template_file_path = _make_cloudformation_template(
            project_path,
            encoded_user_data,
            s3_bucket_name,
            sam_template_name,
            elb_name,
            aws_ec2_deployment_config.ami_id,
            aws_ec2_deployment_config.instance_type,
            aws_ec2_deployment_config.autoscale_min_size,
            aws_ec2_deployment_config.autoscale_desired_capacity,
            aws_ec2_deployment_config.autoscale_max_size,
        )
        validate_sam_template(sam_template_name,
                              aws_ec2_deployment_config.region, project_path)

        logger.info("Building service")
        build_template(template_file_path, project_path,
                       aws_ec2_deployment_config.region)

        logger.info("Packaging service")
        package_template(s3_bucket_name, project_path,
                         aws_ec2_deployment_config.region)

        logger.info("Deploying service")
        deploy_template(
            deployment_stack_name,
            s3_bucket_name,
            project_path,
            aws_ec2_deployment_config.region,
        )