You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description: Creates EBS snapshot of an EC2 volume when stack is deleted.
3
+
4
+
Parameters:
5
+
EC2InstanceId:
6
+
Type: String
7
+
Description: The ID of the EC2 instance.
8
+
AllowedPattern: ^i-[a-zA-Z0-9]{8,17}$
9
+
ConstraintDescription: Must be a valid EC2 instance ID starting with 'i-' followed by 8-17 alphanumeric characters
10
+
11
+
VolumeDeviceName:
12
+
Type: String
13
+
Description: The device name of the volume attached to the EC2 instance.
14
+
AllowedPattern: ^/dev/[a-zA-Z0-9]+$
15
+
ConstraintDescription: Must be a valid device name starting with '/dev/'
16
+
17
+
ProductId:
18
+
Type: String
19
+
Description: The MathWorks product ID associated with the EC2 instance.
20
+
MinLength: 1
21
+
MaxLength: 128
22
+
AllowedPattern: ^[a-zA-Z0-9\-_]+$
23
+
ConstraintDescription: Product ID must be 1-128 alphanumeric characters, hyphens or underscores
24
+
25
+
Tags:
26
+
Type: String
27
+
Description: Tags to add to the snapshot in the form of "<key1>=<value1>,<key2>=<value2>". Can be empty.
28
+
29
+
PreSnapshotCommand:
30
+
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'.
32
+
Default: ""
33
+
34
+
CustomExecutionRoleArn:
35
+
Type: String
36
+
Default: ""
37
+
Description: (Optional) ARN of an existing IAM role to be used by the lambda function. Make sure that the IAM role has the necessary permissions to create EBS snapshots. If you leave this parameter blank, a new IAM role will be created.
existing_keys = {tag['Key'] for tag in tags} # Set of existing keys to check for duplicates
212
+
additional_tags = [
213
+
{'Key': k, 'Value': v}
214
+
for k, v in (tag.split('=') for tag in tags_str.split(','))
215
+
if k not in existing_keys
216
+
]
217
+
tags.extend(additional_tags)
218
+
219
+
logger.info(f"Creating snapshot for Volume ID: {volume_id} with tags: {tags}")
220
+
221
+
snapshot = ec2.create_snapshot(
222
+
VolumeId=volume_id,
223
+
Description= f"This snapshot was created by MathWorks IaC when you deleted your stack for the product: '{product_id}'. Check the tags to learn more about the deleted stack.",
raise Exception("Document name or command couldn't be fetched successfully. Make sure 'PreSnapshotCommand' is in valid form of '{{DocumentName}}COMMAND'.")
0 commit comments