Skip to content

Commit d5b14e0

Browse files
Update CI/CD instructions
1 parent 9129d88 commit d5b14e0

File tree

7 files changed

+56
-20
lines changed

7 files changed

+56
-20
lines changed

.idea/aws.xml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/deployment.xml

+10-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/sagemaker-ssh-helper.iml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CONTRIBUTING.md

+29-12
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,30 @@ Configure SageMaker execution role through [defaults](https://sagemaker.readthed
2121

2222
Make sure all tests are working. You need to manually create resources that are passed through environment variables:
2323
```shell
24-
export ACCOUNT_ID="..."
25-
export AWS_ACCESS_KEY_ID="..."
24+
export ACCOUNT_ID="..." # Obviously, you need AWS account
25+
export AWS_ACCESS_KEY_ID="..." # The IAM user should be powerful enough to assume USER_ROLE and to bootstrap CDK
2626
export AWS_SECRET_ACCESS_KEY="..."
2727

28-
export SAGEMAKER_ROLE="..."
29-
export USER_ROLE="..."
30-
export SAGEMAKER_STUDIO_DOMAIN="..."
31-
export SAGEMAKER_STUDIO_VPC_ONLY_DOMAIN="..."
32-
export VPC_ONLY_SECURITY_GROUP="..."
33-
export VPC_ONLY_SUBNET="..."
34-
export SNS_NOTIFICATION_TOPIC_ARN="..."
35-
export LOCAL_USER_ID="AIDACKCEVSQ6C2EXAMPLE:terry@SSO"
28+
export SAGEMAKER_ROLE="..." # You can create it automatically by creating SageMaker Domain
29+
export USER_ROLE="..." # AWS_ACCESS_KEY role should be allowed to assume (be trusted by) this role for at least 10 h.
30+
# User role should trust `codebuild.amazonaws.com`, to call sm-docker.
31+
export SAGEMAKER_STUDIO_DOMAIN="d-..." # You need to create domain manually and create users: test-base-python,
32+
# test-data-science, test-tensorflow, test-pytorch, test-spark, test-firefox.
33+
# Create Studio Classic lifecycle config for KernelGateway apps named
34+
# `sagemaker-ssh-helper-dev` from kernel-lc-config.sh. Add it to the domain.
35+
# For `test-firefox` user, open SageMaker Studio Classic and 'Run' the app.
36+
export VPC_ONLY_SUBNET="subnet-..." # Create in the default VPC. Don't add Internet gateway or NAT to this subnet.
37+
# Configure VPC endpoints for STS, SSM, S3 and SageMaker.
38+
export VPC_ONLY_SECURITY_GROUP="sg-..." # Can be default VPC security group
39+
export SAGEMAKER_STUDIO_VPC_ONLY_DOMAIN="d-..." # Create manually, too. Create `internet-free-user` in the domain.
40+
# Attach `sagemaker-ssh-helper-dev` lifecycle config to
41+
# KernelGateway apps.
42+
export SNS_NOTIFICATION_TOPIC_ARN="..." # Create SNS topic manually, subscribe to it your e-mail
43+
export LOCAL_USER_ID="AROATCKARONAGFEXAMPLE:gitlab-ci" # AWS_ACCESS_KEY UserId from `aws sts get-caller-identity`
3644
export JB_LICENSE_SERVER_HOST="jetbrains-license-server.example.com"
45+
export SAGEMAKER_NOTEBOOK_INSTANCE="ssh-helper-notebook" # Create manually, run SageMaker_SSH_Notebook.ipynb
3746

38-
export SKIP_CDK="false"
47+
export SKIP_CDK="false"
3948
export SKIP_PROFILE_TESTS="false"
4049

4150
export PYTEST_EXTRA_ARGS=" "
@@ -45,7 +54,15 @@ export PYTEST_KEYWORDS=" "
4554
bash run_tests.sh
4655
```
4756

48-
Now write a failing test, put code to make it pass, and make sure other tests are still working to avoid any regression.
57+
*Note:* You can find example CDK bootstrap policy for the AWS access key role in [tests/iam/CDKBootstrapPolicy.json](tests/iam/CDKBootstrapPolicy.json). This role should be also able to access SageMaker default buckets, see [tests/iam/GitLabCIPolicy.json](tests/iam/GitLabCIPolicy.json).
58+
59+
Now write a failing test, put code to make it pass, and make sure other tests are still working to avoid any regression. See [.gitlab-ci.yml](.gitlab-ci.yml) and [run_tests.sh](run_tests.sh) to learn how to do that.
60+
61+
For the full run of all tests, at the moment of writing, you need ~9 hours. The user role should allow [session duration](https://docs.aws.amazon.com/IAM/latest/UserGuide/roles-managingrole-editing-console.html#roles-modify_max-session-duration) for at least 10 hours. We recommend setting the max duration through AWS Console to 12 hours.
62+
63+
**TODO** (for developers): Because IAM role chaining doesn't allow to assume the chained role for more than 1 hour, ACCESS_KEY (for now) should be the IAM user. We should reverse the assume role logic so that tests run under USER_ROLE which in turn assumes the CDK bootstrap role. Now the logic in [run_tests.sh](run_tests.sh) is the opposite.
64+
65+
**TODO** (for developers): We should create CDK to set up domains, users and VPCs.
4966

5067
### Code formatting
5168

run_tests.sh

+11-5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ sm-local-configure
4141
source tests/generate_sagemaker_config.sh
4242
source tests/generate_accelerate_config.sh
4343

44+
# The user should have CDK Bootstrap permissions:
45+
aws sts get-caller-identity
46+
4447
if [ "$SKIP_CDK" == "true" ]; then
4548
echo "Skipping CDK changes"
4649
else
@@ -61,7 +64,6 @@ else
6164
REGION=eu-west-1
6265
# See https://docs.aws.amazon.com/cdk/v2/guide/bootstrapping.html
6366
# See tests/iam/CDKBootstrapPolicy.json
64-
# See tests/iam/CDKCloudFormationPolicy.json
6567
cdk bootstrap aws://"$ACCOUNT_ID"/"$REGION" \
6668
--require-approval never
6769
APP="python -m sagemaker_ssh_helper.cdk.tests_app"
@@ -88,7 +90,7 @@ else
8890
unset REGION
8991
fi
9092

91-
# Set bucket versioning to detect model repacking / dependencies overrides
93+
echo "Set bucket versioning to detect model repacking / dependencies overrides"
9294
# See tests/iam/GitLabCIPolicy.json
9395
aws s3api put-bucket-versioning \
9496
--bucket "$(AWS_DEFAULT_REGION=eu-west-1 bash tests/get_sagemaker_bucket.sh)" \
@@ -102,20 +104,23 @@ export AWS_REGION=eu-west-1
102104
export AWS_DEFAULT_REGION=eu-west-1
103105
aws configure list
104106

105-
# Assume CI/CD role
107+
# Assume USER_ROLE for CI/CD tests:
106108
# shellcheck disable=SC2207
107109
sts=( $(source tests/assume-user-role.sh) )
108110
export AWS_ACCESS_KEY_ID=${sts[0]}
109111
export AWS_SECRET_ACCESS_KEY=${sts[1]}
110112
export AWS_SESSION_TOKEN=${sts[2]}
111113

114+
# To fetch UserId for LOCAL_USER_ID:
115+
aws sts get-caller-identity
116+
112117
# Smoke test of `sm-ssh` utility
113118
AWS_DEFAULT_REGION=eu-west-1 sm-ssh list
114119
AWS_DEFAULT_REGION=eu-west-2 sm-ssh list
115120
mkdir -p /root/.ssh
116121
cat ssh_config_template.txt >>/root/.ssh/config
117-
ssh -o StrictHostKeyChecking=no sagemaker-ssh-helper.notebook.sagemaker \
118-
python --version
122+
ssh -o StrictHostKeyChecking=no ssh-helper-notebook.notebook.sagemaker \
123+
python --version || echo "ERROR: cannot connect to notebook"
119124

120125
# Run tests
121126
mkdir -p tests/output
@@ -131,6 +136,7 @@ coverage run -m pytest \
131136
-o vpc_only_security_group="$VPC_ONLY_SECURITY_GROUP" \
132137
-o sagemaker_role="$SAGEMAKER_ROLE" \
133138
-o sns_notification_topic_arn="$SNS_NOTIFICATION_TOPIC_ARN" \
139+
-o sagemaker_notebook_instance="$SAGEMAKER_NOTEBOOK_INSTANCE" \
134140
-k "$PYTEST_KEYWORDS" $PYTEST_EXTRA_ARGS || EXIT_CODE=$?
135141
coverage xml
136142
coverage html --show-contexts

tests/conftest.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ def pytest_addoption(parser):
66
parser.addini('vpc_only_security_group', '')
77
parser.addini('sagemaker_role', '')
88
parser.addini('sns_notification_topic_arn', '')
9+
parser.addini('sagemaker_notebook_instance', '')

0 commit comments

Comments
 (0)