|
| 1 | +--- |
| 2 | +display_name: AWS AMI Snapshot |
| 3 | +description: Create and manage AMI snapshots for Coder workspaces with restore capabilities |
| 4 | +icon: ../../../../.icons/aws.svg |
| 5 | +verified: false |
| 6 | +tags: [aws, snapshot, ami, backup, persistence] |
| 7 | +--- |
| 8 | + |
| 9 | +# AWS AMI Snapshot Module |
| 10 | + |
| 11 | +This module provides AMI-based snapshot functionality for Coder workspaces running on AWS EC2 instances. It enables users to create snapshots when workspaces are stopped and restore from previous snapshots when starting workspaces. |
| 12 | + |
| 13 | +```tf |
| 14 | +module "ami_snapshot" { |
| 15 | + source = "registry.coder.com/mavrickrishi/aws-ami-snapshot/coder" |
| 16 | + version = "1.0.0" |
| 17 | +
|
| 18 | + instance_id = aws_instance.workspace.id |
| 19 | + default_ami_id = data.aws_ami.ubuntu.id |
| 20 | + template_name = "aws-linux" |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +## Features |
| 25 | + |
| 26 | +- **Automatic Snapshots**: Create AMI snapshots when workspaces are stopped |
| 27 | +- **User Control**: Enable/disable snapshot functionality per workspace |
| 28 | +- **Custom Labels**: Add custom labels to snapshots for easy identification |
| 29 | +- **Snapshot Selection**: Choose from available snapshots when starting workspaces |
| 30 | +- **Automatic Cleanup**: Optional Data Lifecycle Manager integration for automated cleanup |
| 31 | +- **Workspace Isolation**: Snapshots are tagged and filtered by workspace and owner |
| 32 | + |
| 33 | +## Parameters |
| 34 | + |
| 35 | +The module exposes the following parameters to workspace users: |
| 36 | + |
| 37 | +- `enable_snapshots`: Enable/disable AMI snapshot creation (default: true) |
| 38 | +- `snapshot_label`: Custom label for the snapshot (optional) |
| 39 | +- `use_previous_snapshot`: Select a previous snapshot to restore from (default: none) |
| 40 | + |
| 41 | +## Usage |
| 42 | + |
| 43 | +### Basic Usage |
| 44 | + |
| 45 | +```hcl |
| 46 | +module "ami_snapshot" { |
| 47 | + source = "registry.coder.com/modules/aws-ami-snapshot" |
| 48 | +
|
| 49 | + instance_id = aws_instance.workspace.id |
| 50 | + default_ami_id = data.aws_ami.ubuntu.id |
| 51 | + template_name = "aws-linux" |
| 52 | +} |
| 53 | +
|
| 54 | +resource "aws_instance" "workspace" { |
| 55 | + ami = module.ami_snapshot.ami_id |
| 56 | + instance_type = "t3.micro" |
| 57 | +
|
| 58 | + # Prevent Terraform from recreating instance when AMI changes |
| 59 | + lifecycle { |
| 60 | + ignore_changes = [ami] |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +### With Optional Cleanup |
| 66 | + |
| 67 | +```hcl |
| 68 | +module "ami_snapshot" { |
| 69 | + source = "registry.coder.com/modules/aws-ami-snapshot" |
| 70 | +
|
| 71 | + instance_id = aws_instance.workspace.id |
| 72 | + default_ami_id = data.aws_ami.ubuntu.id |
| 73 | + template_name = "aws-linux" |
| 74 | + enable_dlm_cleanup = true |
| 75 | + dlm_role_arn = aws_iam_role.dlm_lifecycle_role.arn |
| 76 | + snapshot_retention_count = 5 |
| 77 | +
|
| 78 | + tags = { |
| 79 | + Environment = "development" |
| 80 | + Project = "my-project" |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +### IAM Role for DLM (Optional) |
| 86 | + |
| 87 | +If using automatic cleanup, create an IAM role for Data Lifecycle Manager: |
| 88 | + |
| 89 | +```hcl |
| 90 | +resource "aws_iam_role" "dlm_lifecycle_role" { |
| 91 | + name = "dlm-lifecycle-role" |
| 92 | +
|
| 93 | + assume_role_policy = jsonencode({ |
| 94 | + Version = "2012-10-17" |
| 95 | + Statement = [ |
| 96 | + { |
| 97 | + Action = "sts:AssumeRole" |
| 98 | + Effect = "Allow" |
| 99 | + Principal = { |
| 100 | + Service = "dlm.amazonaws.com" |
| 101 | + } |
| 102 | + } |
| 103 | + ] |
| 104 | + }) |
| 105 | +} |
| 106 | +
|
| 107 | +resource "aws_iam_role_policy_attachment" "dlm_lifecycle" { |
| 108 | + role = aws_iam_role.dlm_lifecycle_role.name |
| 109 | + policy_arn = "arn:aws:iam::aws:policy/service-role/AWSDataLifecycleManagerServiceRole" |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +## Required IAM Permissions |
| 114 | + |
| 115 | +Users need the following IAM permissions for full functionality: |
| 116 | + |
| 117 | +```json |
| 118 | +{ |
| 119 | + "Version": "2012-10-17", |
| 120 | + "Statement": [ |
| 121 | + { |
| 122 | + "Effect": "Allow", |
| 123 | + "Action": [ |
| 124 | + "ec2:CreateImage", |
| 125 | + "ec2:DescribeImages", |
| 126 | + "ec2:DescribeInstances", |
| 127 | + "ec2:CreateTags", |
| 128 | + "ec2:DescribeTags" |
| 129 | + ], |
| 130 | + "Resource": "*" |
| 131 | + }, |
| 132 | + { |
| 133 | + "Effect": "Allow", |
| 134 | + "Action": [ |
| 135 | + "dlm:CreateLifecyclePolicy", |
| 136 | + "dlm:GetLifecyclePolicy", |
| 137 | + "dlm:UpdateLifecyclePolicy", |
| 138 | + "dlm:DeleteLifecyclePolicy" |
| 139 | + ], |
| 140 | + "Resource": "*", |
| 141 | + "Condition": { |
| 142 | + "StringEquals": { |
| 143 | + "dlm:Target": "INSTANCE" |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + ] |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +## How It Works |
| 152 | + |
| 153 | +1. **Snapshot Creation**: When a workspace transitions to "stop", an AMI snapshot is automatically created (if enabled) |
| 154 | +2. **Tagging**: Snapshots are tagged with workspace name, owner, template, and custom labels |
| 155 | +3. **Snapshot Retrieval**: Available snapshots are retrieved and presented as options for workspace start |
| 156 | +4. **AMI Selection**: The module outputs the appropriate AMI ID (default or selected snapshot) |
| 157 | +5. **Cleanup**: Optional DLM policies can automatically clean up old snapshots |
| 158 | + |
| 159 | +## Considerations |
| 160 | + |
| 161 | +- **Cost**: AMI snapshots incur storage costs. Use cleanup policies to manage costs |
| 162 | +- **Time**: AMI creation takes time; workspace stop operations may take longer |
| 163 | +- **Permissions**: Ensure proper IAM permissions for AMI creation and management |
| 164 | +- **Region**: Snapshots are region-specific and cannot be used across regions |
| 165 | +- **Lifecycle**: Use `ignore_changes = [ami]` on EC2 instances to prevent conflicts |
| 166 | + |
| 167 | +## Examples |
| 168 | + |
| 169 | +See the updated AWS templates that use this module: |
| 170 | + |
| 171 | +- [`coder/templates/aws-linux`](https://registry.coder.com/templates/aws-linux) |
| 172 | +- [`coder/templates/aws-windows`](https://registry.coder.com/templates/aws-windows) |
| 173 | +- [`coder/templates/aws-devcontainer`](https://registry.coder.com/templates/aws-devcontainer) |
0 commit comments