-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.py
More file actions
executable file
·57 lines (51 loc) · 2.99 KB
/
Copy pathdeploy.py
File metadata and controls
executable file
·57 lines (51 loc) · 2.99 KB
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
#!/usr/bin/env python3
import argparse
from datetime import datetime
import boto3
import aws_cdl
parser = argparse.ArgumentParser(description='Tool to deploy ec2-ebs-cost-saving')
parser.add_argument('-p', '--profile', required=True, help='AWS CLI profile to use for the deplyoment')
parser.add_argument('-r', '--region', required=True, help='AWS Region to deploy this solution into')
parser.add_argument('-i', '--instanceid', required=True, help='Instance ID for which this solution should be used for')
parser.add_argument('-m', '--mountpoint', required=True, help='Mount point to mount Volume after restore')
parser.add_argument('-t', '--template', default='cfn_template.yaml' , help='Cloudformation Template file to use. Default: cfn_template.yaml')
parser.add_argument('-b', '--bucket', help='Bucket to upload the code to. Will be created if not existing. Files will be deleted from bucket after deployment. Defaults to <account-alias>-lambda-code')
parser.add_argument('-d', '--delete', action='store_true', help='If set, will delete the ressources that control the given instance')
parser.add_argument('-f', '--force', action='store_true', help='If set, will delete the stack before deployment. Necessary if previous deployment failed')
args = parser.parse_args()
stack_name = 'start-stop-%s' %(args.instanceid)
lambda_start_key = 'start-instance-%s' %(datetime.now().isoformat())
lambda_stop_key = 'stop-instance-%s' %(datetime.now().isoformat())
if not args.bucket:
response = boto3.Session(profile_name=args.profile, region_name = args.region).client('iam').list_account_aliases()
if response['AccountAliases'] == []:
response = boto3.Session(profile_name=args.profile, region_name = args.region).client('sts').get_caller_identity()
account_alias = response['Account']
else:
account_alias = response['AccountAliases'][0]
setattr(args, 'bucket', '%s-lambda-code' %(account_alias))
cfn_parameters = [
{'ParameterKey': 'InstanceId',
'ParameterValue': args.instanceid},
{'ParameterKey': 'MountPoint',
'ParameterValue': args.mountpoint},
{'ParameterKey': 'LambdaCodeBucket',
'ParameterValue': args.bucket},
{'ParameterKey': 'LambdaStartCodeKey',
'ParameterValue': lambda_start_key},
{'ParameterKey': 'LambdaStopCodeKey',
'ParameterValue': lambda_stop_key}
]
cfn_client = aws_cdl.create_cf_client(args.profile, args.region)
if args.delete:
aws_cdl.delete_stack(cfn_client, stack_name)
else:
aws_cdl.upload_lambda(args.profile, 'start-instance-lambda', args.bucket, lambda_start_key, args.region)
aws_cdl.upload_lambda(args.profile, 'stop-instance-lambda', args.bucket, lambda_stop_key, args.region)
try:
aws_cdl.create_update_stack(cfn_client, args.template, cfn_parameters, stack_name, args.force)
except Exception as e:
print(e)
finally:
aws_cdl.delete_lambda_package(args.profile, args.bucket, lambda_start_key, args.region)
aws_cdl.delete_lambda_package(args.profile, args.bucket, lambda_stop_key, args.region)