I want to deploy an ec2 instance on aws.
This is my resource code
data "aws_ami_ids" "ubuntu" {
owners = ["099720109477"]
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
}
output "test" {
value = data.aws_ami_ids.ubuntu
}
resource "aws_instance" "myec2" {
ami = "${data.aws_ami_ids.ubuntu.id}"
instance_type = var.instancetype
key_name = "devops-era"`
security_groups = ["${aws_security_group.allow_http_https.name}"]
tags = {
Name = "var.aws_common_tag"
}
}
But I keep getting the below error whenever I run terraform apply:
Error: creating EC2 Instance: InvalidAMIID.Malformed: Invalid id: "203304658" (expecting "ami-...")
│ status code: 400, request id: 35b803fd-a6c8-4632-82ee-2936c9cbf062
│
│ with aws_instance.myec2,
│ on mini-projet.tf line 28, in resource "aws_instance" "myec2":
│ 28: resource "aws_instance" "myec2" {
I don't understand why
Any suggestions here will be appreciated
CodePudding user response:
Your data source query returns multipe AMI ids. You have to choose explicitly which one you want, e.g. to use the first one:
ami = data.aws_ami_ids.ubuntu.ids[0]