-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneptune-quickstart.yaml
141 lines (138 loc) · 4.73 KB
/
neptune-quickstart.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
Description: Cheap AWS Neptune Stack for introductory development
Resources:
NeptuneBaseStack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: https://s3.eu-west-1.amazonaws.com/cloudwanderer.io/cloudformation/neptune-base-stack.yaml
WorkbenchStack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: https://s3.eu-west-1.amazonaws.com/cloudwanderer.io/cloudformation/neptune-workbench-stack.yaml
Parameters:
NeptuneClusterEndpoint: !GetAtt NeptuneBaseStack.Outputs.DBClusterEndpoint
NeptuneClusterSecurityGroups: !GetAtt NeptuneBaseStack.Outputs.NeptuneSG
NeptuneClusterSubnetId: !GetAtt NeptuneBaseStack.Outputs.PublicSubnet1
SageMakerNotebookName: !GetAtt NeptuneBaseStack.Outputs.DBClusterResourceId
SageMakerNotebookRole: !GetAtt SageMakerNotebookRole.Arn
Cloud9Stack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: https://s3.eu-west-1.amazonaws.com/cloudwanderer.io/cloudformation/cloud9-stack.yaml
Parameters:
SubnetId: !GetAtt NeptuneBaseStack.Outputs.PublicSubnet1
SageMakerNotebookRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument: >
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "sagemaker.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess
- arn:aws:iam::aws:policy/AmazonSageMakerFullAccess
Cloud9LambdaHelperRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument: >
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
}
Policies:
- PolicyName: cloudformation
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "rds:StopDBCluster"
Resource: "*"
- Effect: Allow
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource: "arn:aws:logs:*:*:*"
LambdaFunctionStopNeptuneTrigger:
Type: "AWS::Events::Rule"
Properties:
ScheduleExpression: "rate(3 hours)"
Targets:
- Arn: !GetAtt "LambdaFunctionStopNeptune.Arn"
Id: 1
LambdaFunctionStopNeptuneTriggerPermission:
Type: "AWS::Lambda::Permission"
Properties:
Action: "lambda:InvokeFunction"
FunctionName: !GetAtt "LambdaFunctionStopNeptune.Arn"
Principal: events.amazonaws.com
SourceArn: !GetAtt "LambdaFunctionStopNeptuneTrigger.Arn"
LambdaFunctionStopNeptune:
Type: AWS::Lambda::Function
Properties:
Runtime: python3.9
Role: !GetAtt "Cloud9LambdaHelperRole.Arn"
Handler: index.lambda_handler
Environment:
Variables:
NeptuneClusterId: !GetAtt "NeptuneBaseStack.Outputs.DBClusterId"
Code:
ZipFile: |
import os
import boto3
from botocore.exceptions import ClientError
def lambda_handler(event, context):
neptune = boto3.client('neptune')
neptune_cluster_id = os.environ['NeptuneClusterId']
try:
neptune.stop_db_cluster(DBClusterIdentifier=neptune_cluster_id)
except ClientError as ex:
if ex.response['Error']['Code'] == 'InvalidDBClusterStateFault':
print(f"{neptune_cluster_id} already stopped")
return
raise
Description: Stop a Neptune DB cluster if it's been running for too long
TracingConfig:
Mode: Active
Outputs:
NeptuneClusterEndpoint:
Description: The endpoint for the Neptune Cluster
Value: !GetAtt NeptuneBaseStack.Outputs.DBClusterEndpoint
Cloud9Link:
Description: The endpoint for the Neptune Cluster
Value: !Join
- ""
- - "https://"
- !Ref AWS::Region
- ".console.aws.amazon.com/cloud9/ide/"
- !GetAtt Cloud9Stack.Outputs.Cloud9Id
SageMakerNoteBookLink:
Description: Link to the Jupyter Notebook
Value: !Join
- ""
- - "https://"
- !Ref AWS::Region
- ".console.aws.amazon.com/sagemaker/home?region="
- !Ref AWS::Region
- "#/notebook-instances/openNotebook/"
- !GetAtt "WorkbenchStack.Outputs.NotebookName"
- "?view=classic"