teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

コード追加

2021/07/15 18:10

投稿

koichi88
koichi88

スコア10

title CHANGED
File without changes
body CHANGED
@@ -37,6 +37,8 @@
37
37
  (client.に関してはまだ理解できておりません。)
38
38
 
39
39
  ## コード追記
40
+
41
+ ```
40
42
  import boto3
41
43
  import logging
42
44
  import os
@@ -106,4 +108,5 @@
106
108
  FunctionName='modify-rds',
107
109
  InvocationType='Event',
108
110
  Payload=json.dumps(params)
109
- )
111
+ )
112
+ ```

1

こーど追加

2021/07/15 18:10

投稿

koichi88
koichi88

スコア10

title CHANGED
File without changes
body CHANGED
@@ -27,11 +27,83 @@
27
27
  再度実行したところ、違うエラーに変わりました。
28
28
 
29
29
  ```
30
- [ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 35)
30
+ [ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 34)
31
31
  Traceback (most recent call last):
32
- File "/var/task/lambda_function.py" Line 35
32
+ File "/var/task/lambda_function.py" Line 34
33
33
  client.restore-db-cluster-from-snapshot(
34
34
  ```
35
35
 
36
36
  lambda内でのrestore-db-cluster-from-snapshotを利用する場合もclient.の後に追加する形で動作すると思っているのですが、何かご存知であれば、教えて頂きたいです。
37
- (client.に関してはまだ理解できておりません。)
37
+ (client.に関してはまだ理解できておりません。)
38
+
39
+ ## コード追記
40
+ import boto3
41
+ import logging
42
+ import os
43
+ import json
44
+
45
+ logger = logging.getLogger()
46
+ logger.setLevel(logging.INFO)
47
+ client = boto3.client('rds')
48
+
49
+ def lambda_handler(event, context):
50
+ snapshot_id = os.environ.get('SNAPSHOT_ID')
51
+ if not snapshot_id:
52
+ logger.info('START get_snapshot_id')
53
+ snapshot_id = get_snapshot_id()
54
+ logger.info('END get_snapshot_id')
55
+ if (snapshot_id is None):
56
+ logger.info('not found snapshot')
57
+ return
58
+
59
+ logger.info('snapshot_id:' + snapshot_id)
60
+
61
+ #if (not is_exist_instance()):
62
+ logger.info('START restore')
63
+ restore(snapshot_id)
64
+ logger.info('END restore')
65
+ logger.info('START call_modify_lambda')
66
+ call_modify_lambda()
67
+ logger.info('END call_modify_lambda')
68
+ #else:
69
+ # logger.info('exist instance')
70
+
71
+ def restore(snapshot_id):
72
+ logger.info('restore snapshot id:' + snapshot_id)
73
+ client.restore-db-cluster-from-snapshot(
74
+ AvailabilityZones=['ap-northeast-1a'],
75
+ DBClusterIdentifier='xxxx-cluster',
76
+ SnapshotIdentifier=snapshot_id,
77
+ Engine='aurora-mysql',
78
+ DBClusterParameterGroupName='paramgroup-XXX'
79
+ )
80
+
81
+ def is_exist_instance():
82
+ db_instances = client.describe_db_instances();
83
+ target_instance = \
84
+ filter(lambda x : x['DBInstanceIdentifier'] == os.environ.get('INSTANCE_ID'), db_instances['DBInstances'])
85
+ return len(target_instance) > 0
86
+
87
+ def get_snapshot_id():
88
+ snapshot_list = client.describe_db_snapshots(
89
+ DBInstanceIdentifier=os.environ.get('INSTANCE_ID'),
90
+ SnapshotType='manual',
91
+ )
92
+
93
+ logger.info(snapshot_list)
94
+ snapshot_id = \
95
+ snapshot_list['DBSnapshots'][0]['DBSnapshotIdentifier'] if len(snapshot_list['DBSnapshots']) > 0 else None
96
+
97
+ return snapshot_id
98
+
99
+ def call_modify_lambda():
100
+ params = {}
101
+ params['instance_id'] = os.environ.get('INSTANCE_ID')
102
+ params['retry_count'] = 0
103
+ params['modified_flag'] = False
104
+ client_lambda = boto3.client('lambda')
105
+ client_lambda.invoke(
106
+ FunctionName='modify-rds',
107
+ InvocationType='Event',
108
+ Payload=json.dumps(params)
109
+ )