Skip to content

Commit

Permalink
add terminate instance option
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanLaserGit committed Nov 14, 2024
1 parent a902222 commit ffc5214
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 27 deletions.
13 changes: 9 additions & 4 deletions research_datastream/terraform/GETTING_STARTED.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,19 @@ Starting from execution_template_general_purpose. Make sure to wrap commands in
```

### Edit Run Options
The state machine is capable of confirming a complete execution by checking for the existence output data in the form of an s3 object. Set booleans here. If `s3_bucket` and `s3_prefix` are provided in `datastream_command_options`, `ngen-datastream` will create a `ngen-run.tar.gz` file that can be found at `s3://<s3_bucket>/<s3_prefix>/ngen-run.tar.gz`. `timeout_s` is a timeout for the commands issued during execution. This is valuable for shutting down hanging instances that may become unresponsive due to memory overflow, etc. Default is 3600.
```
"run_options":{
"ii_delete_volume" : false,
"ii_check_s3" : true,
"timeout_s" : 3600
"ii_terminate_instance" : true,
"ii_delete_volume" : false,
"ii_check_s3" : true,
"timeout_s" : 3600
},
```
If `s3_bucket` and `s3_prefix` are provided in `datastream_command_options` and `ii_check_s3` is set to `true` , the state machine will confirm that at least one object exists at `s3://<s3_bucket>/<s3_prefix>/`.

`ii_terminate_instance` and `ii_delete_volume` allow the user to clean up AWS resources to avoid needless costs. While stopped instances do not incur costs, detached volumes do incur costs until deleted.

`timeout_s` is a timeout for the commands issued during execution. This is valuable for shutting down hanging instances that may become unresponsive due to memory overflow, etc. Default is 3600.

### Edit Instance Options
4) Define the AMI ID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
"s3_prefix" : "test_directory"
},
"run_options":{
"ii_delete_volume" : true,
"ii_check_s3" : true,
"timeout_s" : 3600
"ii_delete_volume" : true,
"ii_check_s3" : true,
"ii_terminate_instance" : false,
"timeout_s" : 3600
},
"instance_parameters": {
"ImageId": "ami-062bdcbb454b8d833",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
"runuser -l ec2-user -c 'ls -la'"
],
"run_options":{
"ii_delete_volume" : false,
"ii_check_s3" : false,
"timeout_s" : 3600
"ii_delete_volume" : false,
"ii_terminate_instance" : true,
"ii_check_s3" : false,
"timeout_s" : 3600
},
"instance_parameters" :
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
"s3_prefix" : ""
},
"run_options":{
"ii_delete_volume" : true,
"ii_check_s3" : true,
"timeout_s" : 3600
"ii_terminate_instance" : true,
"ii_delete_volume" : true,
"ii_check_s3" : true,
"timeout_s" : 3600
},
"instance_parameters": {
"ImageId": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ def confirm_detach(volume_id):
time.sleep(1)
else:
return

def confirm_instance_termination(instance_id):
while True:
response = client_ec2.describe_instances(
InstanceIds=[
instance_id
]
)
if response['Reservations'][0]['Instances'][0]['State']['Name'] != 'terminated':
print(f'Instance not yet terminated')
time.sleep(1)
else:
print(f'Instance {instance_id} terminated')
return

def lambda_handler(event, context):
"""
Expand All @@ -41,20 +55,28 @@ def lambda_handler(event, context):
],)
print(response)
volume_id=event['volume_id']
if event["run_options"]["ii_delete_volume"]:
print(f'Instance VolumeId {volume_id} located.')
response = client_ec2.detach_volume(
InstanceId=instance_id,
VolumeId=volume_id,
DryRun=False
if event["run_options"]["ii_terminate_instance"]:
response = client_ec2.terminate_instances(
InstanceIds=[
instance_id,
],
)
confirm_detach(volume_id)
print(f'EBS volume {instance_id} has been successfully detached.')
response = client_ec2.delete_volume(
VolumeId=volume_id,
DryRun=False
)
print(f'EBS volume {instance_id} has been successfully deleted.')
confirm_instance_termination(instance_id)
else:
print(f"Volume {volume_id} remains attached or available and is still incurring costs.")
if event["run_options"]["ii_delete_volume"]:
print(f'Instance VolumeId {volume_id} located.')
response = client_ec2.detach_volume(
InstanceId=instance_id,
VolumeId=volume_id,
DryRun=False
)
confirm_detach(volume_id)
print(f'EBS volume {instance_id} has been successfully detached.')
response = client_ec2.delete_volume(
VolumeId=volume_id,
DryRun=False
)
print(f'EBS volume {volume_id} has been successfully deleted.')
else:
print(f"Volume {volume_id} remains attached or available and is still incurring costs.")

1 change: 1 addition & 0 deletions research_datastream/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ resource "aws_iam_policy" "datastreamlambda_policy" {
"ec2:StartInstances",
"ec2:StopInstances",
"ec2:DescribeInstances",
"ec2:TerminateInstances",
"ec2:DescribeVolumes",
"ec2:DeleteVolume",
"ec2:DetachVolume",
Expand Down

0 comments on commit ffc5214

Please sign in to comment.