質問編集履歴
2
文面の編集
title
CHANGED
File without changes
|
body
CHANGED
@@ -7,8 +7,6 @@
|
|
7
7
|
###実現したいこと
|
8
8
|
txtファイルに「NAME:」という文字列があった場合に、その後ろの文字列を読み込む
|
9
9
|
|
10
|
-
###修正に取り組んでいる箇所
|
11
|
-
ソースコード 70行目
|
12
10
|
###ソースコード
|
13
11
|
|
14
12
|
with open(f'/tmp/app_form_{datetime}', 'r') as f:
|
1
本文の修正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
Python
|
1
|
+
Python txtファイルの読み込み
|
body
CHANGED
@@ -1,89 +1,20 @@
|
|
1
|
-
###
|
1
|
+
###困っていること
|
2
|
+
Pythonを使用してtxtファイルからの読み込み方法
|
2
3
|
|
3
|
-
できている
|
4
|
+
###今できていること
|
4
|
-
|
5
|
+
txtのファイルの1行目だけを読み込み
|
5
6
|
|
6
|
-
できていない
|
7
|
-
IAMユーザに複数のグループを割り当て
|
8
|
-
|
9
7
|
###実現したいこと
|
8
|
+
txtファイルに「NAME:」という文字列があった場合に、その後ろの文字列を読み込む
|
10
9
|
|
11
|
-
|
10
|
+
###修正に取り組んでいる箇所
|
11
|
+
ソースコード 70行目
|
12
|
-
|
12
|
+
###ソースコード
|
13
|
-
GROUP:testguoup
|
14
|
-
GROUP:testguoup2
|
15
13
|
|
16
|
-
結果
|
17
|
-
testユーザー発行
|
18
|
-
testユーザーに「testguoup」と「testguoup2」が割り当て
|
19
|
-
|
20
|
-
現状のPythonのソースコードは1行目だけを読み取り、yamlファイルの「user_name」に割り当てています。
|
21
|
-
そうではなく、テキストファイルの先頭が「NAME:」だった場合は、その後ろの文字列をyamlファイルの「user_name」に割り当てたいです。(グループはgroup_nameに割り当てたいです。)
|
22
|
-
プログラミング初心者で処理の仕方が分かりません。
|
23
|
-
ご教授お願いします。
|
24
|
-
|
25
|
-
|
26
|
-
### Lambda関数のソースコード(Python)
|
27
|
-
import boto3
|
28
|
-
import datetime
|
29
|
-
|
30
|
-
s3bucket = 'iam-user-bucket2'
|
31
|
-
cfn_template_yml = 'cfn_template/create-iam-user.yml'
|
32
|
-
application_file = 'application_file/iam_application_form.txt'
|
33
|
-
|
34
|
-
now = datetime.datetime.now()
|
35
|
-
datetime = now.strftime('%Y%m%d_%H%M%S')
|
36
|
-
|
37
|
-
def create_cfn_template(username):
|
38
|
-
s3 = boto3.resource('s3')
|
39
|
-
s3.Bucket(s3bucket).download_file(cfn_template_yml, f'/tmp/cfn_template_{datetime}')
|
40
|
-
|
41
|
-
with open(f'/tmp/cfn_template_{datetime}', 'r') as f:
|
42
|
-
file_text = f.read()
|
43
|
-
after = file_text.replace('user_name', username)
|
44
|
-
|
45
|
-
with open(f'/tmp/cfn_template_{datetime}', 'w') as f:
|
46
|
-
f.write(after)
|
47
|
-
|
48
|
-
launch_cfn(after, username)
|
49
|
-
|
50
|
-
def launch_cfn(cfn_template, username):
|
51
|
-
cfn = boto3.client('cloudformation')
|
52
|
-
cfn.create_stack(
|
53
|
-
StackName = f'iam-{username}-stack',
|
54
|
-
TemplateBody = cfn_template,
|
55
|
-
Capabilities = [
|
56
|
-
'CAPABILITY_NAMED_IAM',
|
57
|
-
]
|
58
|
-
)
|
59
|
-
|
60
|
-
def lambda_handler(event, context):
|
61
|
-
s3 = boto3.resource('s3')
|
62
|
-
s3.Bucket(s3bucket).download_file(application_file, f'/tmp/app_form_{datetime}')
|
63
|
-
|
64
14
|
with open(f'/tmp/app_form_{datetime}', 'r') as f:
|
65
15
|
username = f.readline()
|
66
16
|
|
67
|
-
create_cfn_template(username)
|
68
17
|
|
69
18
|
|
70
|
-
|
71
|
-
### Cloudformationのコード(yaml)
|
72
|
-
AWSTemplateFormatVersion: '2010-09-09'
|
73
|
-
Resources:
|
74
|
-
IAMUser:
|
75
|
-
Type: AWS::IAM::User
|
76
|
-
Properties:
|
77
|
-
UserName: user_name
|
78
|
-
Groups:
|
79
|
-
- group_name
|
80
|
-
- group_name2
|
81
|
-
LoginProfile:
|
82
|
-
Password: abc12345
|
83
|
-
PasswordResetRequired: True
|
84
|
-
|
85
|
-
###参考サイト
|
86
|
-
https://qiita.com/latin1/items/96be66d010bc1698fdcf
|
87
|
-
|
88
19
|
### 補足情報(FW/ツールのバージョンなど)
|
89
20
|
Python3.9
|