-
Notifications
You must be signed in to change notification settings - Fork 43
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
MAVRICK-1
wants to merge
21
commits into
coder:main
Choose a base branch
from
MAVRICK-1:aws-snapshot
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
895a106
proper folder strcture
MAVRICK-1 993ebf6
structured
MAVRICK-1 66f9413
file strcture
MAVRICK-1 e71b9dd
lint
MAVRICK-1 dd94a19
lint error
MAVRICK-1 7536a2a
lint
MAVRICK-1 61893e9
Merge branch 'main' into aws-snapshot
MAVRICK-1 25b1455
fix: correct GitHub username casing in README files
MAVRICK-1 b503e85
feat: update AWS provider configuration and fix tags serialization in…
MAVRICK-1 641995d
fix: update maintainer GitHub username in README
MAVRICK-1 49e7b51
Merge branch 'main' into aws-snapshot
DevelopmentCats c2a0c52
Merge branch 'main' into aws-snapshot
MAVRICK-1 eb7c5fd
feat: add test_mode variable to skip AWS API calls during testing
MAVRICK-1 970a3cd
Merge branch 'main' into aws-snapshot
DevelopmentCats 8b2e3b0
fix: remove outdated variables and outputs sections from README.md
MAVRICK-1 a0ca69c
fix: remove redundant newline in README.md
MAVRICK-1 1b24629
Merge branch 'main' into aws-snapshot
MAVRICK-1 c220d32
Merge branch 'main' into aws-snapshot
MAVRICK-1 d8f0715
fix: update README.md with personal details and enhance bio
MAVRICK-1 fa8c4e3
chore: remove maintainer_github field
DevelopmentCats 3d1115d
Merge branch 'main' into aws-snapshot
DevelopmentCats File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
display_name: "mavrickrishi" | ||
description: "Modules and templates by mavrickrishi" | ||
github_url: "https://github.com/mavrick-1" | ||
status: "community" | ||
--- | ||
|
||
# mavrickrishi | ||
|
||
This namespace contains modules and templates created by mavrickrishi. | ||
|
||
## Modules | ||
|
||
- **aws-ami-snapshot**: Create and manage AMI snapshots for Coder workspaces with restore capabilities |
197 changes: 197 additions & 0 deletions
197
registry/mavrickrishi/modules/aws-ami-snapshot/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
--- | ||
display_name: AWS AMI Snapshot | ||
description: Create and manage AMI snapshots for Coder workspaces with restore capabilities | ||
icon: ../../../../.icons/aws.svg | ||
maintainer_github: coder | ||
DevelopmentCats marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
|
||
## Variables | ||
MAVRICK-1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
| Name | Description | Type | Default | Required | | ||
| ------------------------ | ------------------------------------------------------------ | ----------- | ------- | -------- | | ||
| instance_id | The EC2 instance ID to create snapshots from | string | n/a | yes | | ||
| default_ami_id | The default AMI ID to use when not restoring from a snapshot | string | n/a | yes | | ||
| template_name | The name of the Coder template using this module | string | n/a | yes | | ||
| test_mode | Set to true when running tests to skip AWS API calls | bool | false | no | | ||
| tags | Additional tags to apply to snapshots | map(string) | {} | no | | ||
| enable_dlm_cleanup | Enable Data Lifecycle Manager for automated snapshot cleanup | bool | false | no | | ||
| dlm_role_arn | ARN of the IAM role for DLM | string | "" | no | | ||
| snapshot_retention_count | Number of snapshots to retain when using DLM cleanup | number | 7 | no | | ||
DevelopmentCats marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Outputs | ||
|
||
| Name | Description | | ||
| ------------------- | ----------------------------------------------------- | | ||
| ami_id | The AMI ID to use for the workspace instance | | ||
| is_using_snapshot | Whether the workspace is using a snapshot AMI | | ||
| snapshot_ami_id | The AMI ID of the created snapshot (if any) | | ||
| available_snapshots | List of available snapshot AMI IDs for this workspace | | ||
| snapshot_info | Detailed information about available 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` | ||
MAVRICK-1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- `coder/templates/aws-windows` | ||
- `coder/templates/aws-devcontainer` |
59 changes: 59 additions & 0 deletions
59
registry/mavrickrishi/modules/aws-ami-snapshot/main.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}), | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.