質問編集履歴

2

コード追加

2021/07/15 18:10

投稿

koichi88
koichi88

スコア10

test CHANGED
File without changes
test CHANGED
@@ -76,6 +76,10 @@
76
76
 
77
77
  ## コード追記
78
78
 
79
+
80
+
81
+ ```
82
+
79
83
  import boto3
80
84
 
81
85
  import logging
@@ -215,3 +219,5 @@
215
219
  Payload=json.dumps(params)
216
220
 
217
221
  )
222
+
223
+ ```

1

こーど追加

2021/07/15 18:10

投稿

koichi88
koichi88

スコア10

test CHANGED
File without changes
test CHANGED
@@ -56,11 +56,11 @@
56
56
 
57
57
  ```
58
58
 
59
- [ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 35)
59
+ [ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 34)
60
60
 
61
61
  Traceback (most recent call last):
62
62
 
63
- File "/var/task/lambda_function.py" Line 35
63
+ File "/var/task/lambda_function.py" Line 34
64
64
 
65
65
  client.restore-db-cluster-from-snapshot(
66
66
 
@@ -71,3 +71,147 @@
71
71
  lambda内でのrestore-db-cluster-from-snapshotを利用する場合もclient.の後に追加する形で動作すると思っているのですが、何かご存知であれば、教えて頂きたいです。
72
72
 
73
73
  (client.に関してはまだ理解できておりません。)
74
+
75
+
76
+
77
+ ## コード追記
78
+
79
+ import boto3
80
+
81
+ import logging
82
+
83
+ import os
84
+
85
+ import json
86
+
87
+
88
+
89
+ logger = logging.getLogger()
90
+
91
+ logger.setLevel(logging.INFO)
92
+
93
+ client = boto3.client('rds')
94
+
95
+
96
+
97
+ def lambda_handler(event, context):
98
+
99
+ snapshot_id = os.environ.get('SNAPSHOT_ID')
100
+
101
+ if not snapshot_id:
102
+
103
+ logger.info('START get_snapshot_id')
104
+
105
+ snapshot_id = get_snapshot_id()
106
+
107
+ logger.info('END get_snapshot_id')
108
+
109
+ if (snapshot_id is None):
110
+
111
+ logger.info('not found snapshot')
112
+
113
+ return
114
+
115
+
116
+
117
+ logger.info('snapshot_id:' + snapshot_id)
118
+
119
+
120
+
121
+ #if (not is_exist_instance()):
122
+
123
+ logger.info('START restore')
124
+
125
+ restore(snapshot_id)
126
+
127
+ logger.info('END restore')
128
+
129
+ logger.info('START call_modify_lambda')
130
+
131
+ call_modify_lambda()
132
+
133
+ logger.info('END call_modify_lambda')
134
+
135
+ #else:
136
+
137
+ # logger.info('exist instance')
138
+
139
+
140
+
141
+ def restore(snapshot_id):
142
+
143
+ logger.info('restore snapshot id:' + snapshot_id)
144
+
145
+ client.restore-db-cluster-from-snapshot(
146
+
147
+ AvailabilityZones=['ap-northeast-1a'],
148
+
149
+ DBClusterIdentifier='xxxx-cluster',
150
+
151
+ SnapshotIdentifier=snapshot_id,
152
+
153
+ Engine='aurora-mysql',
154
+
155
+ DBClusterParameterGroupName='paramgroup-XXX'
156
+
157
+ )
158
+
159
+
160
+
161
+ def is_exist_instance():
162
+
163
+ db_instances = client.describe_db_instances();
164
+
165
+ target_instance = \
166
+
167
+ filter(lambda x : x['DBInstanceIdentifier'] == os.environ.get('INSTANCE_ID'), db_instances['DBInstances'])
168
+
169
+ return len(target_instance) > 0
170
+
171
+
172
+
173
+ def get_snapshot_id():
174
+
175
+ snapshot_list = client.describe_db_snapshots(
176
+
177
+ DBInstanceIdentifier=os.environ.get('INSTANCE_ID'),
178
+
179
+ SnapshotType='manual',
180
+
181
+ )
182
+
183
+
184
+
185
+ logger.info(snapshot_list)
186
+
187
+ snapshot_id = \
188
+
189
+ snapshot_list['DBSnapshots'][0]['DBSnapshotIdentifier'] if len(snapshot_list['DBSnapshots']) > 0 else None
190
+
191
+
192
+
193
+ return snapshot_id
194
+
195
+
196
+
197
+ def call_modify_lambda():
198
+
199
+ params = {}
200
+
201
+ params['instance_id'] = os.environ.get('INSTANCE_ID')
202
+
203
+ params['retry_count'] = 0
204
+
205
+ params['modified_flag'] = False
206
+
207
+ client_lambda = boto3.client('lambda')
208
+
209
+ client_lambda.invoke(
210
+
211
+ FunctionName='modify-rds',
212
+
213
+ InvocationType='Event',
214
+
215
+ Payload=json.dumps(params)
216
+
217
+ )