Fix metadata token usage for EC2 metadata requests and add AZ error handling #84
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.
Overview
This pull request addresses a bug where the EC2 instance's availability zone (AZ) was not properly retrieved, causing the DNS A record update in Route53 to fail. The root cause was that the IMDSv2 token was used only for retrieving the public IP address, but not for obtaining other critical metadata such as the instance-id and AZ. Without the token, the AZ was empty, leading to an error during region extraction using
${AZ::-1}.Changes Made
IMDSv2 Token Consistency:
Updated the metadata retrieval commands for
instance-idandplacement/availability-zoneto include theX-aws-ec2-metadata-tokenheader.AZ Validation:
Added an error check to exit the script early if the availability zone is empty, preventing further processing and potential errors.
Region Extraction:
With the AZ correctly obtained, the region extraction
${AZ::-1}now works as expected, ensuring that subsequent AWS CLI commands (e.g., fetching the tag for DOMAIN_NAME and updating Route53 records) operate in the correct region.How to Test
DOMAIN_NAMEtag set (e.g.,nisei.com).sudo systemctl start minecloud.service.Additional Notes
This is my first contribution to an open source project. If I have made any mistakes in the contribution process, I would greatly appreciate any feedback on how to improve.
My native language is Japanese, and I used generative AI to help produce the English text in this pull request. I apologize in advance if any part of the English comes off as impolite or contains errors.
Thanks for considering this fix!