Skip to content
This repository was archived by the owner on Sep 26, 2020. It is now read-only.

Commit fa9cb61

Browse files
committed
Separate ensure_ec2_instance_profile from ensure_ec2_role
1 parent fa46941 commit fa9cb61

File tree

1 file changed

+42
-10
lines changed

1 file changed

+42
-10
lines changed

axon/client.py

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ def make_client(name, region):
3131
return boto3.client(name, region_name=region)
3232

3333

34+
def make_resource(name):
35+
return boto3.resource(name)
36+
37+
3438
def ensure_log_group(group_name, region):
3539
"""
3640
Ensures that a log group is present. If there is a matching log group, nothing is created.
@@ -208,11 +212,12 @@ def ensure_role(client, role_name):
208212
def ensure_task_role(region):
209213
"""
210214
Ensures a task role exists. If there is one matching role, its Arn is returned. If there are
211-
multiple matching roles, a RuntimeError is raised. If there are no matching roles, a new one
215+
multiple matching roles, a `RuntimeError` is raised. If there are no matching roles, a new one
212216
is created.
213217
214-
TODO: Fix this.
218+
TODO: Fix this:
215219
This method does not check that a matching role has the correct policies.
220+
216221
:param region: The region, or `None` to pull the region from the environment.
217222
:return: The role Arn.
218223
"""
@@ -253,9 +258,14 @@ def ensure_task_role(region):
253258
return role_arn
254259

255260

256-
def ensure_ec2_role(region):
257-
role_name = "axon-ec2-autogenerated-role"
258-
profile_name = "axon-ec2-autogenerated-instance-profile"
261+
def ensure_ec2_role(region, role_name="axon-ec2-autogenerated-role"):
262+
"""
263+
Ensures the EC2 role exists. Creates the role if it does not exist.
264+
265+
:param region: The region, or `None` to pull the region from the environment.
266+
:param role_name: The name of the role to ensure.
267+
:return: The role Arn.
268+
"""
259269
client = make_client("iam", region)
260270
role_arn = ensure_role(client, role_name)
261271
if role_arn is None:
@@ -282,15 +292,36 @@ def ensure_ec2_role(region):
282292
client.attach_role_policy(RoleName=role_name,
283293
PolicyArn="arn:aws:iam::aws:policy/AmazonS3FullAccess")
284294

295+
return role_arn
296+
297+
298+
def ensure_ec2_instance_profile(region, profile_name="axon-ec2-autogenerated-instance-profile",
299+
role_name="axon-ec2-autogenerated-role"):
300+
"""
301+
Ensures the EC2 instance profile exists and has the EC2 role attached.
302+
303+
:param region: The region, or `None` to pull the region from the environment.
304+
:param profile_name: The name of the instance profile to ensure.
305+
:param role_name: The name of the role to ensure.
306+
:return: The instance profile Arn.
307+
"""
308+
client = make_client("iam", region)
309+
iam_resource = make_resource('iam')
310+
311+
# Get or create the instance profile
285312
try:
286-
client.get_instance_profile(InstanceProfileName=profile_name)
313+
local_profile = client.get_instance_profile(InstanceProfileName=profile_name)
287314
except:
288-
client.create_instance_profile(InstanceProfileName=profile_name)
289-
iam_resource = boto3.resource('iam')
290-
instance_profile = iam_resource.InstanceProfile(profile_name)
315+
local_profile = client.create_instance_profile(InstanceProfileName=profile_name)
316+
317+
instance_profile = iam_resource.InstanceProfile(
318+
local_profile['InstanceProfile']['InstanceProfileName'])
319+
320+
if role_name not in [role.name for role in instance_profile.roles]:
321+
# Add the role if it does not exist
291322
instance_profile.add_role(RoleName=role_name)
292323

293-
return role_arn
324+
return instance_profile.arn
294325

295326

296327
def ensure_cluster(ecs_client, cluster_name):
@@ -395,6 +426,7 @@ def impl_ensure_configuration(cluster_name, task_family, region):
395426
ensure_ecs_security_group(region)
396427
ensure_task_role(region)
397428
ensure_ec2_role(region)
429+
ensure_ec2_instance_profile(region)
398430

399431

400432
def impl_start_task(cluster_name, task_family, revision, region):

0 commit comments

Comments
 (0)