-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
aws_key_pair forces new resource after importing #1092
Comments
I don't believe AWS exposes the public_key via their API calls. I suppose an instance could be launched and then the key retrieved but I suspect that is out of scope at the moment. Plus it would be strictly better if AWS exposed this information. |
Hey folks, As @recurrence exposed it, the @iserko As AWS does not provide this information when using the API, there is nothing Terraform can help on that... 😞 Do you mind me closing this issue? Thanks! |
@Ninir, I understand the reason for why it doesn't currently work as advertised. Four options are possible:
As it stands right now if you want to import it at the moment, manually changing terraform.tfstate is your only way to go, without destroying and recreating the resource. |
+1 to 1! I suppose this breaks with the current form of the import statement but taking a secondary argument is a natural progression. That would save the manual state edit while waiting for AWS to return it. |
@Ninir I think we may be able to get around this by using a custom import method that simply sets |
@iserko |
Hi folks, Circling back to that, editing the state is the only viable solution for now. I created a PR to expose that. I hope documenting this edge case is enough. Let me know! |
If there is a safe character delimiter we can use, we can optionally allow the import ID to contain both the key name and public key, e.g. with a comma ( $ terraform import aws_key_pair.example 'example,ssh-rsa AAAA...ZZZ== [email protected]' Then in the import code it can check for the delimiter to perform either a combined import or fallback to the old behavior of just a key name import. 😄 |
Wouldn't the fingerprint be the more correct way to match a remote key with local state?
|
I'm fairly new to all of this, but I thought I'd leave a helper that I needed when I was building context. I agree some tooling would be great, but I second the comment that updating the docs would be helpful. What Works for Me ™️If you encounter this problem and do not want to create a new resource, you'll need to manually (text-editor) change your tfstate file. Open the JSON in your Once this is done your terraform should apply cleanly. |
@raymondberg I wrote a blog post on just that 😀 |
You can do the replacement programmatically with jq. jq \
--arg name "bastion_keypair" \
--arg public_key "ssh-rsa AAAAB3NzaC1yc2E..." \
'
(.resources | map(.name == $name and .type == "aws_key_pair") | index(true)) as $index |
.resources[$index].instances[0].attributes.public_key = $public_key
' \
terraform.tfstate If the output looks good, save a backup of your previous state, save the output to a file, replace the state with the new output, and the apply command should work as expected. For help understanding the filter, see these links: |
Lots of workarounds described, so that should be enough for now. Closing the issue as it is quite old already. |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. |
Terraform Version
Terraform v0.9.11
Affected Resource(s)
Terraform Configuration Files
Expected Behavior
After using
terraform import aws_key_pair.igor igor_20150712
I would have expected aterraform plan
to not result in any changes.Actual Behavior
Got
Steps to Reproduce
terraform import aws_key_pair.igor
terraform plan -target aws_key_pair.igor
Workaround
Manually adding a
public_key
section intoterraform.tfstate
. Changing:to
resulted in no changes detected in the next
terraform plan
.The text was updated successfully, but these errors were encountered: