Skip to content

resource/aws_key_pair: fix public key import drift#46870

Open
corymhall wants to merge 3 commits intohashicorp:mainfrom
corymhall:b-ec2-key-pair-import-public-key
Open

resource/aws_key_pair: fix public key import drift#46870
corymhall wants to merge 3 commits intohashicorp:mainfrom
corymhall:b-ec2-key-pair-import-public-key

Conversation

@corymhall
Copy link
Contributor

@corymhall corymhall commented Mar 11, 2026

Rollback Plan

If a change needs to be reverted, we will publish an updated version of the library.

Changes to Security Controls

No changes to security controls.

Description

Fix aws_key_pair import so the provider reads and persists public_key after import.

This change:

  • sets IncludePublicKey=true when reading a key pair by name
  • stores the returned public_key in resource state during read
  • normalizes OpenSSH public keys so state does not drift when AWS rewrites the key comment or trailing newline
  • removes the stale import workaround from acceptance tests and adds explicit import coverage
  • removes the stale documentation note that said the public key could not be returned by the API

Relations

Closes #44647

References

Output from Acceptance Testing

% make testacc TESTS='TestAccEC2KeyPair|TestAccEC2KeyPairDataSource' PKG=ec2

PASS
ok   github.com/hashicorp/terraform-provider-aws/internal/service/ec2  41.322s

@corymhall corymhall requested a review from a team as a code owner March 11, 2026 16:02
@github-actions
Copy link
Contributor

Community Guidelines

This comment is added to every new Pull Request to provide quick reference to how the Terraform AWS Provider is maintained. Please review the information below, and thank you for contributing to the community that keeps the provider thriving! 🚀

Voting for Prioritization

  • Please vote on this Pull Request by adding a 👍 reaction to the original post to help the community and maintainers prioritize it.
  • Please see our prioritization guide for additional information on how the maintainers handle prioritization.
  • Please do not leave +1 or other comments that do not add relevant new information or questions; they generate extra noise for others following the Pull Request and do not help prioritize the request.

Pull Request Authors

  • Review the contribution guide relating to the type of change you are making to ensure all of the necessary steps have been taken.
  • Whether or not the branch has been rebased will not impact prioritization, but doing so is always a welcome surprise.

@github-actions github-actions bot added needs-triage Waiting for first response or review from a maintainer. documentation Introduces or discusses updates to documentation. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure. service/ec2 Issues and PRs that pertain to the ec2 service. size/M Managed by automation to categorize the size of a PR. labels Mar 11, 2026
@dosubot dosubot bot added the bug Addresses a defect in current functionality. label Mar 11, 2026
@justinretzolk justinretzolk added resource-import Pertains to issues importing resources. and removed needs-triage Waiting for first response or review from a maintainer. labels Mar 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Addresses a defect in current functionality. documentation Introduces or discusses updates to documentation. resource-import Pertains to issues importing resources. service/ec2 Issues and PRs that pertain to the ec2 service. size/M Managed by automation to categorize the size of a PR. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow importing of public key in aws_key_pair

2 participants