-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-Configure-Launch-Template-Test.py
executable file
·147 lines (127 loc) · 6.29 KB
/
1-Configure-Launch-Template-Test.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import csv
import boto3
from pprint import pprint
session = boto3.session.Session()
client_mgn = session.client('mgn')
client_ec2 = session.client('ec2')
input_file = csv.DictReader(open("./servers.csv"))
# For loop through CSV file
for each_row in input_file:
## Declare variables
server = (each_row["Server"])
subnet_id = (each_row["subnet_id"])
disk_type = (each_row["disk_type"])
iops = (each_row["iops"])
subnet_id = (each_row["subnet_id"])
securitygroup_id = (each_row["securitygroup_id"])
instanceType = (each_row["instanceType"])
ip_address = (each_row["ip_address"])
instance_role_arn = (each_row["instance_role_arn"])
tag_environment = (each_row["tag_environment"])
tag_application = (each_row["tag_application"])
my_list_of_dicts = []
source_servers = client_mgn.describe_source_servers()['items']
# For loop through all MGN source servers
for each_item in source_servers:
if each_item['tags']['Name'] == server:
print("\nGetting source server ID for {}...".format(server))
print(each_item['sourceServerID'])
# Update MGN Launch Configuration
update_launch_config = client_mgn.update_launch_configuration(
launchDisposition='STOPPED',
licensing={
'osByol': True
},
sourceServerID=each_item['sourceServerID'],
targetInstanceTypeRightSizingMethod='NONE'
)
print("/n")
pprint(update_launch_config)
print("\nGetting launch template ID for {}...".format(server))
get_launch_config = client_mgn.get_launch_configuration(
sourceServerID=each_item['sourceServerID'],
)
print(get_launch_config['ec2LaunchTemplateID'])
print("\nGetting latest template version for {}...".format(server))
launch_template_version = client_ec2.describe_launch_template_versions(
LaunchTemplateId=get_launch_config['ec2LaunchTemplateID'],
Versions=['$Latest']
)
for each_version in launch_template_version['LaunchTemplateVersions']:
print("Latest version is {}...".format(each_version['VersionNumber']))
for each_block_device in each_version['LaunchTemplateData']['BlockDeviceMappings']:
print("dev name is {}".format(each_block_device['DeviceName']))
print("size is {}".format(each_block_device['Ebs']['VolumeSize']))
print("type is {}".format(disk_type))
my_list_of_dicts.append({'DeviceName': each_block_device['DeviceName'],'Ebs': {'VolumeSize': each_block_device['Ebs']['VolumeSize'], 'Iops': int(iops), 'VolumeType': disk_type}})
create_launch_template = client_ec2.create_launch_template_version(
LaunchTemplateId=get_launch_config['ec2LaunchTemplateID'],
SourceVersion=str(each_version['VersionNumber']),
# SourceVersion="1",
LaunchTemplateData={
'BlockDeviceMappings': my_list_of_dicts
}
)
## Get latest launch template version
launch_template_version = client_ec2.describe_launch_template_versions(
LaunchTemplateId=get_launch_config['ec2LaunchTemplateID'],
Versions=['$Latest']
)
for each_version in launch_template_version['LaunchTemplateVersions']:
print("Latest version is {}...".format(each_version['VersionNumber']))
create_launch_template = client_ec2.create_launch_template_version(
LaunchTemplateId=get_launch_config['ec2LaunchTemplateID'],
SourceVersion=str(each_version['VersionNumber']),
LaunchTemplateData={
'IamInstanceProfile': {
'Arn': instance_role_arn
},
'NetworkInterfaces': [
{
'DeviceIndex': 0,
'Groups': [
securitygroup_id
],
'PrivateIpAddresses': [
{
'Primary': True,
'PrivateIpAddress': ip_address
},
],
'SubnetId': subnet_id
},
],
'TagSpecifications': [
{
'ResourceType': 'instance',
'Tags': [
{
'Key': 'Name',
'Value': server
},
{
'Key': 'tag_environment',
'Value': tag_environment
},
{
'Key': 'tag_application',
'Value': tag_application
},
]
},
],
'InstanceType': instanceType
}
)
## Get latest launch template version
launch_template_version = client_ec2.describe_launch_template_versions(
LaunchTemplateId=get_launch_config['ec2LaunchTemplateID'],
Versions=['$Latest']
)
for each_version in launch_template_version['LaunchTemplateVersions']:
print("Latest version is {}...".format(each_version['VersionNumber']))
## Setting new default version of launch template
set_default_version = client_ec2.modify_launch_template(
LaunchTemplateId=get_launch_config['ec2LaunchTemplateID'],
DefaultVersion=str(each_version['VersionNumber'])
)