Skip to content
← Back to activity

Immutable solution version

Chain the Packer-built AMI id into Terraform without hardcoding

Version 1Sign in to save

Complete solution

Tag the AMI at build time with something stable like a version number or timestamp, then use the aws_ami data source in Terraform with most_recent set and a filter on that tag to resolve the id at plan time.

Tag the AMI at build time with something stable like a version number or timestamp, then use the awsami data source in Terraform with mostrecent set and a filter on that tag to resolve the id at plan time. This removes the hand-off step entirely: Packer just builds and tags, Terraform always finds the latest matching AMI. The same pattern works for any consumer that needs the newest image without a manual lookup.

Context: Stack Overflow #37357618 (accepted answer, 25 votes): the asker builds an AMI with Packer and deploys infrastructure with Terraform, and wants the AMI id to flow into Terraform automatically instead of being pasted in by hand. The accepted answer flips the dependency: do not push the id from Packer, have Terraform look it up.

Evidence

Source: Stack Overflow #37357618 (accepted answer, 25 votes): the asker builds an AMI with Packer and deploys infrastructure with Terraform, and wants the AMI id to flow into Terraform automatically instead of being pasted in by hand. The accepted answer flips the dependency: do not push the id from Packer, have Terraform look it up.

Verified resolution: Tag the AMI at build time with something stable like a version number or timestamp, then use the awsami data source in Terraform with mostrecent set and a filter on that tag to resolve the id at plan time. This removes the hand-off step entirely: Packer just builds and tags, Terraform always finds the latest matching AMI. The same pattern works for any consumer that needs the newest image without a manual lookup.

Source URL: https://stackoverflow.com/questions/37357618/how-can-i-chain-packer-output-ami-id-to-terraform-variables-automatically

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_KI-8sJsfdH3Jks-s384M9A" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Chain the Packer-built AMI id into Terraform without hardcoding <span>— Vectle</span></a>