SageMaker esempi di utilizzo di for Kotlin SDK - Esempi di codice dell'AWS SDK

Ci sono altri AWS SDK esempi disponibili nel repository AWS Doc SDK Examples GitHub .

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

SageMaker esempi di utilizzo di for Kotlin SDK

I seguenti esempi di codice mostrano come eseguire azioni e implementare scenari comuni utilizzando AWS SDK for Kotlin with. SageMaker

Le operazioni sono estratti di codice da programmi più grandi e devono essere eseguite nel contesto. Sebbene le azioni mostrino come richiamare le singole funzioni di servizio, puoi vedere le azioni nel loro contesto negli scenari correlati.

Gli scenari sono esempi di codice che mostrano come eseguire attività specifiche richiamando più funzioni all'interno di un servizio o combinandole con altre AWS servizi.

Ogni esempio include un collegamento al codice sorgente completo, in cui è possibile trovare istruzioni su come configurare ed eseguire il codice nel contesto.

Nozioni di base

I seguenti esempi di codice mostrano come iniziare a utilizzare SageMaker.

SDKper Kotlin
Nota

c'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

suspend fun listBooks() { SageMakerClient { region = "us-west-2" }.use { sageMakerClient -> val response = sageMakerClient.listNotebookInstances(ListNotebookInstancesRequest {}) response.notebookInstances?.forEach { item -> println("The notebook name is: ${item.notebookInstanceName}") } } }
Argomenti

Azioni

Il seguente esempio di codice mostra come usare. CreatePipeline

SDKper Kotlin
Nota

c'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

// Create a pipeline from the example pipeline JSON. suspend fun setupPipeline(filePath: String?, roleArnVal: String?, functionArnVal: String?, pipelineNameVal: String?) { println("Setting up the pipeline.") val parser = JSONParser() // Read JSON and get pipeline definition. FileReader(filePath).use { reader -> val obj: Any = parser.parse(reader) val jsonObject: JSONObject = obj as JSONObject val stepsArray: JSONArray = jsonObject.get("Steps") as JSONArray for (stepObj in stepsArray) { val step: JSONObject = stepObj as JSONObject if (step.containsKey("FunctionArn")) { step.put("FunctionArn", functionArnVal) } } println(jsonObject) // Create the pipeline. val pipelineRequest = CreatePipelineRequest { pipelineDescription = "Kotlin SDK example pipeline" roleArn = roleArnVal pipelineName = pipelineNameVal pipelineDefinition = jsonObject.toString() } SageMakerClient { region = "us-west-2" }.use { sageMakerClient -> sageMakerClient.createPipeline(pipelineRequest) } } }

Il seguente esempio di codice mostra come usare. DeletePipeline

SDKper Kotlin
Nota

c'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

// Delete a SageMaker pipeline by name. suspend fun deletePipeline(pipelineNameVal: String) { val pipelineRequest = DeletePipelineRequest { pipelineName = pipelineNameVal } SageMakerClient { region = "us-west-2" }.use { sageMakerClient -> sageMakerClient.deletePipeline(pipelineRequest) println("*** Successfully deleted $pipelineNameVal") } }

Il seguente esempio di codice mostra come usare. DescribePipelineExecution

SDKper Kotlin
Nota

c'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

suspend fun waitForPipelineExecution(executionArn: String?) { var status: String var index = 0 do { val pipelineExecutionRequest = DescribePipelineExecutionRequest { pipelineExecutionArn = executionArn } SageMakerClient { region = "us-west-2" }.use { sageMakerClient -> val response = sageMakerClient.describePipelineExecution(pipelineExecutionRequest) status = response.pipelineExecutionStatus.toString() println("$index. The status of the pipeline is $status") TimeUnit.SECONDS.sleep(4) index++ } } while ("Executing" == status) println("Pipeline finished with status $status") }

Il seguente esempio di codice mostra come usare. StartPipelineExecution

SDKper Kotlin
Nota

c'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

// Start a pipeline run with job configurations. suspend fun executePipeline(bucketName: String, queueUrl: String?, roleArn: String?, pipelineNameVal: String): String? { println("Starting pipeline execution.") val inputBucketLocation = "s3://$bucketName/samplefiles/latlongtest.csv" val output = "s3://$bucketName/outputfiles/" val gson = GsonBuilder() .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) .setPrettyPrinting() .create() // Set up all parameters required to start the pipeline. val parameters: MutableList<Parameter> = java.util.ArrayList<Parameter>() val para1 = Parameter { name = "parameter_execution_role" value = roleArn } val para2 = Parameter { name = "parameter_queue_url" value = queueUrl } val inputJSON = """{ "DataSourceConfig": { "S3Data": { "S3Uri": "s3://$bucketName/samplefiles/latlongtest.csv" }, "Type": "S3_DATA" }, "DocumentType": "CSV" }""" println(inputJSON) val para3 = Parameter { name = "parameter_vej_input_config" value = inputJSON } // Create an ExportVectorEnrichmentJobOutputConfig object. val jobS3Data = VectorEnrichmentJobS3Data { s3Uri = output } val outputConfig = ExportVectorEnrichmentJobOutputConfig { s3Data = jobS3Data } val gson4: String = gson.toJson(outputConfig) val para4: Parameter = Parameter { name = "parameter_vej_export_config" value = gson4 } println("parameter_vej_export_config:" + gson.toJson(outputConfig)) val para5JSON = "{\"MapMatchingConfig\":null,\"ReverseGeocodingConfig\":{\"XAttributeName\":\"Longitude\",\"YAttributeName\":\"Latitude\"}}" val para5: Parameter = Parameter { name = "parameter_step_1_vej_config" value = para5JSON } parameters.add(para1) parameters.add(para2) parameters.add(para3) parameters.add(para4) parameters.add(para5) val pipelineExecutionRequest = StartPipelineExecutionRequest { pipelineExecutionDescription = "Created using Kotlin SDK" pipelineExecutionDisplayName = "$pipelineName-example-execution" pipelineParameters = parameters pipelineName = pipelineNameVal } SageMakerClient { region = "us-west-2" }.use { sageMakerClient -> val response = sageMakerClient.startPipelineExecution(pipelineExecutionRequest) return response.pipelineExecutionArn } }

Scenari

L'esempio di codice seguente mostra come:

  • Imposta le risorse per una pipeline.

  • Configura una pipeline che esegua un lavoro geospaziale.

  • Avvio dell'esecuzione di una pipeline.

  • Monitora lo stato dell'esecuzione.

  • Visualizza l'output della pipeline.

  • Pulisci le risorse.

Per ulteriori informazioni, consulta Creare ed eseguire SageMaker pipeline utilizzando AWS SDKs Community.aws.

SDKper Kotlin
Nota

c'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

val DASHES = String(CharArray(80)).replace("\u0000", "-") private var eventSourceMapping = "" suspend fun main(args: Array<String>) { val usage = """ Usage: <sageMakerRoleName> <lambdaRoleName> <functionName> <functionKey> <queueName> <bucketName> <bucketFunction> <lnglatData> <spatialPipelinePath> <pipelineName> Where: sageMakerRoleName - The name of the Amazon SageMaker role. lambdaRoleName - The name of the AWS Lambda role. functionName - The name of the AWS Lambda function (for example,SageMakerExampleFunction). functionKey - The name of the Amazon S3 key name that represents the Lambda function (for example, SageMakerLambda.zip). queueName - The name of the Amazon Simple Queue Service (Amazon SQS) queue. bucketName - The name of the Amazon Simple Storage Service (Amazon S3) bucket. bucketFunction - The name of the Amazon S3 bucket that contains the Lambda ZIP file. lnglatData - The file location of the latlongtest.csv file required for this use case. spatialPipelinePath - The file location of the GeoSpatialPipeline.json file required for this use case. pipelineName - The name of the pipeline to create (for example, sagemaker-sdk-example-pipeline). """ if (args.size != 10) { println(usage) exitProcess(1) } val sageMakerRoleName = args[0] val lambdaRoleName = args[1] val functionKey = args[2] val functionName = args[3] val queueName = args[4] val bucketName = args[5] val bucketFunction = args[6] val lnglatData = args[7] val spatialPipelinePath = args[8] val pipelineName = args[9] val handlerName = "org.example.SageMakerLambdaFunction::handleRequest" println(DASHES) println("Welcome to the Amazon SageMaker pipeline example scenario.") println( """ This example workflow will guide you through setting up and running an Amazon SageMaker pipeline. The pipeline uses an AWS Lambda function and an Amazon SQS Queue. It runs a vector enrichment reverse geocode job to reverse geocode addresses in an input file and store the results in an export file. """.trimIndent(), ) println(DASHES) println(DASHES) println("First, we will set up the roles, functions, and queue needed by the SageMaker pipeline.") val lambdaRoleArn: String = checkLambdaRole(lambdaRoleName) val sageMakerRoleArn: String = checkSageMakerRole(sageMakerRoleName) val functionArn = checkFunction(functionName, bucketFunction, functionKey, handlerName, lambdaRoleArn) val queueUrl = checkQueue(queueName, functionName) println(DASHES) println(DASHES) println("Setting up bucket $bucketName") if (!checkBucket(bucketName)) { setupBucket(bucketName) println("Put $lnglatData into $bucketName") val objectKey = "samplefiles/latlongtest.csv" putS3Object(bucketName, objectKey, lnglatData) } println(DASHES) println(DASHES) println("Now we can create and run our pipeline.") setupPipeline(spatialPipelinePath, sageMakerRoleArn, functionArn, pipelineName) val pipelineExecutionARN = executePipeline(bucketName, queueUrl, sageMakerRoleArn, pipelineName) println("The pipeline execution ARN value is $pipelineExecutionARN") waitForPipelineExecution(pipelineExecutionARN) println("Wait 30 secs to get output results $bucketName") TimeUnit.SECONDS.sleep(30) getOutputResults(bucketName) println(DASHES) println(DASHES) println( """ The pipeline has completed. To view the pipeline and runs in SageMaker Studio, follow these instructions: https://docs.aws.amazon.com/sagemaker/latest/dg/pipelines-studio.html """.trimIndent(), ) println(DASHES) println(DASHES) println("Do you want to delete the AWS resources used in this Workflow? (y/n)") val `in` = Scanner(System.`in`) val delResources = `in`.nextLine() if (delResources.compareTo("y") == 0) { println("Lets clean up the AWS resources. Wait 30 seconds") TimeUnit.SECONDS.sleep(30) deleteEventSourceMapping(functionName) deleteSQSQueue(queueName) listBucketObjects(bucketName) deleteBucket(bucketName) delLambdaFunction(functionName) deleteLambdaRole(lambdaRoleName) deleteSagemakerRole(sageMakerRoleName) deletePipeline(pipelineName) } else { println("The AWS Resources were not deleted!") } println(DASHES) println(DASHES) println("SageMaker pipeline scenario is complete.") println(DASHES) } // Delete a SageMaker pipeline by name. suspend fun deletePipeline(pipelineNameVal: String) { val pipelineRequest = DeletePipelineRequest { pipelineName = pipelineNameVal } SageMakerClient { region = "us-west-2" }.use { sageMakerClient -> sageMakerClient.deletePipeline(pipelineRequest) println("*** Successfully deleted $pipelineNameVal") } } suspend fun deleteSagemakerRole(roleNameVal: String) { val sageMakerRolePolicies = getSageMakerRolePolicies() IamClient { region = "us-west-2" }.use { iam -> for (policy in sageMakerRolePolicies) { // First the policy needs to be detached. val rolePolicyRequest = DetachRolePolicyRequest { policyArn = policy roleName = roleNameVal } iam.detachRolePolicy(rolePolicyRequest) } // Delete the role. val roleRequest = DeleteRoleRequest { roleName = roleNameVal } iam.deleteRole(roleRequest) println("*** Successfully deleted $roleNameVal") } } suspend fun deleteLambdaRole(roleNameVal: String) { val lambdaRolePolicies = getLambdaRolePolicies() IamClient { region = "us-west-2" }.use { iam -> for (policy in lambdaRolePolicies) { // First the policy needs to be detached. val rolePolicyRequest = DetachRolePolicyRequest { policyArn = policy roleName = roleNameVal } iam.detachRolePolicy(rolePolicyRequest) } // Delete the role. val roleRequest = DeleteRoleRequest { roleName = roleNameVal } iam.deleteRole(roleRequest) println("*** Successfully deleted $roleNameVal") } } suspend fun delLambdaFunction(myFunctionName: String) { val request = DeleteFunctionRequest { functionName = myFunctionName } LambdaClient { region = "us-west-2" }.use { awsLambda -> awsLambda.deleteFunction(request) println("$myFunctionName was deleted") } } suspend fun deleteBucket(bucketName: String?) { val request = DeleteBucketRequest { bucket = bucketName } S3Client { region = "us-east-1" }.use { s3 -> s3.deleteBucket(request) println("The $bucketName was successfully deleted!") } } suspend fun deleteBucketObjects(bucketName: String, objectName: String?) { val toDelete = ArrayList<ObjectIdentifier>() val obId = ObjectIdentifier { key = objectName } toDelete.add(obId) val delOb = Delete { objects = toDelete } val dor = DeleteObjectsRequest { bucket = bucketName delete = delOb } S3Client { region = "us-east-1" }.use { s3Client -> s3Client.deleteObjects(dor) println("*** $bucketName objects were deleted.") } } suspend fun listBucketObjects(bucketNameVal: String) { val listObjects = ListObjectsRequest { bucket = bucketNameVal } S3Client { region = "us-east-1" }.use { s3Client -> val res = s3Client.listObjects(listObjects) val objects = res.contents if (objects != null) { for (myValue in objects) { println("The name of the key is ${myValue.key}") deleteBucketObjects(bucketNameVal, myValue.key) } } } } // Delete the specific Amazon SQS queue. suspend fun deleteSQSQueue(queueNameVal: String?) { val getQueueRequest = GetQueueUrlRequest { queueName = queueNameVal } SqsClient { region = "us-west-2" }.use { sqsClient -> val urlVal = sqsClient.getQueueUrl(getQueueRequest).queueUrl val deleteQueueRequest = DeleteQueueRequest { queueUrl = urlVal } sqsClient.deleteQueue(deleteQueueRequest) } } // Delete the queue event mapping. suspend fun deleteEventSourceMapping(functionNameVal: String) { if (eventSourceMapping.compareTo("") == 0) { LambdaClient { region = "us-west-2" }.use { lambdaClient -> val request = ListEventSourceMappingsRequest { functionName = functionNameVal } val response = lambdaClient.listEventSourceMappings(request) val eventList = response.eventSourceMappings if (eventList != null) { for (event in eventList) { eventSourceMapping = event.uuid.toString() } } } } val eventSourceMappingRequest = DeleteEventSourceMappingRequest { uuid = eventSourceMapping } LambdaClient { region = "us-west-2" }.use { lambdaClient -> lambdaClient.deleteEventSourceMapping(eventSourceMappingRequest) println("The event mapping is deleted!") } } // Reads the objects in the S3 bucket and displays the values. private suspend fun readObject(bucketName: String, keyVal: String?) { println("Output file contents: \n") val objectRequest = GetObjectRequest { bucket = bucketName key = keyVal } S3Client { region = "us-east-1" }.use { s3Client -> s3Client.getObject(objectRequest) { resp -> val byteArray = resp.body?.toByteArray() val text = byteArray?.let { String(it, StandardCharsets.UTF_8) } println("Text output: $text") } } } // Display the results from the output directory. suspend fun getOutputResults(bucketName: String?) { println("Getting output results $bucketName.") val listObjectsRequest = ListObjectsRequest { bucket = bucketName prefix = "outputfiles/" } S3Client { region = "us-east-1" }.use { s3Client -> val response = s3Client.listObjects(listObjectsRequest) val s3Objects: List<Object>? = response.contents if (s3Objects != null) { for (`object` in s3Objects) { if (bucketName != null) { readObject(bucketName, (`object`.key)) } } } } } suspend fun waitForPipelineExecution(executionArn: String?) { var status: String var index = 0 do { val pipelineExecutionRequest = DescribePipelineExecutionRequest { pipelineExecutionArn = executionArn } SageMakerClient { region = "us-west-2" }.use { sageMakerClient -> val response = sageMakerClient.describePipelineExecution(pipelineExecutionRequest) status = response.pipelineExecutionStatus.toString() println("$index. The status of the pipeline is $status") TimeUnit.SECONDS.sleep(4) index++ } } while ("Executing" == status) println("Pipeline finished with status $status") } // Start a pipeline run with job configurations. suspend fun executePipeline(bucketName: String, queueUrl: String?, roleArn: String?, pipelineNameVal: String): String? { println("Starting pipeline execution.") val inputBucketLocation = "s3://$bucketName/samplefiles/latlongtest.csv" val output = "s3://$bucketName/outputfiles/" val gson = GsonBuilder() .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) .setPrettyPrinting() .create() // Set up all parameters required to start the pipeline. val parameters: MutableList<Parameter> = java.util.ArrayList<Parameter>() val para1 = Parameter { name = "parameter_execution_role" value = roleArn } val para2 = Parameter { name = "parameter_queue_url" value = queueUrl } val inputJSON = """{ "DataSourceConfig": { "S3Data": { "S3Uri": "s3://$bucketName/samplefiles/latlongtest.csv" }, "Type": "S3_DATA" }, "DocumentType": "CSV" }""" println(inputJSON) val para3 = Parameter { name = "parameter_vej_input_config" value = inputJSON } // Create an ExportVectorEnrichmentJobOutputConfig object. val jobS3Data = VectorEnrichmentJobS3Data { s3Uri = output } val outputConfig = ExportVectorEnrichmentJobOutputConfig { s3Data = jobS3Data } val gson4: String = gson.toJson(outputConfig) val para4: Parameter = Parameter { name = "parameter_vej_export_config" value = gson4 } println("parameter_vej_export_config:" + gson.toJson(outputConfig)) val para5JSON = "{\"MapMatchingConfig\":null,\"ReverseGeocodingConfig\":{\"XAttributeName\":\"Longitude\",\"YAttributeName\":\"Latitude\"}}" val para5: Parameter = Parameter { name = "parameter_step_1_vej_config" value = para5JSON } parameters.add(para1) parameters.add(para2) parameters.add(para3) parameters.add(para4) parameters.add(para5) val pipelineExecutionRequest = StartPipelineExecutionRequest { pipelineExecutionDescription = "Created using Kotlin SDK" pipelineExecutionDisplayName = "$pipelineName-example-execution" pipelineParameters = parameters pipelineName = pipelineNameVal } SageMakerClient { region = "us-west-2" }.use { sageMakerClient -> val response = sageMakerClient.startPipelineExecution(pipelineExecutionRequest) return response.pipelineExecutionArn } } // Create a pipeline from the example pipeline JSON. suspend fun setupPipeline(filePath: String?, roleArnVal: String?, functionArnVal: String?, pipelineNameVal: String?) { println("Setting up the pipeline.") val parser = JSONParser() // Read JSON and get pipeline definition. FileReader(filePath).use { reader -> val obj: Any = parser.parse(reader) val jsonObject: JSONObject = obj as JSONObject val stepsArray: JSONArray = jsonObject.get("Steps") as JSONArray for (stepObj in stepsArray) { val step: JSONObject = stepObj as JSONObject if (step.containsKey("FunctionArn")) { step.put("FunctionArn", functionArnVal) } } println(jsonObject) // Create the pipeline. val pipelineRequest = CreatePipelineRequest { pipelineDescription = "Kotlin SDK example pipeline" roleArn = roleArnVal pipelineName = pipelineNameVal pipelineDefinition = jsonObject.toString() } SageMakerClient { region = "us-west-2" }.use { sageMakerClient -> sageMakerClient.createPipeline(pipelineRequest) } } } suspend fun putS3Object(bucketName: String, objectKey: String, objectPath: String) { val request = PutObjectRequest { bucket = bucketName key = objectKey body = File(objectPath).asByteStream() } S3Client { region = "us-east-1" }.use { s3 -> s3.putObject(request) println("Successfully placed $objectKey into bucket $bucketName") } } suspend fun setupBucket(bucketName: String) { val request = CreateBucketRequest { bucket = bucketName } S3Client { region = "us-east-1" }.use { s3 -> s3.createBucket(request) println("$bucketName is ready") } } suspend fun checkBucket(bucketName: String): Boolean { try { val headBucketRequest = HeadBucketRequest { bucket = bucketName } S3Client { region = "us-east-1" }.use { s3Client -> s3Client.headBucket(headBucketRequest) println("$bucketName exists") return true } } catch (e: S3Exception) { println("Bucket does not exist") } return false } // Connect the queue to the Lambda function as an event source. suspend fun connectLambda(queueUrlVal: String?, lambdaNameVal: String?) { println("Connecting the Lambda function and queue for the pipeline.") var queueArn = "" // Specify the attributes to retrieve. val atts: MutableList<QueueAttributeName> = ArrayList() atts.add(QueueAttributeName.QueueArn) val attributesRequest = GetQueueAttributesRequest { queueUrl = queueUrlVal attributeNames = atts } SqsClient { region = "us-west-2" }.use { sqsClient -> val response = sqsClient.getQueueAttributes(attributesRequest) val queueAtts = response.attributes if (queueAtts != null) { for ((key, value) in queueAtts) { println("Key = $key, Value = $value") queueArn = value } } } val eventSourceMappingRequest = CreateEventSourceMappingRequest { eventSourceArn = queueArn functionName = lambdaNameVal } LambdaClient { region = "us-west-2" }.use { lambdaClient -> val response1 = lambdaClient.createEventSourceMapping(eventSourceMappingRequest) eventSourceMapping = response1.uuid.toString() println("The mapping between the event source and Lambda function was successful") } } // Set up the SQS queue to use with the pipeline. suspend fun setupQueue(queueNameVal: String, lambdaNameVal: String): String { println("Setting up queue named $queueNameVal") val queueAtt: MutableMap<String, String> = HashMap() queueAtt.put("DelaySeconds", "5") queueAtt.put("ReceiveMessageWaitTimeSeconds", "5") queueAtt.put("VisibilityTimeout", "300") val createQueueRequest = CreateQueueRequest { queueName = queueNameVal attributes = queueAtt } SqsClient { region = "us-west-2" }.use { sqsClient -> sqsClient.createQueue(createQueueRequest) println("\nGet queue url") val getQueueUrlResponse = sqsClient.getQueueUrl(GetQueueUrlRequest { queueName = queueNameVal }) TimeUnit.SECONDS.sleep(15) connectLambda(getQueueUrlResponse.queueUrl, lambdaNameVal) println("Queue ready with Url " + getQueueUrlResponse.queueUrl) return getQueueUrlResponse.queueUrl.toString() } } // Checks to see if the Amazon SQS queue exists. If not, this method creates a new queue // and returns the ARN value. suspend fun checkQueue(queueNameVal: String, lambdaNameVal: String): String? { println("Checking to see if the queue exists. If not, a new queue will be created for use in this workflow.") var queueUrl: String try { val request = GetQueueUrlRequest { queueName = queueNameVal } SqsClient { region = "us-west-2" }.use { sqsClient -> val response = sqsClient.getQueueUrl(request) queueUrl = response.queueUrl.toString() println(queueUrl) } } catch (e: SqsException) { println(e.message + " A new queue will be created") queueUrl = setupQueue(queueNameVal, lambdaNameVal) } return queueUrl } suspend fun createNewFunction(myFunctionName: String, s3BucketName: String, myS3Key: String, myHandler: String, myRole: String): String { val functionCode = FunctionCode { s3Bucket = s3BucketName s3Key = myS3Key } val request = CreateFunctionRequest { functionName = myFunctionName code = functionCode description = "Created by the Lambda Kotlin API" handler = myHandler role = myRole runtime = Runtime.Java11 memorySize = 1024 timeout = 200 } LambdaClient { region = "us-west-2" }.use { awsLambda -> val functionResponse = awsLambda.createFunction(request) awsLambda.waitUntilFunctionActive { functionName = myFunctionName } println("${functionResponse.functionArn} was created") return functionResponse.functionArn.toString() } } suspend fun checkFunction(myFunctionName: String, s3BucketName: String, myS3Key: String, myHandler: String, myRole: String): String { println("Checking to see if the function exists. If not, a new AWS Lambda function will be created for use in this workflow.") var functionArn: String try { // Does this function already exist. val functionRequest = GetFunctionRequest { functionName = myFunctionName } LambdaClient { region = "us-west-2" }.use { lambdaClient -> val response = lambdaClient.getFunction(functionRequest) functionArn = response.configuration?.functionArn.toString() println("$functionArn exists") } } catch (e: LambdaException) { println(e.message + " A new function will be created") functionArn = createNewFunction(myFunctionName, s3BucketName, myS3Key, myHandler, myRole) } return functionArn } // Checks to see if the SageMaker role exists. If not, this method creates it. suspend fun checkSageMakerRole(roleNameVal: String): String { println("Checking to see if the role exists. If not, a new role will be created for AWS SageMaker to use.") var roleArn: String try { val roleRequest = GetRoleRequest { roleName = roleNameVal } IamClient { region = "AWS_GLOBAL" }.use { iamClient -> val response = iamClient.getRole(roleRequest) roleArn = response.role?.arn.toString() println(roleArn) } } catch (e: IamException) { println(e.message + " A new role will be created") roleArn = createSageMakerRole(roleNameVal) } return roleArn } suspend fun createSageMakerRole(roleNameVal: String): String { val sageMakerRolePolicies = getSageMakerRolePolicies() println("Creating a role to use with SageMaker.") val assumeRolePolicy = "{" + "\"Version\": \"2012-10-17\"," + "\"Statement\": [{" + "\"Effect\": \"Allow\"," + "\"Principal\": {" + "\"Service\": [" + "\"sagemaker.amazonaws.com\"," + "\"sagemaker-geospatial.amazonaws.com\"," + "\"lambda.amazonaws.com\"," + "\"s3.amazonaws.com\"" + "]" + "}," + "\"Action\": \"sts:AssumeRole\"" + "}]" + "}" val request = CreateRoleRequest { roleName = roleNameVal assumeRolePolicyDocument = assumeRolePolicy description = "Created using the AWS SDK for Kotlin" } IamClient { region = "AWS_GLOBAL" }.use { iamClient -> val roleResult = iamClient.createRole(request) // Attach the policies to the role. for (policy in sageMakerRolePolicies) { val attachRequest = AttachRolePolicyRequest { roleName = roleNameVal policyArn = policy } iamClient.attachRolePolicy(attachRequest) } // Allow time for the role to be ready. TimeUnit.SECONDS.sleep(15) System.out.println("Role ready with ARN ${roleResult.role?.arn}") return roleResult.role?.arn.toString() } } // Checks to see if the Lambda role exists. If not, this method creates it. suspend fun checkLambdaRole(roleNameVal: String): String { println("Checking to see if the role exists. If not, a new role will be created for AWS Lambda to use.") var roleArn: String val roleRequest = GetRoleRequest { roleName = roleNameVal } try { IamClient { region = "AWS_GLOBAL" }.use { iamClient -> val response = iamClient.getRole(roleRequest) roleArn = response.role?.arn.toString() println(roleArn) } } catch (e: IamException) { println(e.message + " A new role will be created") roleArn = createLambdaRole(roleNameVal) } return roleArn } private suspend fun createLambdaRole(roleNameVal: String): String { val lambdaRolePolicies = getLambdaRolePolicies() val assumeRolePolicy = "{" + "\"Version\": \"2012-10-17\"," + "\"Statement\": [{" + "\"Effect\": \"Allow\"," + "\"Principal\": {" + "\"Service\": [" + "\"sagemaker.amazonaws.com\"," + "\"sagemaker-geospatial.amazonaws.com\"," + "\"lambda.amazonaws.com\"," + "\"s3.amazonaws.com\"" + "]" + "}," + "\"Action\": \"sts:AssumeRole\"" + "}]" + "}" val request = CreateRoleRequest { roleName = roleNameVal assumeRolePolicyDocument = assumeRolePolicy description = "Created using the AWS SDK for Kotlin" } IamClient { region = "AWS_GLOBAL" }.use { iamClient -> val roleResult = iamClient.createRole(request) // Attach the policies to the role. for (policy in lambdaRolePolicies) { val attachRequest = AttachRolePolicyRequest { roleName = roleNameVal policyArn = policy } iamClient.attachRolePolicy(attachRequest) } // Allow time for the role to be ready. TimeUnit.SECONDS.sleep(15) println("Role ready with ARN " + roleResult.role?.arn) return roleResult.role?.arn.toString() } } fun getLambdaRolePolicies(): Array<String?> { val lambdaRolePolicies = arrayOfNulls<String>(5) lambdaRolePolicies[0] = "arn:aws:iam::aws:policy/AmazonSageMakerFullAccess" lambdaRolePolicies[1] = "arn:aws:iam::aws:policy/AmazonSQSFullAccess" lambdaRolePolicies[2] = "arn:aws:iam::aws:policy/service-role/" + "AmazonSageMakerGeospatialFullAccess" lambdaRolePolicies[3] = "arn:aws:iam::aws:policy/service-role/" + "AmazonSageMakerServiceCatalogProductsLambdaServiceRolePolicy" lambdaRolePolicies[4] = "arn:aws:iam::aws:policy/service-role/" + "AWSLambdaSQSQueueExecutionRole" return lambdaRolePolicies } fun getSageMakerRolePolicies(): Array<String?> { val sageMakerRolePolicies = arrayOfNulls<String>(3) sageMakerRolePolicies[0] = "arn:aws:iam::aws:policy/AmazonSageMakerFullAccess" sageMakerRolePolicies[1] = "arn:aws:iam::aws:policy/service-role/" + "AmazonSageMakerGeospatialFullAccess" sageMakerRolePolicies[2] = "arn:aws:iam::aws:policy/AmazonSQSFullAccess" return sageMakerRolePolicies }