Skip to content

Commit f84ba7a

Browse files
authored
Merge pull request from devenes/test
Update app
2 parents c25f3ce + d5c0474 commit f84ba7a

File tree

2 files changed

+80
-6
lines changed

2 files changed

+80
-6
lines changed

README.md

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,31 @@
22

33
![api](./readme/boto3.jpg)
44

5-
### Python API uses Flask and Boto3 libraries. It has the features of Instance listing, Instance start and Instance stop; it has 3 endpoints communicate with EC2 service on AWS.
5+
### Python API uses the Flask and Boto3 libraries. It has instance listing, instance start, instance stop, instance create and instance terminate features; It has 5 endpoints communicating with EC2 service on AWS.
6+
7+
---
8+
9+
## Create Instance
10+
11+
You send a POST request to the `create` endpoint with the following parameters and it will create Ubuntu EC2 instance. You create your EC2 Instance with the `region_name`, `KeyName` and `SecurityGroupId` parameters you send in the request.
12+
13+
```
14+
http://<api_host>:<api_port>/ec2/create
15+
```
16+
17+
| Parameter | Type | Description |
18+
| :---------------------- | :------- | :-------------------- |
19+
| `aws_access_key_id` | `string` | AWS Access Key ID |
20+
| `aws_secret_access_key` | `string` | AWS Secret Access Key |
21+
| `region_name` | `string` | AWS Region Name |
22+
| `KeyName` | `string` | AWS Key Name |
23+
| `SecurityGroupId` | `string` | AWS Security Group ID |
624

725
---
826

927
## List Instances
1028

11-
You send a request to this endpoint by entering the following parameters, and you can list all the instances in your AWS account in the region you specify and their status.
29+
You send a GET request to the `list` endpoint by entering the following parameters, then you can list all the instances and their status in your AWS account in the region you specify.
1230

1331
```
1432
http://<api_host>:<api_port>/ec2/list
@@ -24,7 +42,7 @@ http://<api_host>:<api_port>/ec2/list
2442

2543
## Start Instance
2644

27-
You send a request to this endpoint by entering the following parameters, and you can start the instance you specified with `instance_id`, in the region you specified with `region_name` and the status of the instance will be changed to `running`.
45+
You send a POST request to the `start` endpoint by entering the following parameters, and you can start the instance you specified with `instance_id`, in the region you specified with `region_name` and the status of the instance will be changed to `pending` and then to `running`.
2846

2947
```
3048
http://<api_host>:<api_port>/ec2/start
@@ -41,7 +59,7 @@ http://<api_host>:<api_port>/ec2/start
4159

4260
## Stop Instance
4361

44-
You send a request to this endpoint by entering the following parameters, and you can stop the instance you specified with `instance_id`, in the region you specified with `region_name` and the status of the instance will be changed to `stopped`.
62+
You send a POST request to the `stop` endpoint by entering the following parameters, and you can stop the instance you specified with `instance_id`, in the region you specified with `region_name` and the status of the instance will be changed to `stopping` and then to `stopped`.
4563

4664
```
4765
http://<api_host>:<api_port>/ec2/stop
@@ -55,3 +73,18 @@ http://<api_host>:<api_port>/ec2/stop
5573
| `instance_id` | `string` | Instance ID Number |
5674

5775
---
76+
77+
## Terminate Instance
78+
79+
You send a POST request to the `terminate` endpoint by entering the following parameters, and you can terminate the instance you specified with `instance_id`, in the region you specified with `region_name` and the status of the instance will be changed to `shutting-down` and then `terminated`.
80+
81+
```
82+
http://<api_host>:<api_port>/ec2/terminate
83+
```
84+
85+
| Parameter | Type | Description |
86+
| :---------------------- | :------- | :-------------------- |
87+
| `aws_access_key_id` | `string` | AWS Access Key ID |
88+
| `aws_secret_access_key` | `string` | AWS Secret Access Key |
89+
| `region_name` | `string` | AWS Region Name |
90+
| `instance_id` | `string` | Instance ID Number |

app.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def get_aws_client(request):
2323

2424

2525
# Endpoint: http://<api_host>:<api_port>/ec2/list
26-
@ app.route('/ec2/list', methods=['GET'])
26+
@app.route('/ec2/list', methods=['GET'])
2727
def aws_list():
2828
try:
2929
client = get_aws_client(request)
@@ -90,7 +90,48 @@ def stop_ec2_instances():
9090
return jsonify({'Message': "Unexpected Error occured", 'Error': str(error)}), 500
9191

9292

93+
@app.route("/ec2/create", methods=["POST"])
94+
def create_ec2_instance():
95+
try:
96+
client = get_aws_client(request)
97+
KeyName = request.args.get("KeyName")
98+
SecurityGroupIds = request.args.get("SecurityGroupId")
99+
100+
response = client.run_instances(
101+
ImageId='ami-0fb653ca2d3203ac1', # Ubuntu Server 20.04, 64-bit x86
102+
InstanceType='t2.micro', # 1 vCPU, 1 GB RAM (Free tier ^^)
103+
MinCount=1,
104+
MaxCount=1,
105+
KeyName=KeyName,
106+
SecurityGroupIds=[SecurityGroupIds]
107+
)
108+
109+
print(response)
110+
return jsonify(response["Instances"][0]), 200
111+
112+
except Exception as error:
113+
print(str(error))
114+
return jsonify({'Message': "Unexpected Error occured", 'Error': str(error)}), 500
115+
116+
117+
@app.route("/ec2/terminate", methods=["POST"])
118+
def terminate_ec2_instance():
119+
try:
120+
client = get_aws_client(request)
121+
InstanceId = request.args.get("InstanceId")
122+
123+
response = client.terminate_instances(
124+
InstanceIds=[InstanceId]
125+
)
126+
127+
return jsonify(response["TerminatingInstances"][0]), 200
128+
129+
except Exception as error:
130+
print(str(error))
131+
return jsonify({'Message': "Unexpected Error occured", 'Error': str(error)}), 500
132+
133+
93134
if __name__ == "__main__":
94135
app.run(host=config["host"],
95136
port=config["port"],
96-
debug=False)
137+
debug=True)

0 commit comments

Comments
 (0)