-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2724e4
commit e6254e0
Showing
13 changed files
with
167 additions
and
143 deletions.
There are no files selected for viewing
14 changes: 0 additions & 14 deletions
14
infra/modules/sagemaker_deployment/lambda_function/s3_move_output.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
27 changes: 27 additions & 0 deletions
27
infra/modules/sagemaker_output_mover/lambda_function/s3_move_output.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import boto3 | ||
|
||
def lambda_handler(event, context): | ||
try: | ||
s3 = boto3.resource('s3') | ||
|
||
sagemaker_endpoint_name = event["requestParameters"]["endpointName"] | ||
input_file_uri = event["requestParameters"]["inputLocation"] | ||
input_file_bucket = input_file_uri.split("/user/federated/")[0].split("s3://")[1] | ||
federated_user_id = input_file_uri.split("/user/federated/")[1].split("/")[0] | ||
|
||
output_file_uri = event["responseParameters"]["outputLocation"] | ||
output_file_bucket = output_file_uri.split("https://")[1].split("/")[0].split(".s3.eu-west-2.amazonaws.com")[0] | ||
output_file_key = output_file_uri.split("https://")[1].split("/")[1] | ||
|
||
copy_source = { | ||
'Bucket': output_file_bucket, | ||
'Key': output_file_key | ||
} | ||
s3_filepath_output = f"user/federated/{federated_user_id}/sagemaker/outputs/{output_file_key}" | ||
s3.meta.client.copy(copy_source, input_file_bucket, s3_filepath_output) | ||
|
||
print(f"User {federated_user_id} called Sagemaker endpoint {sagemaker_endpoint_name} and the output file key was {s3_filepath_output}") | ||
|
||
except Exception as e: | ||
print("An error occurred") | ||
raise e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
|
||
resource "aws_iam_role" "iam_for_lambda_s3_move" { | ||
name = "iam_for_lambda_s3_move" | ||
assume_role_policy = jsonencode({ | ||
Version = "2012-10-17" | ||
Statement = [ | ||
{ | ||
Action = "sts:AssumeRole" | ||
Effect = "Allow" | ||
Principal = { | ||
Service = "lambda.amazonaws.com" | ||
} | ||
}]}) | ||
} | ||
|
||
resource "aws_iam_role_policy" "policy_for_lambda_s3_move" { | ||
name = "policy_for_lambda_s3_move" | ||
role = aws_iam_role.iam_for_lambda_s3_move.id | ||
|
||
policy = jsonencode({ | ||
Version = "2012-10-17" | ||
Statement = [ | ||
{ | ||
Action = ["SNS:Receive", "SNS:Subscribe"] | ||
Effect = "Allow" | ||
Resource = aws_sns_topic.async-sagemaker-success-topic.arn | ||
}, | ||
{ | ||
Action = ["s3:GetObject"] | ||
Effect = "Allow" | ||
Resource = "arn:aws:s3:::*sagemaker*" | ||
}, | ||
{ | ||
Action = ["s3:PutObject"] | ||
Effect = "Allow" | ||
Resource = "${var.s3_bucket_notebooks_arn}*" | ||
} | ||
] | ||
}) | ||
} | ||
|
||
data "archive_file" "lambda_payload" { | ||
type = "zip" | ||
source_file = "${path.module}/lambda_function/s3_move_output.py" | ||
output_path = "${path.module}/lambda_function/payload.zip" | ||
} | ||
|
||
resource "aws_lambda_function" "lambda_s3_move_output" { | ||
filename = data.archive_file.lambda_payload.output_path | ||
source_code_hash = data.archive_file.lambda_payload.output_base64sha256 | ||
function_name = "lambda_s3_move_output" | ||
role = aws_iam_role.iam_for_lambda_s3_move.arn | ||
handler = "s3_move_output.lambda_handler" | ||
runtime = "python3.12" | ||
timeout = 30 | ||
} | ||
|
||
|
||
resource "aws_sns_topic" "async-sagemaker-success-topic" { | ||
name = "async-sagemaker-success-topic" | ||
policy = data.aws_iam_policy_document.sns_publish_and_read_policy.json | ||
} | ||
|
||
resource "aws_sns_topic_subscription" "topic_lambda" { | ||
topic_arn = aws_sns_topic.async-sagemaker-success-topic.arn | ||
protocol = "lambda" | ||
endpoint = aws_lambda_function.lambda_s3_move_output.arn | ||
} | ||
|
||
resource "aws_lambda_permission" "with_sns" { | ||
statement_id = "AllowExecutionFromSNS" | ||
action = "lambda:InvokeFunction" | ||
function_name = aws_lambda_function.lambda_s3_move_output.function_name | ||
principal = "sns.amazonaws.com" | ||
source_arn = aws_sns_topic.async-sagemaker-success-topic.arn | ||
} | ||
|
||
data "aws_iam_policy_document" "sns_publish_and_read_policy" { | ||
statement { | ||
sid = "sns_publish_and_read_policy_1" | ||
actions = ["SNS:Publish"] | ||
effect = "Allow" | ||
principals { | ||
type = "Service" | ||
identifiers = ["sagemaker.amazonaws.com"] | ||
} | ||
resources = ["arn:aws:sns:${var.aws_region}:${var.account_id}:async-sagemaker-success-topic"] | ||
} | ||
statement { | ||
sid = "sns_publish_and_read_policy_2" | ||
actions = ["SNS:Receive","SNS:Subscribe"] | ||
effect = "Allow" | ||
principals { | ||
type = "Service" | ||
identifiers = ["lambda.amazonaws.com"] | ||
} | ||
resources = ["arn:aws:sns:${var.aws_region}:${var.account_id}:async-sagemaker-success-topic"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
output "sns_success_topic_arn" { | ||
value = aws_sns_topic.async-sagemaker-success-topic.arn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
variable "account_id" { | ||
type = string | ||
description = "AWS Account ID" | ||
} | ||
|
||
variable "aws_region" { | ||
type = string | ||
description = "AWS Region in format e.g. us-west-1" | ||
} | ||
|
||
variable "s3_bucket_notebooks_arn" { | ||
type = string | ||
description = "S3 Bucket for notebook user data storage" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters