Skip to content

AWS AMI Snapshot Module for Persistent Workspace State #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
895a106
proper folder strcture
MAVRICK-1 Jul 12, 2025
993ebf6
structured
MAVRICK-1 Jul 12, 2025
66f9413
file strcture
MAVRICK-1 Jul 12, 2025
e71b9dd
lint
MAVRICK-1 Jul 12, 2025
dd94a19
lint error
MAVRICK-1 Jul 12, 2025
7536a2a
lint
MAVRICK-1 Jul 12, 2025
61893e9
Merge branch 'main' into aws-snapshot
MAVRICK-1 Jul 16, 2025
25b1455
fix: correct GitHub username casing in README files
MAVRICK-1 Jul 18, 2025
b503e85
feat: update AWS provider configuration and fix tags serialization in…
MAVRICK-1 Jul 18, 2025
641995d
fix: update maintainer GitHub username in README
MAVRICK-1 Jul 18, 2025
49e7b51
Merge branch 'main' into aws-snapshot
DevelopmentCats Jul 25, 2025
c2a0c52
Merge branch 'main' into aws-snapshot
MAVRICK-1 Jul 29, 2025
eb7c5fd
feat: add test_mode variable to skip AWS API calls during testing
MAVRICK-1 Jul 29, 2025
970a3cd
Merge branch 'main' into aws-snapshot
DevelopmentCats Aug 1, 2025
8b2e3b0
fix: remove outdated variables and outputs sections from README.md
MAVRICK-1 Aug 1, 2025
a0ca69c
fix: remove redundant newline in README.md
MAVRICK-1 Aug 4, 2025
1b24629
Merge branch 'main' into aws-snapshot
MAVRICK-1 Aug 4, 2025
c220d32
Merge branch 'main' into aws-snapshot
MAVRICK-1 Aug 4, 2025
d8f0715
fix: update README.md with personal details and enhance bio
MAVRICK-1 Aug 7, 2025
fa8c4e3
chore: remove maintainer_github field
DevelopmentCats Aug 8, 2025
3d1115d
Merge branch 'main' into aws-snapshot
DevelopmentCats Aug 8, 2025
a834079
Merge branch 'main' into aws-snapshot
DevelopmentCats Aug 12, 2025
e3f9992
Merge branch 'main' into aws-snapshot
MAVRICK-1 Aug 12, 2025
3aca26b
Update registry/mavrickrishi/modules/aws-ami-snapshot/main.tf
MAVRICK-1 Aug 12, 2025
e151320
Update registry/mavrickrishi/modules/aws-ami-snapshot/main.tf
MAVRICK-1 Aug 12, 2025
f9a2fa4
Merge branch 'main' into aws-snapshot
MAVRICK-1 Aug 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
muc = "muc" # For Munich location code
Hashi = "Hashi"
HashiCorp = "HashiCorp"
mavrickrishi = "mavrickrishi" # Username
mavrick = "mavrick" # Username

[files]
extend-exclude = ["registry/coder/templates/aws-devcontainer/architecture.svg"] #False positive
Binary file added registry/mavrickrishi/.images/avatar.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions registry/mavrickrishi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
display_name: "Rishi Mondal"
bio: "Breaking code, fixing bugs, and occasionally making it work! Always caffeinated, always committing"
avatar_url: "./.images/avatar.jpeg"
github: "MAVRICK-1"
linkedin: "https://www.linkedin.com/in/rishi-mondal-5238b2282/" # Optional
website: "https://mavrick-portfolio.vercel.app/" # Optional
support_email: "[email protected]" # Optional
status: "community"
---

# Rishi Mondal

I'm Rishi Mondal, a passionate developer from Chinsurah Hooghly, West Bengal, India.
I'm a maintainer at CNCF KubeStellar, GSoC contributor at UCSC OSPO, and a Docker Captain.
When I'm not breaking code and fixing bugs, you'll find me contributing to open-source projects,
participating in LFX CNCF programs, and helping the developer community grow.

## Modules

- **aws-ami-snapshot**: Create and manage AMI snapshots for Coder workspaces with restore capabilities
173 changes: 173 additions & 0 deletions registry/mavrickrishi/modules/aws-ami-snapshot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
---
display_name: AWS AMI Snapshot
description: Create and manage AMI snapshots for Coder workspaces with restore capabilities
icon: ../../../../.icons/aws.svg
verified: false
tags: [aws, snapshot, ami, backup, persistence]
---

# AWS AMI Snapshot Module

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.

```tf
module "ami_snapshot" {
source = "registry.coder.com/mavrickrishi/aws-ami-snapshot/coder"
version = "1.0.0"

instance_id = aws_instance.workspace.id
default_ami_id = data.aws_ami.ubuntu.id
template_name = "aws-linux"
}
```

## Features

- **Automatic Snapshots**: Create AMI snapshots when workspaces are stopped
- **User Control**: Enable/disable snapshot functionality per workspace
- **Custom Labels**: Add custom labels to snapshots for easy identification
- **Snapshot Selection**: Choose from available snapshots when starting workspaces
- **Automatic Cleanup**: Optional Data Lifecycle Manager integration for automated cleanup
- **Workspace Isolation**: Snapshots are tagged and filtered by workspace and owner

## Parameters

The module exposes the following parameters to workspace users:

- `enable_snapshots`: Enable/disable AMI snapshot creation (default: true)
- `snapshot_label`: Custom label for the snapshot (optional)
- `use_previous_snapshot`: Select a previous snapshot to restore from (default: none)

## Usage

### Basic Usage

```hcl
module "ami_snapshot" {
source = "registry.coder.com/modules/aws-ami-snapshot"

instance_id = aws_instance.workspace.id
default_ami_id = data.aws_ami.ubuntu.id
template_name = "aws-linux"
}

resource "aws_instance" "workspace" {
ami = module.ami_snapshot.ami_id
instance_type = "t3.micro"

# Prevent Terraform from recreating instance when AMI changes
lifecycle {
ignore_changes = [ami]
}
}
```

### With Optional Cleanup

```hcl
module "ami_snapshot" {
source = "registry.coder.com/modules/aws-ami-snapshot"

instance_id = aws_instance.workspace.id
default_ami_id = data.aws_ami.ubuntu.id
template_name = "aws-linux"
enable_dlm_cleanup = true
dlm_role_arn = aws_iam_role.dlm_lifecycle_role.arn
snapshot_retention_count = 5

tags = {
Environment = "development"
Project = "my-project"
}
}
```

### IAM Role for DLM (Optional)

If using automatic cleanup, create an IAM role for Data Lifecycle Manager:

```hcl
resource "aws_iam_role" "dlm_lifecycle_role" {
name = "dlm-lifecycle-role"

assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "dlm.amazonaws.com"
}
}
]
})
}

resource "aws_iam_role_policy_attachment" "dlm_lifecycle" {
role = aws_iam_role.dlm_lifecycle_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSDataLifecycleManagerServiceRole"
}
```

## Required IAM Permissions

Users need the following IAM permissions for full functionality:

```json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:CreateImage",
"ec2:DescribeImages",
"ec2:DescribeInstances",
"ec2:CreateTags",
"ec2:DescribeTags"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"dlm:CreateLifecyclePolicy",
"dlm:GetLifecyclePolicy",
"dlm:UpdateLifecyclePolicy",
"dlm:DeleteLifecyclePolicy"
],
"Resource": "*",
"Condition": {
"StringEquals": {
"dlm:Target": "INSTANCE"
}
}
}
]
}
```

## How It Works

1. **Snapshot Creation**: When a workspace transitions to "stop", an AMI snapshot is automatically created (if enabled)
2. **Tagging**: Snapshots are tagged with workspace name, owner, template, and custom labels
3. **Snapshot Retrieval**: Available snapshots are retrieved and presented as options for workspace start
4. **AMI Selection**: The module outputs the appropriate AMI ID (default or selected snapshot)
5. **Cleanup**: Optional DLM policies can automatically clean up old snapshots

## Considerations

- **Cost**: AMI snapshots incur storage costs. Use cleanup policies to manage costs
- **Time**: AMI creation takes time; workspace stop operations may take longer
- **Permissions**: Ensure proper IAM permissions for AMI creation and management
- **Region**: Snapshots are region-specific and cannot be used across regions
- **Lifecycle**: Use `ignore_changes = [ami]` on EC2 instances to prevent conflicts

## Examples

See the updated AWS templates that use this module:

- [`coder/templates/aws-linux`](https://registry.coder.com/templates/aws-linux)
- [`coder/templates/aws-windows`](https://registry.coder.com/templates/aws-windows)
- [`coder/templates/aws-devcontainer`](https://registry.coder.com/templates/aws-devcontainer)
59 changes: 59 additions & 0 deletions registry/mavrickrishi/modules/aws-ami-snapshot/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { describe, expect, it } from "bun:test";
import {
runTerraformApply,
runTerraformInit,
testRequiredVariables,
} from "~test";

describe("aws-ami-snapshot", async () => {
await runTerraformInit(import.meta.dir);

it("required variables with test mode", async () => {
await runTerraformApply(import.meta.dir, {
instance_id: "i-1234567890abcdef0",
default_ami_id: "ami-12345678",
template_name: "test-template",
test_mode: true,
});
});

it("missing variable: instance_id", async () => {
await expect(runTerraformApply(import.meta.dir, {
default_ami_id: "ami-12345678",
template_name: "test-template",
test_mode: true,
})).rejects.toThrow();
});

it("missing variable: default_ami_id", async () => {
await expect(runTerraformApply(import.meta.dir, {
instance_id: "i-1234567890abcdef0",
template_name: "test-template",
test_mode: true,
})).rejects.toThrow();
});

it("missing variable: template_name", async () => {
await expect(runTerraformApply(import.meta.dir, {
instance_id: "i-1234567890abcdef0",
default_ami_id: "ami-12345678",
test_mode: true,
})).rejects.toThrow();
});

it("supports optional variables", async () => {
await runTerraformApply(import.meta.dir, {
instance_id: "i-1234567890abcdef0",
default_ami_id: "ami-12345678",
template_name: "test-template",
test_mode: true,
enable_dlm_cleanup: true,
dlm_role_arn: "arn:aws:iam::123456789012:role/dlm-lifecycle-role",
snapshot_retention_count: 5,
tags: JSON.stringify({
Environment: "test",
Project: "coder",
}),
});
});
});
Loading