External network access examples¶
This topic provides examples of accessing external network locations from user-defined functions and procedures.
Accessing PyPi to install packages in Snowpark Container¶
You can access the PyPi package repository by creating an external access integration.
You might do this when you want to allow Notebook users on Container Runtime to install pip
packages using the pip install command. With this kind of integration, you can also
allow Snowpark Container Services to install pip packages.
This example uses the Snowflake-managed network rule snowflake.external_access.pypi_rule
described in Privileges and commands.
Create an external access integration using the
snowflake.external_access.pypi_rulenetwork rule.Create a
developerrole for users who need to usepip installin a Snowpark Container or Notebook on Container Runtime.Grant to the
developerrole the privileges needed to use the external access integration you created.
Accessing the Google Translate API with OAuth¶
The following steps include code to create an external access integration for access to the Google Translation API. The steps add the security integration and the permissions needed to execute the statements.
Create a network rule representing the external location.
For more information about the role of a network rule in external access, including privileges required, see Creating a network rule to represent the external network location.
Create a security integration to hold the OAuth credentials required to authenticate with the external network location specified by the
google_apis_network_rulenetwork rule.For reference information on the command, including privileges required, see CREATE SECURITY INTEGRATION (External API Authentication).
Create a secret to represent the credentials contained by the
google_translate_oauthsecurity integration.For more information about the role of the secret in external access, including privileges required, see Creating a secret to represent credentials.
The secret must specify a refresh token with its OAUTH_REFRESH_TOKEN parameter. To obtain a refresh token from the service provider (in this case, from the Google Cloud Translation API service), you can use a way the provider offers or use Snowflake system functions.
To create a secret with a refresh token, use either Google OAuth Playground or Snowflake system functions, as described by the following:
Snowflake system functions
Execute CREATE SECRET to create a secret. You’ll update it with the refresh token in a later step.
Execute the SYSTEM$START_OAUTH_FLOW function to retrieve a URL with which you can obtain a refresh token, specifying as its argument the name of the secret you created previously.
The function generates a URL you can use to complete the OAuth consent process.
In a browser, visit the generated URL and complete the OAuth2 consent process. When you’ve finished, leave the browser open to the last page of the process.
From the browser address bar, copy all of the text after the question mark in the URL of the last page of the consent process.
Execute the SYSTEM$FINISH_OAUTH_FLOW function, specifying as an argument the parameters you just copied from the browser address bar to update the secret with a refresh token.
Be sure to execute SYSTEM$FINISH_OAUTH_FLOW in the same session as SYSTEM$START_OAUTH_FLOW. SYSTEM$FINISH_OAUTH_FLOW updates the secret you specified in SYSTEM$START_OAUTH_FLOW with access token and refresh token it obtained from the OAuth server.
Google OAuth Playground
In Google OAuth Playground, select and authorize the Cloud Translation API as specified in step 1.
In Step 2, click exchange authorization code for tokens, then copy the refresh token token value.
Execute CREATE SECRET to create a secret that specifies the refresh token value you copied.
For more information about the role of a secret in external access, including privileges required, see Creating a secret to represent credentials.
Create an external access integration using the network rule and secret.
For more information about the role of an external access integration, including privileges required, see Creating an external access integration.
Create a
developerrole that will be assigned to users who need to create a UDF or procedure that uses the integration.Grant to the
developerrole privileges needed to create a UDF that uses the objects for external access. This includes the following:The READ privilege on the secret.
The USAGE privilege on the schema containing the secret.
The USAGE privilege on the integration.
Create a UDF
google_translate_pythonthat translates the specified text into a phrase in the specified language. For more information, see Using the external access integration in a function or procedure.Grant the USAGE privilege on the
google_translate_pythonfunction so that those with theuserrole can call it.Execute the
google_translate_pythonfunction to translate a phrase.This generates the following output.
Accessing an external lambda function with basic authentication¶
The following steps include example code to create an external access integration for access to a lambda function external to Snowflake. The example uses a placeholder for the external endpoint itself, but it could be a function available at a REST service endpoint, for example.
The external access is used in a vectorized Python UDF that receives a Pandas DataFrame containing the data.
Create a network rule
lambda_network_rulerepresenting the external locationmy_external_service(here, a placeholder value for the location of an external endpoint).For more information about the role of a network rule in external access, see Creating a network rule to represent the external network location.
Create a secret to represent credentials required by the external service.
Handler code later in this example retrieves the credentials from the secret using a Snowflake API for Python.
For more information about the role of the secret in external access, see Creating a secret to represent credentials.
Create a
developerrole and grant to it READ privileges on the secret. This role will be assigned to users who need to create a UDF or procedure that uses the secret.Also, create the role that users will use to call the function.
Grant to the
developerrole privileges needed to create a UDF that uses the objects for external access. This includes the following:The READ privilege on the secret.
The USAGE privilege on the schema containing the secret.
Create an external access integration to specify the external endpoint and credentials through the network rule and secret you created.
For more information about the role of an external access integration, including privileges required, see Creating an external access integration.
Create a vectorized Python UDF
return_double_columnthat accesses an external network location to process data received as a Pandas DataFrame.For more information on using external access in a UDF, see Using the external access integration in a function or procedure.
Grant the USAGE privilege on the
return_double_columnfunction so that those with theuserrole can call it.Execute the
return_double_columnfunction, making a request to the external endpoint.Code in the following example creates a two-column table and inserts 100,000,000 rows containing 4-byte integers. The code then executes the
return_double_columnfunction, passing values from columnafor processing by the external endpoint.
Accessing Amazon S3 with AWS IAM¶
The following steps include example code to connect to an AWS S3 bucket using IAM.
For more information about AWS IAM, see AWS IAM documentation.
Create a network rule,
aws_s3_network_rule, that represents the AWS S3 bucket at the location specified by the VALUE_LIST parameter.For more information about the role of a network rule in external access, see Creating a network rule to represent the external network location.
Create a security integration to hold the AWS IAM Amazon Resource Name (ARN) credentials required to authenticate with the external network location specified by the
aws_s3_network_rulenetwork rule.For reference information on the command, including privileges required, see CREATE SECURITY INTEGRATION (AWS IAM Authentication).
Get the ARN and ID for the IAM USER.
Execute the DESC command on the security integration you created.
From the output displayed, copy the values of the following properties to use in the next step:
API_AWS_IAM_USER_ARN
API_AWS_EXTERNAL_ID
Grant the IAM user permissions needed to access the bucket.
Use the ARN and ID values when configuring a trust policy as described in Step 5 of Option 1: Configure a Snowflake storage integration to access Amazon S3.
Create a secret of type CLOUD_PROVIDER_TOKEN to represent credentials required by the external service.
Handler code later in this example retrieves the credentials from the secret using a Snowflake API.
For more information about the role of the secret in external access, see Creating a secret to represent credentials.
Create a
developerrole and grant to it READ privileges on the secret. This role will be assigned to users who need to create a UDF or procedure that uses the secret.Also, create the role that users will use to call the function.
Grant to the
developerrole the privileges needed to create a UDF that uses the objects for external access. This includes the following:The READ privilege on the secret.
The USAGE privilege on the schema containing the secret.
Create an external access integration to specify the external endpoint and credentials through the network rule and secret you created.
For more information about the role of an external access integration, including privileges required, see Creating an external access integration.
Create a UDF that uses the external access integration to connect with the Amazon S3 bucket specified in the network rule you created.
The handler code uses Snowflake APIs to retrieve a token from the secret you created. From this token, you can use Snowflake APIs to retrieve values needed to create a session for connecting with Amazon S3, including an access key ID, secret access key, and session token.
For more information on using external access in a UDF, see Using the external access integration in a function or procedure.
Grant the USAGE privilege on the UDF so that those with the
userrole can call it.Execute the function to connect to the external endpoint.