Skip to content

Commit 0792b38

Browse files
feat: Add a new ec2 restart option that can also change the instance type (#487)
1 parent 66e7f91 commit 0792b38

File tree

5 files changed

+71
-17
lines changed

5 files changed

+71
-17
lines changed

docs/ami.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ aec ami describe
3939
4040
Name ImageId CreationDate RootDeviceName Size
4141
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
42-
ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-20170727 ami-1e749f67 2025-04-01T09:46:15.000Z /dev/sda1 15
43-
ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20170721 ami-785db401 2025-04-01T09:46:15.000Z /dev/sda1 15
42+
ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-20170727 ami-1e749f67 2025-04-01T22:53:13.000Z /dev/sda1 15
43+
ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20170721 ami-785db401 2025-04-01T22:53:13.000Z /dev/sda1 15
4444
```
4545
<!-- [[[end]]] -->
4646

docs/ec2.md

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ from aec.main import build_parser
1515
cog.out(f"```\n{build_parser()._subparsers._actions[1].choices['ec2'].format_help()}```")
1616
]]] -->
1717
```
18-
usage: aec ec2 [-h]
19-
{create-key-pair,describe,launch,logs,modify,start,stop,sec-groups,subnets,rename,tag,tags,status,templates,terminate,user-data} ...
18+
usage: aec ec2 [-h] {create-key-pair,describe,launch,logs,modify,start,stop,restart,sec-groups,subnets,rename,tag,tags,status,templates,terminate,user-data} ...
2019
2120
optional arguments:
2221
-h, --help show this help message and exit
2322
2423
subcommands:
25-
{create-key-pair,describe,launch,logs,modify,start,stop,sec-groups,subnets,rename,tag,tags,status,templates,terminate,user-data}
24+
{create-key-pair,describe,launch,logs,modify,start,stop,restart,sec-groups,subnets,rename,tag,tags,status,templates,terminate,user-data}
2625
create-key-pair Create a key pair.
2726
describe List EC2 instances in the region.
2827
launch Launch a tagged EC2 instance with an EBS volume.
2928
logs Show the system logs.
3029
modify Change an instance's type.
3130
start Start EC2 instance.
3231
stop Stop EC2 instance.
32+
restart Restart EC2 instance, optionally changing the instance type.
3333
sec-groups Describe security groups in the region, optionally filtered by VPC ID.
3434
subnets Describe subnets.
3535
rename Rename EC2 instance(s).
@@ -72,6 +72,11 @@ Start a stopped instance, and wait for the SSM agent to come online (at which po
7272
aec ec2 start "lady gaga" -w
7373
```
7474

75+
Restart an instance and change the instance type:
76+
```
77+
aec ec2 restart "lady gaga" -t m5.large
78+
```
79+
7580
List all instances in the region:
7681

7782
<!-- [[[cog
@@ -82,8 +87,8 @@ aec ec2 describe
8287
8388
InstanceId State Name Type DnsName LaunchTime ImageId
8489
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
85-
i-d11c784672f583196 running alice t3.small ec2-54-214-8-190.compute-1.amazonaws.com 2025-04-01 09:46:15+00:00 ami-03cf127a
86-
i-919d8e2adf1445b8d running sam t3.small ec2-54-214-106-199.compute-1.amazonaws.com 2025-04-01 09:46:16+00:00 ami-03cf127a
90+
i-a6c907a7659c657ec running alice t3.small ec2-54-214-143-173.compute-1.amazonaws.com 2025-04-01 22:53:13+00:00 ami-03cf127a
91+
i-10775ea95215edd19 running sam t3.small ec2-54-214-92-78.compute-1.amazonaws.com 2025-04-01 22:53:13+00:00 ami-03cf127a
8792
```
8893
<!-- [[[end]]] -->
8994

@@ -121,8 +126,8 @@ aec ec2 describe -c Name,SubnetId,Volumes,Image.CreationDate
121126
122127
Name SubnetId Volumes Image.CreationDate
123128
───────────────────────────────────────────────────────────────────────────────
124-
alice subnet-3c34502dfd1bc971e ['Size=15 GiB'] 2025-04-01T09:46:15.000Z
125-
sam subnet-3c34502dfd1bc971e ['Size=15 GiB'] 2025-04-01T09:46:15.000Z
129+
alice subnet-7c7eebe4933bd527a ['Size=15 GiB'] 2025-04-01T22:53:13.000Z
130+
sam subnet-7c7eebe4933bd527a ['Size=15 GiB'] 2025-04-01T22:53:13.000Z
126131
```
127132
<!-- [[[end]]] -->
128133

@@ -217,12 +222,12 @@ aec ec2 subnets
217222
218223
SubnetId VpcId AvailabilityZone CidrBlock Name
219224
─────────────────────────────────────────────────────────────────────────────────────────────
220-
subnet-3c34502dfd1bc971e vpc-b1f00f09091896c57 us-east-1a 172.31.0.0/20
221-
subnet-569837d96526af096 vpc-b1f00f09091896c57 us-east-1b 172.31.16.0/20
222-
subnet-8a3b095072e46bf09 vpc-b1f00f09091896c57 us-east-1c 172.31.32.0/20
223-
subnet-c3e0557e8334ec7b5 vpc-b1f00f09091896c57 us-east-1d 172.31.48.0/20
224-
subnet-67ffe1d070fd23148 vpc-b1f00f09091896c57 us-east-1e 172.31.64.0/20
225-
subnet-fbfe5e577853e5221 vpc-b1f00f09091896c57 us-east-1f 172.31.80.0/20
225+
subnet-7c7eebe4933bd527a vpc-2d040ead29eee6cf8 us-east-1a 172.31.0.0/20
226+
subnet-3961372401467a0d6 vpc-2d040ead29eee6cf8 us-east-1b 172.31.16.0/20
227+
subnet-c174b5405eaf42d81 vpc-2d040ead29eee6cf8 us-east-1c 172.31.32.0/20
228+
subnet-4d930d9329f0bcd9b vpc-2d040ead29eee6cf8 us-east-1d 172.31.48.0/20
229+
subnet-6029fe0fac51d968a vpc-2d040ead29eee6cf8 us-east-1e 172.31.64.0/20
230+
subnet-1dd87fc3fe137e5aa vpc-2d040ead29eee6cf8 us-east-1f 172.31.80.0/20
226231
```
227232
<!-- [[[end]]] -->
228233

@@ -236,8 +241,8 @@ aec ec2 sec-groups
236241
237242
GroupId GroupName Description VpcId
238243
───────────────────────────────────────────────────────────────────────────────────────
239-
sg-25d2fabfa34eaa065 default default VPC security group vpc-b1f00f09091896c57
240-
default default default vpc-b1f00f09091896c57
244+
sg-af760af3a4fc243fe default default VPC security group vpc-2d040ead29eee6cf8
245+
default default default vpc-2d040ead29eee6cf8
241246
```
242247
<!-- [[[end]]] -->
243248

src/aec/command/ec2.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,24 @@ def modify(config: Config, ident: str, type: str) -> list[Instance]:
510510
return describe(config, ident)
511511

512512

513+
def restart(config: Config, ident: str, type: str | None = None, wait_ssm: bool = False) -> list[Instance]:
514+
"""Restart EC2 instance, optionally changing the instance type."""
515+
print(f"Stopping instance {ident}")
516+
stop(config, ident)
517+
518+
instance_status = describe(config, ident)[0]["State"]
519+
while instance_status != "stopped":
520+
print(f"Waiting for instance {ident} to stop ...")
521+
sleep(5)
522+
instance_status = describe(config, ident)[0]["State"]
523+
524+
if type:
525+
print(f"Changing instance type to {type}")
526+
modify(config, ident, type)
527+
528+
return start(config, ident, wait_ssm=wait_ssm)
529+
530+
513531
def create_key_pair(config: Config, key_name: str, file_path: str) -> str:
514532
"""Create a key pair."""
515533

src/aec/main.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ def tag_arg_checker(tag: str) -> str:
8686
config_arg,
8787
Arg("ident", type=str, help="Name tag of instance or instance id")
8888
]),
89+
Cmd(ec2.restart, [
90+
config_arg,
91+
Arg("ident", type=str, help="Name tag of instance or instance id"),
92+
Arg("-t", "--type", type=str, help="Modify the instance to the given type"),
93+
Arg("-w", "--wait-ssm", action='store_true', help="Wait until the SSM agent is online before exiting"),
94+
]),
8995
Cmd(ec2.sec_groups, [
9096
config_arg,
9197
Arg("-v", "--vpc-id", help="Filter to these VPCs"),

tests/test_ec2.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
logs,
2121
modify,
2222
rename,
23+
restart,
2324
sec_groups,
2425
start,
2526
status,
@@ -354,6 +355,30 @@ def test_stop_start(mock_aws_config: Config):
354355
start(mock_aws_config, ident="alice")
355356

356357

358+
def test_restart_new_type(mock_aws_config: Config):
359+
launch(mock_aws_config, "alice", ami_id)
360+
361+
result = restart(mock_aws_config, ident="alice", type="new_type")
362+
363+
assert result[0]["Name"] == "alice"
364+
assert result[0]["Type"] == "new_type"
365+
assert result[0]["State"] == "running"
366+
367+
assert len(result) == 1
368+
369+
370+
def test_restart_same_type(mock_aws_config: Config):
371+
launch(mock_aws_config, "alice", ami_id)
372+
373+
result = restart(mock_aws_config, ident="alice")
374+
375+
assert result[0]["Name"] == "alice"
376+
assert result[0]["Type"] == "t3.small"
377+
assert result[0]["State"] == "running"
378+
379+
assert len(result) == 1
380+
381+
357382
def test_subnets(mock_aws_config: Config):
358383
assert len(subnets(mock_aws_config)) == 6
359384
assert len(subnets(mock_aws_config, vpc_id="foobar")) == 0

0 commit comments

Comments
 (0)