forked from Digital-Asset-Developer-Resources/aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AMI.py
139 lines (105 loc) · 3.58 KB
/
AMI.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
#!/usr/bin/env python
# coding: utf-8
# ## AWS AMI
#
# 1. Import boto3 package
# 2. Read *blockchain-nodes* profile found in ~.aws/credentials
# 3. Create a EC2 session for instance creation
# In[ ]:
import boto3
import os
session = boto3.Session(profile_name='blockchain-nodes')
ec2 = session.resource('ec2')
# ## Helper functions to find existing subnets and security group IDs.
#
# See AWS VPC notebook for creation of these resources.
# In[ ]:
def get_subnet_id(AVZONE=session.region_name+'b',
subnet_type='public'):
for vpc in ec2.vpcs.all():
for az in ec2.meta.client.describe_availability_zones()["AvailabilityZones"]:
for subnet in vpc.subnets.filter(Filters=[{"Name": "availabilityZone", "Values": [az["ZoneName"]]}]):
#print(vpc.id, az["ZoneName"], subnet.id, subnet.tags[0]['Value'])
if (az["ZoneName"] == AVZONE) & (subnet_type in subnet.tags[0]['Value']):
return vpc.id, subnet.id
def get_security_group_id(session,VPC_ID,SECURITYGROUP_NAME):
client = boto3.client("ec2", region_name=session.region_name)
return client.describe_security_groups(Filters = [{"Name":"vpc-id",
"Values":[VPC_ID]
},{
"Name":"group-name",
"Values":[SECURITYGROUP_NAME]
}])\
['SecurityGroups'][0]['GroupId']
# ## Set Variables
#
# 1. tags
# 2. selecting key pair pem file
# 3. vpc
# 4. subnet
# 5. IAM role
# 6. security group
# 7. availability zone
# 8. instance type
# 9. node disk size (Gb)
#
# In[ ]:
BLOCKCHAIN_ID = "YOUR_BLOCKCHAIN"
AMI_ID = 'ami-0bc54c49c084b5342' ##This changes every periodically for every chain!!!
KEY_NAME = "blockchain-nodes-keypair"
SECURITYGROUP_NAME = "blockchain-nodes-sg"
DATAVOLUME_NAME = "YOUR BLOCKCHAIN Mainnet/PrivateNet/Testnet/Devnet Chain Data"
DATAVOLUME_SIZE = 100
INSTANCE_NAME = 'YOUR BLOCKCHAIN Mainnet/PrivateNet/Testnet/Devnet'
Instance_Type = 't2.medium' #micro=1GB, small=2GB, medium=4GB, large=8GB, etc.
IAM_ROLE = "blockchain-node-role"
AVZONE = session.region_name+'b'
VPC_ID, SUBNET_ID = get_subnet_id(AVZONE,"public")
SECURITY_GROUP_ID = get_security_group_id(session,VPC_ID,SECURITYGROUP_NAME)
USERDATA = '''#!/bin/bash
sudo mount /dev/xvdf /data
'''
# ## Launch Existing AMI
#
# User data necessary for mounting blockchain data volume.
#
# **TO DO** create entry in /etc/fstab during EC2 node creation to remove USERDATA.
#
# In[ ]:
instances = ec2.create_instances(
ImageId=AMI_ID,
MinCount=1,
MaxCount=1,
UserData=USERDATA,
IamInstanceProfile={
'Name': IAM_ROLE
},
BlockDeviceMappings=[
{
'DeviceName': '/dev/xvdf',
'Ebs': {
'DeleteOnTermination': True,
'VolumeSize': DATAVOLUME_SIZE,
'VolumeType': 'gp2'
},
},
],
InstanceType=Instance_Type,
KeyName=KEY_NAME,
Placement={'AvailabilityZone':AVZONE},
TagSpecifications=[
{
'ResourceType': 'instance',
'Tags': [
{
'Key': 'Name',
'Value': INSTANCE_NAME
},
]
},
],
NetworkInterfaces=[{'SubnetId': SUBNET_ID,
'DeviceIndex': 0,
'AssociatePublicIpAddress': True,
'Groups': [SECURITY_GROUP_ID]}])
# In[ ]: