Unknown model· unknown
from ShareGPT 90K (shared ChatGPT conversations)
Create a terraform data block to query for the latest image of Ubuntu 22.04 and…
- User
Create a terraform data block to query for the latest image of Ubuntu 22.04 and reference that within the
amiargument in the aws_instance.web block. - Assistant
Here's a sample Terraform data block to query the latest Amazon Machine Image (AMI) for Ubuntu 22.04:
sqldata "aws_ami" "ubuntu_2204" { most_recent = true filter { name = "name" values = ["ubuntu/images/hvm-ssd/ubuntu-focal-22.04-amd64-server-*"] } filter { name = "virtualization-type" values = ["hvm"] } } resource "aws_instance" "web" { ami = data.aws_ami.ubuntu_2204.id instance_type = "t2.micro" }This code uses the
datablock to query the latest AMI that matches the filter criteria:ubuntu/images/hvm-ssd/ubuntu-focal-22.04-amd64-server-*andvirtualization-type = hvm. Theaws_amidata source returns the latest image ID, which is stored indata.aws_ami.ubuntu_2204.id. This value is then referenced in theamiargument of theaws_instance.webresource to launch an EC2 instance based on the latest Ubuntu 22.04 AMI.