-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_instance.py
More file actions
55 lines (43 loc) · 1.91 KB
/
create_instance.py
File metadata and controls
55 lines (43 loc) · 1.91 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
import logging
import boto3
from botocore.exceptions import ClientError
def create_ec2_instance(image_id, instance_type, keypair_name):
"""Provision and launch an EC2 instance
The method returns without waiting for the instance to reach
a running state.
:param image_id: ID of AMI to launch, such as 'ami-XXXX'
:param instance_type: string, such as 't2.micro'
:param keypair_name: string, name of the key pair
:return Dictionary containing information about the instance. If error,
returns None.
"""
# Provision and launch the EC2 instance
ec2_client = boto3.client('ec2')
try:
response = ec2_client.run_instances(ImageId=image_id,
InstanceType=instance_type,
KeyName=keypair_name,
MinCount=1,
MaxCount=1)
except ClientError as e:
logging.error(e)
return None
return response['Instances'][0]
def main():
"""Exercise create_ec2_instance()"""
# Assign these values before running the program
image_id = 'AMI_IMAGE_ID'
instance_type = 'INSTANCE_TYPE'
keypair_name = 'KEYPAIR_NAME'
# Set up logging
logging.basicConfig(level=logging.DEBUG,
format='%(levelname)s: %(asctime)s: %(message)s')
# Provision and launch the EC2 instance
instance_info = create_ec2_instance(image_id, instance_type, keypair_name)
if instance_info is not None:
logging.info(f'Launched EC2 Instance {instance_info["InstanceId"]}')
logging.info(f' VPC ID: {instance_info["VpcId"]}')
logging.info(f' Private IP Address: {instance_info["PrivateIpAddress"]}')
logging.info(f' Current State: {instance_info["State"]["Name"]}')
if __name__ == '__main__':
main()