Skip to content

Commit f835c0d

Browse files
authored
Improve CloudFormation template for Neptune DB Notebook (#571)
* Improve CloudFormation template for Neptune DB Notebook * update changelog * update authmode param
1 parent b6cd163 commit f835c0d

File tree

3 files changed

+112
-49
lines changed

3 files changed

+112
-49
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Starting with v1.31.6, this file will contain a record of major features and upd
44

55
## Upcoming
66
- Updated `create-graph` CLI commands in Neptune Analytics samples ([Link to PR](https://github.com/aws/graph-notebook/pull/565))
7+
- Updated NeptuneDB Notebook CloudFormation template ([Link to PR](https://github.com/aws/graph-notebook/pull/571))
78
- Added `@neptune_graph_only` magics decorator ([Link to PR](https://github.com/aws/graph-notebook/pull/569))
89
- Added `%graph_pg_info` line magic ([Link to PR](https://github.com/aws/graph-notebook/pull/570))
910

additional-databases/sagemaker/neptune-notebook-cloudformation/README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
## Launching graph-notebook as Amazon Neptune Workbench via AWS CloudFormation
22

3-
The AWS CloudFormation template in this folder, [`neptune-workbench-stack.yaml`](neptune-workbench-stack.yaml), deploys Amazon Neptune workbench notebooks as resources, and includes the base 'Getting Started' notebooks. The workbench lets you work with your Amazon Neptune cluster using Jupyter notebooks hosted by Amazon SageMaker. You are billed for workbench resources through Amazon SageMaker, separately from your Neptune billing.
3+
The AWS CloudFormation template in this folder, [`neptune-workbench-stack.yaml`](neptune-workbench-stack.yaml), deploys Amazon Neptune workbench notebooks as resources, and includes the base 'Getting Started' notebooks. The workbench lets you work with your Amazon Neptune Database cluster using Jupyter notebooks hosted by Amazon SageMaker. You are billed for workbench resources through Amazon SageMaker, separately from your Neptune billing.
44

55
### Parameter details
66
#### Minimum permissions for the SageMakerNotebookRole
7-
This is the ARN for the AWS IAM role that the notebook instance will assume. Make sure that this role has at least the following minimum permissions within its service role policy:
7+
You may opt to have your notebook instance assume an existing AWS IAM role, via the `SageMakerNotebookRoleArn` stack parameter. Make sure that this role has at least the following minimum permissions within its service role policy:
88

99
```json
1010
{
@@ -17,21 +17,36 @@ This is the ARN for the AWS IAM role that the notebook instance will assume. Mak
1717
"s3:ListBucket"
1818
],
1919
"Resource": [
20-
"arn:aws:s3:::aws-neptune-notebook",
21-
"arn:aws:s3:::aws-neptune-notebook/*"
20+
"arn:(AWS Partition):s3:::aws-neptune-notebook-(AWS Region)",
21+
"arn:(AWS Partition):s3:::aws-neptune-notebook-(AWS Region)/*"
2222
]
2323
},
2424
{
2525
"Effect": "Allow",
2626
"Action": "neptune-db:connect",
2727
"Resource": [
28-
"your-cluster-arn/*"
28+
"arn:(AWS Partition):neptune-db:(AWS Region):(AWS Account ID):(Cluster Resource ID)/*"
2929
]
3030
}
3131
]
3232
}
3333
```
3434

35+
If you would like to enable CloudWatch logging, also add:
36+
```json
37+
{
38+
"Effect": "Allow",
39+
"Action": [
40+
"logs:CreateLogGroup",
41+
"logs:CreateLogStream",
42+
"logs:PutLogEvents"
43+
],
44+
"Resource": [
45+
"arn:(AWS Partition):logs:(AWS Region):(AWS Account ID):log-group:/aws/sagemaker/*"
46+
]
47+
}
48+
```
49+
3550
The role should also establish the following trust relationship:
3651

3752
```json
@@ -48,11 +63,3 @@ The role should also establish the following trust relationship:
4863
]
4964
}
5065
```
51-
52-
#### How to populate the 'Cluster' value within the AWS Console for Amazon Neptune Notebooks
53-
Add the following tags manually to the notebook instance.
54-
55-
| Key | Value |
56-
| ------------- |-------------|
57-
| **aws-neptune-cluster-id** | Amazon Neptune database cluster ID (found under *DB cluster id* under *Configuration* of the selected cluster in the AWS console) |
58-
| **aws-neptune-resource-id** | Amazon Neptune cluster resource ID (found under *Resource id* under *Configuration* of the selected cluster in the AWS console) |

additional-databases/sagemaker/neptune-notebook-cloudformation/neptune-workbench-stack.yaml

Lines changed: 91 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,28 @@ Parameters:
5252
Description: The cluster endpoint of an existing Neptune cluster.
5353
Type: String
5454

55+
NeptuneClusterResourceId:
56+
Description: The resource ID of the existing Neptune cluster.
57+
Type: String
58+
5559
NeptuneClusterPort:
56-
Description: 'OPTIONAL: The Port of an existing Neptune cluster (default 8182).'
60+
Description: 'OPTIONAL: The Port of the existing Neptune cluster (default 8182).'
5761
Type: String
5862
Default: '8182'
5963

64+
NeptuneClusterAuthMode:
65+
Description: The IAM authentication setting on the existing Neptune cluster.
66+
Type: String
67+
Default: 'DEFAULT'
68+
AllowedValues:
69+
- 'IAM'
70+
- 'DEFAULT'
71+
72+
NeptuneClusterLoadFromS3Arn:
73+
Description: 'OPTIONAL: The ARN of the S3 bucket to load data from.'
74+
Type: String
75+
Default: ''
76+
6077
NeptuneClusterSecurityGroups:
6178
Description: The VPC security group IDs. The security groups must be for the same VPC as specified in the subnet.
6279
Type: List<AWS::EC2::SecurityGroup::Id>
@@ -65,15 +82,21 @@ Parameters:
6582
Description: The ID of the subnet in a VPC to which you would like to have a connectivity from your ML compute instance.
6683
Type: AWS::EC2::Subnet::Id
6784

68-
SageMakerNotebookRole:
69-
Description: The ARN for the IAM role that the notebook instance will assume.
85+
SageMakerNotebookRoleArn:
86+
Description: 'OPTIONAL: The ARN for the IAM role that the notebook instance will assume. If not provided, a role will be automatically created, based on the cluster information.'
7087
Type: String
71-
AllowedPattern: ^arn:aws[a-z\-]*:iam::\d{12}:role/?[a-zA-Z_0-9+=,.@\-_/]+$
88+
Default: ''
7289

7390
SageMakerNotebookName:
7491
Description: The name of the Neptune notebook.
7592
Type: String
7693

94+
Conditions:
95+
UseSageMakerNotebookAutoRole:
96+
Fn::Equals:
97+
- !Ref SageMakerNotebookRoleArn
98+
- ""
99+
77100
Resources:
78101
NeptuneNotebookInstance:
79102
Type: AWS::SageMaker::NotebookInstance
@@ -89,42 +112,74 @@ Resources:
89112
SecurityGroupIds:
90113
Ref: NeptuneClusterSecurityGroups
91114
RoleArn:
92-
Ref: SageMakerNotebookRole
115+
Fn::If:
116+
- UseSageMakerNotebookAutoRole
117+
- Fn::GetAtt:
118+
- SageMakerNotebookAutoRole
119+
- Arn
120+
- Ref: SageMakerNotebookRoleArn
93121
LifecycleConfigName:
94122
Fn::GetAtt:
95-
- NeptuneNotebookInstanceLifecycleConfig
96-
- NotebookInstanceLifecycleConfigName
123+
- NeptuneNotebookInstanceLifecycleConfig
124+
- NotebookInstanceLifecycleConfigName
125+
Tags:
126+
- Key: aws-neptune-cluster-id
127+
Value: !Select [ 0, !Split [ ".", !Ref NeptuneClusterEndpoint ] ]
128+
- Key: aws-neptune-resource-id
129+
Value: !Ref NeptuneClusterResourceId
97130

98131
NeptuneNotebookInstanceLifecycleConfig:
99132
Type: AWS::SageMaker::NotebookInstanceLifecycleConfig
100133
Properties:
101134
OnStart:
102-
- Content:
103-
Fn::Base64:
104-
Fn::Join:
105-
- ''
106-
- - "#!/bin/bash\n"
107-
- sudo -u ec2-user -i << 'EOF'
108-
- "\n"
109-
- echo 'export GRAPH_NOTEBOOK_AUTH_MODE=
110-
- "DEFAULT' >> ~/.bashrc\n"
111-
- echo 'export GRAPH_NOTEBOOK_HOST=
112-
- !Ref NeptuneClusterEndpoint
113-
- "' >> ~/.bashrc\n"
114-
- echo 'export GRAPH_NOTEBOOK_PORT=
115-
- !Ref NeptuneClusterPort
116-
- "' >> ~/.bashrc\n"
117-
- echo 'export NEPTUNE_LOAD_FROM_S3_ROLE_ARN=
118-
- "' >> ~/.bashrc\n"
119-
- echo 'export AWS_REGION=
120-
- !Ref AWS::Region
121-
- "' >> ~/.bashrc\n"
122-
- aws s3 cp s3://aws-neptune-notebook/graph_notebook.tar.gz /tmp/graph_notebook.tar.gz
123-
- "\n"
124-
- rm -rf /tmp/graph_notebook
125-
- "\n"
126-
- tar -zxvf /tmp/graph_notebook.tar.gz -C /tmp
127-
- "\n"
128-
- /tmp/graph_notebook/install.sh
129-
- "\n"
130-
- EOF
135+
- Content:
136+
Fn::Base64: !Sub |
137+
#!/bin/bash
138+
sudo -u ec2-user -i << 'EOF'
139+
echo 'export GRAPH_NOTEBOOK_AUTH_MODE=${NeptuneClusterAuthMode}' >> ~/.bashrc
140+
echo 'export GRAPH_NOTEBOOK_SSL=True' >> ~/.bashrc
141+
echo 'export GRAPH_NOTEBOOK_HOST=${NeptuneClusterEndpoint}' >> ~/.bashrc
142+
echo 'export GRAPH_NOTEBOOK_PORT=${NeptuneClusterPort}' >> ~/.bashrc
143+
echo "export GRAPH_NOTEBOOK_SERVICE=neptune-db" >> ~/.bashrc
144+
echo 'export NEPTUNE_LOAD_FROM_S3_ROLE_ARN=${NeptuneClusterLoadFromS3Arn}' >> ~/.bashrc
145+
echo 'export AWS_REGION=${AWS::Region}' >> ~/.bashrc
146+
aws s3 cp s3://aws-neptune-notebook-${AWS::Region}/graph_notebook.tar.gz /tmp/graph_notebook.tar.gz
147+
rm -rf /tmp/graph_notebook
148+
tar -zxvf /tmp/graph_notebook.tar.gz -C /tmp
149+
/tmp/graph_notebook/install.sh
150+
EOF
151+
152+
SageMakerNotebookAutoRole:
153+
Type: AWS::IAM::Role
154+
Condition: UseSageMakerNotebookAutoRole
155+
Properties:
156+
AssumeRolePolicyDocument:
157+
Version: "2012-10-17"
158+
Statement:
159+
- Effect: Allow
160+
Principal:
161+
Service: sagemaker.amazonaws.com
162+
Action: sts:AssumeRole
163+
Policies:
164+
- PolicyName: SageMakerNotebookPolicy
165+
PolicyDocument:
166+
Version: "2012-10-17"
167+
Statement:
168+
- Effect: Allow
169+
Action:
170+
- s3:GetObject
171+
- s3:ListBucket
172+
Resource:
173+
- Fn::Sub: "arn:${AWS::Partition}:s3:::aws-neptune-notebook-${AWS::Region}"
174+
- Fn::Sub: "arn:${AWS::Partition}:s3:::aws-neptune-notebook-${AWS::Region}/*"
175+
- Effect: Allow
176+
Action: neptune-db:connect
177+
Resource:
178+
- Fn::Sub: "arn:${AWS::Partition}:neptune-db:${AWS::Region}:${AWS::AccountId}:${NeptuneClusterResourceId}/*"
179+
- Effect: Allow
180+
Action:
181+
- logs:CreateLogGroup
182+
- logs:CreateLogStream
183+
- logs:PutLogEvents
184+
Resource:
185+
- Fn::Sub: "arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/sagemaker/*"

0 commit comments

Comments
 (0)