forked from techiescamp/python-for-devops
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-instance.py
More file actions
139 lines (118 loc) · 4.54 KB
/
create-instance.py
File metadata and controls
139 lines (118 loc) · 4.54 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
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
#! /bin/python3
import boto3, os
from botocore.exceptions import ClientError
vpc_id = "vpc-0d42bf2f27be967ff"
subnet_id = "subnet-00b5ede5e160caa59"
ami_id = "ami-0ddf424f81ddb0720"
instance_type = "t2.small"
app_name = "flask"
create_key = True
key_name = "dev-key"
key_location = "/Users/bibinwilson/.ssh/devops-class/"
ec2 = boto3.client('ec2')
def createSecurityGroup():
global security_group_id
try:
response = ec2.create_security_group(GroupName=app_name + "-sg",
Description=app_name + " Security Group",
VpcId=vpc_id,
TagSpecifications=[
{
'ResourceType': 'security-group',
'Tags': [
{
'Key': 'Name',
'Value': app_name + "-sg"
}
]
},
],
)
security_group_id = response['GroupId']
print('Security Group Created %s in vpc %s.' % (security_group_id, vpc_id))
ingress = ec2.authorize_security_group_ingress(
GroupId=security_group_id,
IpPermissions=[
{
'IpProtocol': 'tcp',
'FromPort': 80,
'ToPort': 80,
'IpRanges': [
{
'CidrIp': '0.0.0.0/0'
}
]
},
{
'IpProtocol': 'tcp',
'FromPort': 22,
'ToPort': 22,
'IpRanges': [
{
'CidrIp': '0.0.0.0/0'
}
]
}
])
print('Ingress Successfully Set %s' % ingress)
except ClientError as e:
print(e)
def createKeyPair():
try:
key_pair = ec2.create_key_pair(KeyName=key_name)
ssh_private_key = key_pair["KeyMaterial"]
with os.fdopen(os.open(key_location + key_name + ".pem", os.O_WRONLY | os.O_CREAT, 0o400), "w+") as handle:
handle.write(ssh_private_key)
except ClientError as e:
print(e)
def createInstance():
blockDeviceMappings = [
{
'DeviceName': "/dev/sda1",
'Ebs': {
'DeleteOnTermination': True,
'VolumeSize': 20,
'VolumeType': 'gp2'
}
},
]
instances = ec2.run_instances(
ImageId= ami_id,
MinCount=1,
MaxCount=1,
InstanceType=instance_type,
SubnetId=subnet_id,
KeyName=key_name,
SecurityGroupIds=[security_group_id],
BlockDeviceMappings=blockDeviceMappings,
TagSpecifications=[
{
'ResourceType': 'instance',
'Tags': [
{
'Key': 'Name',
'Value': app_name + "-server"
}
]
},
{
'ResourceType': 'volume',
'Tags': [
{
'Key': 'Name',
'Value': app_name + "-root-disk"
}
]
}
]
)
print(instances["Instances"][0]["InstanceId"])
if __name__ == "__main__":
createSecurityGroup()
if create_key == True:
createKeyPair();
createInstance()
# References
# [1] https://codeflex.co/boto3-create-ec2-with-tags/
# [2] https://www.learnaws.org/2020/12/16/aws-ec2-boto3-ultimate-guide/
# [3] https://arjunmohnot.medium.com/aws-ec2-management-with-python-and-boto-3-59d849f1f58f