Manage the EC2 key pair as a Terraform resource#1
Open
mmshad wants to merge 1 commit into
Open
Conversation
The key pair is created with boto3 in provision.py and Terraform only references it by name, so it is never in Terraform state. A plain terraform destroy does not remove it, and the destroy flow deletes it only as a separate best effort boto3 call that can be skipped or fail, leaving orphaned key pairs in AWS. provision.py now generates the key locally with ssh-keygen and passes the public key to a new aws_key_pair resource, so Terraform owns its lifecycle. The boto3 delete stays as an idempotent fallback for workspaces created before this change. The README IAM permission changes from CreateKeyPair to ImportKeyPair.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #2
Problem
The key pair is created with boto3 in provision.py and Terraform only
references it by name, so it is never in Terraform state. A plain
terraform destroy does not remove it, and the destroy flow deletes it
only as a separate best effort boto3 call that can be skipped or fail.
The result is orphaned key pairs in AWS.
Fix
provision.py now generates the key locally with ssh-keygen, writes the
private .pem to the workspace at mode 0400, and passes the public key to
a new aws_key_pair resource. Terraform now owns the key pair lifecycle,
so terraform destroy removes it with everything else. The boto3 delete
stays as an idempotent fallback so key pairs from older workspaces are
still cleaned up. The README IAM permission changes from CreateKeyPair
to ImportKeyPair, which is what aws_key_pair uses.
Testing
Verified end to end on a real instance: provisioned a g4dn.xlarge,
confirmed aws_key_pair is in Terraform state, logged in over SSH with
the generated key, then terraform destroy removed the instance and the
key pair. terraform validate and plan also pass.