|
| 1 | +# AWS EC2 with Snapshot Support |
| 2 | + |
| 3 | +This template creates an AWS EC2 instance with automatic AMI snapshot capabilities using the `aws-ami-snapshot` module. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Automatic AMI Snapshots**: Creates snapshots when workspaces are stopped |
| 8 | +- **Snapshot Restoration**: Restore from previous snapshots when starting workspaces |
| 9 | +- **Lifecycle Management**: Automatic cleanup of old snapshots (keeps last 7) |
| 10 | +- **Persistent Storage**: Data persists across workspace restarts via AMI snapshots |
| 11 | +- **SSH & Code-Server**: Built-in SSH access and VS Code in the browser |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +### Prerequisites |
| 16 | + |
| 17 | +1. AWS credentials configured in your Coder deployment |
| 18 | +2. Proper IAM permissions for EC2, AMI, and DLM operations |
| 19 | + |
| 20 | +### Required IAM Permissions |
| 21 | + |
| 22 | +```json |
| 23 | +{ |
| 24 | + "Version": "2012-10-17", |
| 25 | + "Statement": [ |
| 26 | + { |
| 27 | + "Effect": "Allow", |
| 28 | + "Action": [ |
| 29 | + "ec2:RunInstances", |
| 30 | + "ec2:TerminateInstances", |
| 31 | + "ec2:DescribeInstances", |
| 32 | + "ec2:DescribeInstanceAttribute", |
| 33 | + "ec2:DescribeSecurityGroups", |
| 34 | + "ec2:CreateSecurityGroup", |
| 35 | + "ec2:DeleteSecurityGroup", |
| 36 | + "ec2:AuthorizeSecurityGroupIngress", |
| 37 | + "ec2:AuthorizeSecurityGroupEgress", |
| 38 | + "ec2:CreateKeyPair", |
| 39 | + "ec2:DeleteKeyPair", |
| 40 | + "ec2:DescribeKeyPairs", |
| 41 | + "ec2:CreateTags", |
| 42 | + "ec2:DescribeTags" |
| 43 | + ], |
| 44 | + "Resource": "*" |
| 45 | + }, |
| 46 | + { |
| 47 | + "Effect": "Allow", |
| 48 | + "Action": [ |
| 49 | + "ec2:CreateImage", |
| 50 | + "ec2:DescribeImages", |
| 51 | + "ec2:DeregisterImage", |
| 52 | + "ec2:CreateSnapshot", |
| 53 | + "ec2:DescribeSnapshots", |
| 54 | + "ec2:DeleteSnapshot" |
| 55 | + ], |
| 56 | + "Resource": "*" |
| 57 | + }, |
| 58 | + { |
| 59 | + "Effect": "Allow", |
| 60 | + "Action": [ |
| 61 | + "dlm:CreateLifecyclePolicy", |
| 62 | + "dlm:GetLifecyclePolicy", |
| 63 | + "dlm:UpdateLifecyclePolicy", |
| 64 | + "dlm:DeleteLifecyclePolicy" |
| 65 | + ], |
| 66 | + "Resource": "*" |
| 67 | + }, |
| 68 | + { |
| 69 | + "Effect": "Allow", |
| 70 | + "Action": [ |
| 71 | + "iam:CreateRole", |
| 72 | + "iam:DeleteRole", |
| 73 | + "iam:AttachRolePolicy", |
| 74 | + "iam:DetachRolePolicy", |
| 75 | + "iam:PassRole" |
| 76 | + ], |
| 77 | + "Resource": "*" |
| 78 | + } |
| 79 | + ] |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +### Deploying the Template |
| 84 | + |
| 85 | +1. Create the template in Coder: |
| 86 | + ```bash |
| 87 | + coder template create aws-snapshot-example \ |
| 88 | + --directory /path/to/this/template |
| 89 | + ``` |
| 90 | + |
| 91 | +2. Create a workspace: |
| 92 | + ```bash |
| 93 | + coder create my-workspace --template aws-snapshot-example |
| 94 | + ``` |
| 95 | + |
| 96 | +### How Snapshots Work |
| 97 | + |
| 98 | +1. **Automatic Creation**: When you stop your workspace, an AMI snapshot is automatically created |
| 99 | +2. **Snapshot Selection**: When starting a workspace, you can choose to restore from a previous snapshot |
| 100 | +3. **Lifecycle Management**: Old snapshots are automatically deleted after 7 snapshots are accumulated |
| 101 | + |
| 102 | +### Workspace Parameters |
| 103 | + |
| 104 | +- **Region**: Choose your AWS region |
| 105 | +- **Instance Type**: Select from t3.micro, t3.small, or t3.medium |
| 106 | +- **Enable Snapshots**: Toggle snapshot creation on/off |
| 107 | +- **Snapshot Label**: Add a custom label to your snapshots |
| 108 | +- **Use Previous Snapshot**: Select a snapshot to restore from |
| 109 | + |
| 110 | +### Accessing Your Workspace |
| 111 | + |
| 112 | +Once your workspace is running: |
| 113 | + |
| 114 | +1. **SSH**: Click the SSH button or use: |
| 115 | + ```bash |
| 116 | + ssh <username>@<public-ip> |
| 117 | + ``` |
| 118 | + |
| 119 | +2. **Code-Server**: Access VS Code in your browser through the Coder UI |
| 120 | + |
| 121 | +### Development Tools Included |
| 122 | + |
| 123 | +- Docker |
| 124 | +- Git |
| 125 | +- Build essentials |
| 126 | +- Common development utilities (vim, tmux, htop, jq) |
| 127 | + |
| 128 | +## Customization |
| 129 | + |
| 130 | +### Modify Instance Types |
| 131 | + |
| 132 | +Edit the `instance_type` parameter in `main.tf` to add more options: |
| 133 | + |
| 134 | +```hcl |
| 135 | +option { |
| 136 | + name = "t3.large (2 vCPU, 8 GB RAM)" |
| 137 | + value = "t3.large" |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +### Change Snapshot Retention |
| 142 | + |
| 143 | +Modify the `snapshot_retention_count` in the module configuration: |
| 144 | + |
| 145 | +```hcl |
| 146 | +module "ami_snapshot" { |
| 147 | + # ... |
| 148 | + snapshot_retention_count = 14 # Keep 14 snapshots instead of 7 |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +### Add More Software |
| 153 | + |
| 154 | +Edit the `user_data.sh` script or the `startup_script` in the Coder agent to install additional software. |
| 155 | + |
| 156 | +## Troubleshooting |
| 157 | + |
| 158 | +### Snapshots Not Creating |
| 159 | + |
| 160 | +1. Check IAM permissions |
| 161 | +2. Verify the workspace transition completed |
| 162 | +3. Check AWS CloudTrail logs for errors |
| 163 | + |
| 164 | +### Cannot Restore from Snapshot |
| 165 | + |
| 166 | +1. Ensure the snapshot AMI still exists |
| 167 | +2. Verify it's in the same region |
| 168 | +3. Check that the snapshot is owned by your AWS account |
| 169 | + |
| 170 | +### High Costs |
| 171 | + |
| 172 | +- Snapshots incur storage costs |
| 173 | +- Use lifecycle management to limit retained snapshots |
| 174 | +- Consider using smaller instance types for development |
| 175 | + |
| 176 | +## Support |
| 177 | + |
| 178 | +For issues or questions, please refer to the Coder documentation or contact your administrator. |
0 commit comments