Skip to content

Commit 53b4b20

Browse files
aeghbali-mwGitHub Enterprise
authored and
GitHub Enterprise
committed
EBS Snapshot 1.0.1 (#27)
1 parent d99758e commit 53b4b20

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

aws/ebs-snapshot-lambda/v1/.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v1.0.0
1+
v1.0.1

aws/ebs-snapshot-lambda/v1/ebs-snapshot-lambda.yml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Parameters:
2828

2929
PreSnapshotCommand:
3030
Type: String
31-
Description: Optional SSM command to run before snapshot creation. Ensure your command can be executed in 60 seconds or less; otherwise, your command might time out, and the snapshot will be created without running the command. The command must be in the valid format '{{DocumentName}} COMMAND'. DocumentName must be either 'AWS-RunShellScript' or 'AWS-RunPowerShellScript'.
31+
Description: Optional SSM command to run before snapshot creation. Ensure that the EC2 instance is running and that your command can be executed in 60 seconds or less; otherwise, the snapshot will be created without running the command. The command must be in the valid format '{{DocumentName}} COMMAND'. DocumentName must be either 'AWS-RunShellScript' or 'AWS-RunPowerShellScript'.
3232
Default: ""
3333

3434
CustomExecutionRoleArn:
@@ -84,7 +84,7 @@ Resources:
8484
- logs:CreateLogStream
8585
- logs:PutLogEvents
8686
Resource:
87-
- !Sub arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/*MWSnapshotCreationFunction*
87+
- !Sub arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/*
8888
- !If
8989
- HasSSMCommand
9090
- Sid: SSMPermissions
@@ -133,9 +133,17 @@ Resources:
133133
product_id = event['ResourceProperties']['ProductId']
134134
tags_str = event['ResourceProperties'].get('Tags', '')
135135
136-
optional_ssm_command = event['ResourceProperties'].get('PreSnapshotCommand', '')
137-
_handle_optional_ssm_command(ec2_instance_id, optional_ssm_command)
138-
136+
ec2 = boto3.client('ec2')
137+
resp = ec2.describe_instances(InstanceIds=[ec2_instance_id])
138+
state = resp['Reservations'][0]['Instances'][0]['State']['Name']
139+
logger.info(f"EC2 instance {ec2_instance_id} is in state: {state}")
140+
141+
if state == 'running':
142+
optional_ssm_command = event['ResourceProperties'].get('PreSnapshotCommand', '')
143+
_handle_optional_ssm_command(ec2_instance_id, optional_ssm_command)
144+
else:
145+
logger.info(f"Skipped SSM command for EC2 instance {ec2_instance_id} in state: {state}")
146+
139147
snapshot_id = _create_snapshot(ec2_instance_id, volume_device_name, product_id, tags_str)
140148
message = f"Snapshot created successfully. Snapshot ID: {snapshot_id}"
141149
logger.info(message)

0 commit comments

Comments
 (0)