Skip to content

Commit

Permalink
Fixed the PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mahesh-balumuri committed Aug 29, 2024
1 parent acaf97b commit 6f79563
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 63 deletions.
30 changes: 15 additions & 15 deletions data/mlops-stepfunctions/config/input.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
app_id: aiops
model_id: demo
model_id: MODEL_NAME
job_prefix: mlops
preprocessing:
run: true
Expand All @@ -25,7 +25,7 @@ preprocessing:
- InputName: Code
AppManaged: false
S3Input:
S3Uri: s3://MLOPS_BUCKET/demo/scripts
S3Uri: s3://MLOPS_BUCKET/MODEL_NAME/scripts
LocalPath: /opt/ml/processing/code
S3DataType: S3Prefix
S3InputMode: File
Expand All @@ -35,13 +35,13 @@ preprocessing:
- OutputName: train
AppManaged: false
S3Output:
S3Uri: s3://MLOPS_BUCKET/demo/processing/train
S3Uri: s3://MLOPS_BUCKET/MODEL_NAME/processing/train
LocalPath: /opt/ml/processing/train
S3UploadMode: EndOfJob
- OutputName: test
AppManaged: false
S3Output:
S3Uri: s3://MLOPS_BUCKET/demo/processing/test
S3Uri: s3://MLOPS_BUCKET/MODEL_NAME/processing/test
LocalPath: /opt/ml/processing/test
S3UploadMode: EndOfJob
StoppingCondition:
Expand Down Expand Up @@ -71,16 +71,16 @@ training:
DataSource:
S3DataSource:
S3DataType: S3Prefix
S3Uri: s3://MLOPS_BUCKET/demo/processing/train
S3Uri: s3://MLOPS_BUCKET/MODEL_NAME/processing/train
S3DataDistributionType: FullyReplicated
- ChannelName: code
DataSource:
S3DataSource:
S3DataType: S3Prefix
S3Uri: s3://MLOPS_BUCKET/demo/scripts
S3Uri: s3://MLOPS_BUCKET/MODEL_NAME/scripts
S3DataDistributionType: FullyReplicated
OutputDataConfig:
S3OutputPath: s3://MLOPS_BUCKET/demo/model/
S3OutputPath: s3://MLOPS_BUCKET/MODEL_NAME/model/
StoppingCondition:
MaxRuntimeInSeconds: 3600
Tags:
Expand All @@ -106,23 +106,23 @@ evaluation:
- InputName: input
AppManaged: false
S3Input:
S3Uri: s3://MLOPS_BUCKET/demo/model/SFN_EXECUTION_ID/output/model.tar.gz
S3Uri: s3://MLOPS_BUCKET/MODEL_NAME/model/SFN_EXECUTION_ID/output/model.tar.gz
LocalPath: /opt/ml/processing/model
S3DataType: S3Prefix
S3InputMode: File
S3DataDistributionType: FullyReplicated
- InputName: Code
AppManaged: false
S3Input:
S3Uri: s3://MLOPS_BUCKET/demo/scripts
S3Uri: s3://MLOPS_BUCKET/MODEL_NAME/scripts
LocalPath: /opt/ml/processing/code
S3DataType: S3Prefix
S3InputMode: File
S3DataDistributionType: FullyReplicated
- InputName: test
AppManaged: false
S3Input:
S3Uri: s3://MLOPS_BUCKET/demo/processing/test
S3Uri: s3://MLOPS_BUCKET/MODEL_NAME/processing/test
LocalPath: /opt/ml/processing/test
S3DataType: S3Prefix
S3InputMode: File
Expand All @@ -132,7 +132,7 @@ evaluation:
- OutputName: evaluation
AppManaged: false
S3Output:
S3Uri: s3://MLOPS_BUCKET/demo/evaluation/output
S3Uri: s3://MLOPS_BUCKET/MODEL_NAME/evaluation/output
LocalPath: /opt/ml/processing/evaluation
S3UploadMode: EndOfJob
StoppingCondition:
Expand All @@ -153,10 +153,10 @@ CreateModel:
VpcConfig:
PrimaryContainer:
Image: IMAGE_URI
ModelDataUrl: s3://MLOPS_BUCKET/demo/model/SFN_EXECUTION_ID/output/model.tar.gz
ModelDataUrl: s3://MLOPS_BUCKET/MODEL_NAME/model/SFN_EXECUTION_ID/output/model.tar.gz
Environment:
SAGEMAKER_PROGRAM: inference.py
SAGEMAKER_SUBMIT_DIRECTORY: s3://MLOPS_BUCKET/demo/scripts/source.tar.gz
SAGEMAKER_SUBMIT_DIRECTORY: s3://MLOPS_BUCKET/MODEL_NAME/scripts/source.tar.gz

ExecutionRoleArn: SAGEMAKER_EXECUTION_ROLE

Expand All @@ -174,11 +174,11 @@ batchTransform:
DataSource:
S3DataSource:
S3DataType: S3Prefix
S3Uri: s3://MLOPS_BUCKET/demo/processing/test/test_features.csv
S3Uri: s3://MLOPS_BUCKET/MODEL_NAME/processing/test/test_features.csv
TransformOutput:
Accept: text/csv
AssembleWith: Line
S3OutputPath: s3://MLOPS_BUCKET/demo/batch-output/SFN_EXECUTION_ID/
S3OutputPath: s3://MLOPS_BUCKET/MODEL_NAME/batch-output/SFN_EXECUTION_ID/
TransformResources:
InstanceType: ml.m5.xlarge
InstanceCount: 1
Expand Down
6 changes: 6 additions & 0 deletions manifests/mlops-stepfunctions/mlops-stepfunctions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@ targetAccount: primary
dataFiles:
- filePath: data/mlops-stepfunctions/config/input.yaml
parameters:
- name: model-name
# Replace value with the valid model name
value: aws
- name: hours
# Replace the value with the hours at what time the StateMachine Needs to run in UTC daily
value: "18"
- name: config-file-path
value: data/mlops-stepfunctions/config/input.yaml
68 changes: 31 additions & 37 deletions modules/examples/mlops-stepfunctions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,53 +26,47 @@ Here's a typical workflow:

# Deployment Guide

## Set-up the environment(s)
See deployment steps in the [Deployment Guide](../../../DEPLOYMENT.md).

1. Clone the repository and checkout a release branch using the below command:

```
git clone --origin upstream --branch release/1.4.0 https://github.com/awslabs/aiops-modules
```
The release version can be replaced with the version of interest.
## Inputs/Outputs

2. Move into the `aiops-modules` repository:
```
cd aiops-modules
```
3. Create and activate a Virtual environment
```
python3 -m venv .venv && source .venv/bin/activate
```
4. Install the requirements
```
pip install -r ./requirements.txt
```
5. Set environment variables
### Input Parameters

Replace the values below with your AWS account id and Administrator IAM Role.
```
export PRIMARY_ACCOUNT=XXXXXXXXXXXX
export ADMIN_ROLE_ARN=arn:aws:iam::XXXXXXXXXXXX:role/XXXXX
```
#### Required

5. Bootstrap the CDK environment (one time per region) with CDK V2. Assuming you are deploying in `us-east-1`:
```
cdk bootstrap aws://${PRIMARY_ACCOUNT}/us-east-1
```
6. Bootstrap AWS Account(s)
- `model-name` : Model Identifier (default it is "demo")
- `hours`: Time in UTC hour to schedule the event to run the statemachine daily.

Assuming that you will be using a single account, follow the guide [here](https://seed-farmer.readthedocs.io/en/latest/bootstrapping.html#) to bootstrap your account(s) to function as a toolchain and target account.
## Sample manifest declaration

Following is the command to bootstrap your existing account to a toolchain and target account.
Create a manifest file under appropriate location, for example examples/manifests
```
seedfarmer bootstrap toolchain --project aiops --trusted-principal ${ADMIN_ROLE_ARN} --as-target
name: mlops-stepfunctions
path: git::https://github.com/awslabs/aiops-modules.git//modules/examples/mlops-stepfunctions?ref=release/1.4.0&depth=1
parameters:
- name: model-name
value: demo
- name: hours
value: "18"
```

## Deployment
### Module Metadata Outputs

Pick the manifest to deploy. Manifests are located in `manifests/` directory. For example, to deploy this modules, run:
- `MlOpsBucket`: Name of the Bucket where Model Artifacts are stored.
- `SageMakerExecutionRole`: Execution Roles used by SageMaker Service.
- `ImageUri`: Docker Image URI used by SageMaker Jobs.
- `StateMachine`: ARN of State Machine.
- `LambdaFunction`: ARN of Lambda function which starts the execution of State Machine.

!Note: if you are deploying into a region different from `us-east-1`, change the `regionMappings` in `deployment.yaml`.
```
seedfarmer apply manifests/mlops-stepfunctions/deployment.yaml
#### Output Example

```yaml
metadata: | {
"MlOpsBucket": "",
"SageMakerExecutionRole": "arn:aws:iam::123456789012:role/SageMakerExecutionRole",
"ImageUri": "683313688378.dkr.ecr.us-east-1.amazonaws.com/sagemaker-scikit-learn:1.2-1-cpu-py3",
"StateMachine": "arn:aws:states:us-east-1:123456789012:stateMachine:MLOpsStateMachine",
"LambdaFunction": "arn:aws:lambda:us-east-1:123456789012:function:MlOpsLambdaFunction",
}
```
6 changes: 4 additions & 2 deletions modules/examples/mlops-stepfunctions/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
project_name=app_settings.seedfarmer_settings.project_name,
deployment_name=app_settings.seedfarmer_settings.deployment_name,
module_name=app_settings.seedfarmer_settings.module_name,
bucket_policy_arn=app_settings.module_settings.bucket_policy_arn,
permission_boundary_arn=app_settings.module_settings.permission_boundary_arn,
model_name=app_settings.module_settings.model_name,
hours=app_settings.module_settings.hours,
env=aws_cdk.Environment(
account=app_settings.cdk_settings.account,
region=app_settings.cdk_settings.region,
Expand All @@ -40,6 +40,8 @@
"MlOpsBucket": stack.mlops_assets_bucket.bucket_name,
"SageMakerExecutionRole": stack.sagemaker_execution_role.role_arn,
"ImageUri": image_uri,
"StateMachine": stack.state_machine_arn,
"LambdaFunction": stack.lambda_function_arn,
}
),
)
Expand Down
3 changes: 2 additions & 1 deletion modules/examples/mlops-stepfunctions/deployspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ deploy:
- sed -i "s/SAGEMAKER_EXECUTION_ROLE/${SAGEMAKER_EXECUTION_ROLE}/g" scripts/input.yaml
- export IMAGE_URI=$(echo ${IMAGE_URI} | sed -e "s#/#\\\/#g")
- sed -i "s/IMAGE_URI/${IMAGE_URI}/g" scripts/input.yaml
- sed -i "s/MODEL_NAME/${SEEDFARMER_PARAMETER_MODEL_NAME}/g" scripts/input.yaml
- tar -czf scripts/source.tar.gz -C scripts inference.py
- aws s3 cp --recursive scripts/ s3://$MLOPS_BUCKET/demo/scripts/
- aws s3 cp --recursive scripts/ s3://$MLOPS_BUCKET/$SEEDFARMER_PARAMETER_MODEL_NAME/scripts/
destroy:
phases:
install:
Expand Down
5 changes: 2 additions & 3 deletions modules/examples/mlops-stepfunctions/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ class ModuleSettings(CdkBaseSettings):

model_config = SettingsConfigDict(env_prefix="SEEDFARMER_PARAMETER_")

bucket_policy_arn: Optional[str] = Field(default=None)
permission_boundary_arn: Optional[str] = Field(default=None)

model_name: str = Field(default="demo")
hours: str = Field(default="18") # 6PM UTC
tags: Optional[Dict[str, str]] = Field(default=None)


Expand Down
12 changes: 7 additions & 5 deletions modules/examples/mlops-stepfunctions/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: Apache-2.0

import logging
from typing import Any, Optional
from typing import Any

import aws_cdk.aws_events as events
import aws_cdk.aws_events_targets as events_targets
Expand All @@ -27,8 +27,8 @@ def __init__(
project_name: str,
deployment_name: str,
module_name: str,
bucket_policy_arn: Optional[str] = None,
permission_boundary_arn: Optional[str] = None,
model_name: str,
hours: str,
**kwargs: Any,
) -> None:
# MLOPS Env vars
Expand Down Expand Up @@ -141,6 +141,7 @@ def __init__(
state_machine_type=sfn.StateMachineType.STANDARD,
role=sfn_exec_role,
)
self.state_machine_arn = state_machine.state_machine_arn

sfn_execution_for_lambda = aws_iam.PolicyDocument(
statements=[
Expand All @@ -164,6 +165,7 @@ def __init__(
environment={"STATE_MACHINE_ARN": state_machine.state_machine_arn},
timeout=Duration.seconds(60),
)
self.lambda_function_arn = lambda_function.function_arn

lambda_role.attach_inline_policy(aws_iam.Policy(self, "SFNExecutionPolicy", document=sfn_execution_for_lambda))
lambda_role.attach_inline_policy(aws_iam.Policy(self, "S3AccessRole", document=s3_access_statements))
Expand All @@ -175,7 +177,7 @@ def __init__(
"MyEventRule",
schedule=events.Schedule.cron(
minute="0",
hour="18", # 6 PM UTC
hour=hours,
month="*",
week_day="*",
year="*",
Expand All @@ -186,7 +188,7 @@ def __init__(
custom_input = {
"config": {
"bucket": mlops_assets_bucket.bucket_name,
"prefix": "demo/scripts/input.yaml",
"prefix": f"{model_name}/scripts/input.yaml",
}
# Add more key-value pairs as needed
}
Expand Down

0 comments on commit 6f79563

Please sign in to comment.