{ "cells": [ { "cell_type": "markdown", "id": "b0f986a2", "metadata": {}, "source": [ "### Environment Preparation " ] }, { "cell_type": "markdown", "id": "b40d3b9c", "metadata": {}, "source": [ "* prepare session and bucket \n", "* prepare necessary parameters " ] }, { "cell_type": "code", "execution_count": null, "id": "a7033181", "metadata": {}, "outputs": [], "source": [ "import sagemaker \n", "import boto3 \n", "sess = sagemaker.Session()\n", "BUCKET = sess.default_bucket()" ] }, { "cell_type": "code", "execution_count": null, "id": "175467db", "metadata": {}, "outputs": [], "source": [ "BUCKET" ] }, { "cell_type": "code", "execution_count": null, "id": "42bb3606", "metadata": {}, "outputs": [], "source": [ "client = boto3.client(\"sts\")\n", "account_id = client.get_caller_identity()[\"Account\"]\n" ] }, { "cell_type": "code", "execution_count": null, "id": "4b6adb57", "metadata": {}, "outputs": [], "source": [ "!ls \n" ] }, { "cell_type": "markdown", "id": "9353e8bd", "metadata": {}, "source": [ "### Setup lambda functions and SQS \n", "\n", "\n", "We use a lambda function ```process_a2i_results``` to \n", "1. listening to SQS and process a2i results \n", "2. trigger retraining pipelines \n", "\n", "The following steps creates lambda functions \n", "\n", "* copying codes to s3 \n", "* create lambda functions by codes on s3 \n", "* setup environment variables \n" ] }, { "cell_type": "code", "execution_count": null, "id": "01362829", "metadata": {}, "outputs": [], "source": [ "%%bash -s \"$BUCKET\" \n", "cd ./process_a2i_results\n", "zip -r process_a2i_results.zip .\n", "aws s3 cp process_a2i_results.zip s3://$1/lambda/" ] }, { "cell_type": "code", "execution_count": null, "id": "a43d6f46", "metadata": { "scrolled": true }, "outputs": [], "source": [ "%store -r lambda_role_arn " ] }, { "cell_type": "code", "execution_count": null, "id": "021c7e0e", "metadata": {}, "outputs": [], "source": [ "lambda_role_arn" ] }, { "cell_type": "code", "execution_count": null, "id": "89092c07", "metadata": {}, "outputs": [], "source": [ "import os\n", "cwd = os.getcwd()\n", "!aws lambda create-function --function-name process_a2i_results --zip-file fileb://$cwd/process_a2i_results/process_a2i_results.zip --handler lambda_function.lambda_handler --runtime python3.7 --role $lambda_role_arn" ] }, { "cell_type": "markdown", "id": "c9a7fc24", "metadata": {}, "source": [ "#### Configure lambda function - process_a2i_results\n", "![configure environment variable](./content_image/setup_env_vars_for_lambda3.png)" ] }, { "cell_type": "code", "execution_count": null, "id": "c020b594", "metadata": {}, "outputs": [], "source": [ "%store -r model_package_group_name \n", "%store -r pipeline_name" ] }, { "cell_type": "code", "execution_count": null, "id": "a06e6ae0", "metadata": {}, "outputs": [], "source": [ "bucket_key = \"a2i-demo\"\n", "variables = f\"MODEL_GROUP={model_package_group_name},BUCKET={BUCKET},PIPELINE={pipeline_name}\"\n", "env = \"Variables={\"+variables+\"}\"\n", "\n", "!aws lambda update-function-configuration --function-name process_a2i_results --environment \"$env\"" ] }, { "cell_type": "markdown", "id": "deb3bcad", "metadata": {}, "source": [ "### Set up SQS \n", "\n", "* we use SQS to pass the information between lambda functions in this solution \n", "* set up SQS and attach it as a trigger to a lambda function \n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "fc1c4793", "metadata": {}, "outputs": [], "source": [ "!aws lambda create-event-source-mapping --function-name process_a2i_results --batch-size 10 \\\n", "--maximum-batching-window-in-seconds 60 --event-source-arn arn:aws:sqs:us-west-2:$account_id:a2itasks \n" ] }, { "cell_type": "code", "execution_count": null, "id": "8434226b", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "conda_python3", "language": "python", "name": "conda_python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.13" } }, "nbformat": 4, "nbformat_minor": 5 }