In this lab we will provision an Amazon API Gateway and the AWS Lambda functions that will help integrate Snowflake with SageMaker. We will be using the Serverless Framework to help us programmatically define and provision this infrastructure.
-
First we need to create an AWS IAM role that will let SageMaker training jobs to access S3, ECR and other needed services on your behalf.
Sign in to the AWS Management Console and open the IAM console at https://console.aws.amazon.com/iam/.
In the left navigation pane, choose Roles.
-
Choose Create role. For role type, choose AWS Service, find and choose SageMaker, and then choose the SageMaker - Execution use case. Then choose Next: Permissions.
-
On the Attach permissions policy page, choose these managed policies:
AmazonSageMakerFullAccess
. -
Choose Next: Review and create the role. After creation, click back on the role you just created and click on the permissions tab
-
Click on Attach Policies, search for
SSM
and pick theAmazonSSMReadOnlyAccess
policy by checking the checkbox next to it and click on "Attach Policy" -
Copy the
Role ARN
value. -
In the IDE, open the
config.dev.yml
file within thesls
folder and paste in theRole ARN
value you copied in the last step forsagemaker_role_arn
variable. -
Paste in the value you noted down in the last step of LAB 2 for the
training_image_ecr_path
(this is the Docker image you pushed up to ECR). -
Change the
region
if you are deploying to a different region. Save theconfig.dev.yml
file.
Now we need to create a role in your AWS account that Snowflake can assume and invoke the API Gateway.
-
Create a new AWS IAM role: https://console.aws.amazon.com/iam/.
-
When asked to select the type of trusted entity, choose “Another AWS account”.
-
When asked to “Specify accounts that can use this role”, paste in your AWS account ID. Your AWS account ID is the number embedded in any of the ARNs we noted down in previous steps.
-
Click on “Next: Permissions”. No permissions are needed right now so just proceed to the next step.
-
Enter a role name. Record the role name as the
Snowflake External Function Role Name
. After you create the role: Record theRole ARN
as theSnowflake External Function Role ARN
. -
Go back to the
config.dev.yml
file and fill the value for thesnf_ef_role_arn
variable withRole ARN
you captured in the last step. -
In
config.dev.yml
, change the value forunique_id
to your unique username. This is required to make sure the S3 bucket name is unique for your deployment. -
Fill out the
snf_ef_role_principal
with a value using this format:arn:aws:sts::<12-digit-number>:assumed-role/<external_function_role>/snowflake
Your
config.dev.yml
file should now look something like this:
With the blanks filled out in the config.dev.yml
file, now its time to deploy the API Gateway and Lambda Functions.
-
Go to the terminal/command line window, switch to the
sls
directory and issue the following command to deploy the infrastructure:sls deploy
If everything goes smoothly, you should see an output summary listing out the resources created by the Serverless Framework in your AWS account:
Now that we have our Serverless infrastructure deployed, lets move over to the Snowflake UI and work on some SQL magic! 🧙🏼♀️
-
Log into the the Snowflake UI. We need to create API Intagrations and for this we need to work as an
ACCOUNTADMIN
:use role ACCOUNTADMIN;
Also select the DB and schema for which you want to create this External Functions (I'm using TEST.PUBLIC):
use schema MOVIELENS.PUBLIC;
-
Let's create the API integration object within Snowflake that will point to external API Gateway resource:
create or replace api integration snf_recommender_api_integration api_provider = aws_api_gateway api_aws_role_arn = '<snf_ef_role_arn>' enabled = true api_allowed_prefixes = ('<https://api_endpoint_url>');
Grab the value of
snf_ef_role_arn
from theconfig.deg.yml
file. Get the endpoint URL from the Serverless output screen as the value for theapi_allowed_prefixes
field as highlighted in the screenshot below:Run the SQL above with the two values filled in to create the API integration object.
-
Now we need to setup trust between Snowflake and new AWS IAM role that we created earlier. Go ahead and run:
describe integration snf_recommender_api_integration
-
In a separate tab, open the AWS console and navigate to find the IAM role you created earlier. Under the
Trust relationships
click onEdit trust relationship
.Replace the JSON document with this one:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": "<API_AWS_IAM_USER_ARN>" }, "Action": "sts:AssumeRole", "Condition": {"StringEquals": { "sts:ExternalId": "<API_AWS_EXTERNAL_ID>" }} } ] }
And then replace the values of
API_AWS_IAM_USER_ARN
andAPI_AWS_EXTERNAL_ID
with the corresponding values shown in the result of thedescribe api integration
command in the last step.Click
Update Trust Policy