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