AnkitK
AnkitK

Reputation: 37

Can we launch instance from Custom AMI using terraform

I was launching the AWS EC2 from custom AMI named as 'test-ami' using terraform .But i was getting below error for the same .As in the main.tf file first custom ami would be created from known server passing source_instance_id , and then ec2 would be created from this custom AMI .Error is

Error: Error launching source instance: InvalidAMIID.Malformed: Invalid id: "test-ami.id" (expecting "ami-...") status code: 400, request id: 1afd98e1-9d5a-4b1e-b81b-beb24c3da789

on main.tf line 31, in resource "aws_instance" "test-server3": 31: resource "aws_instance" "test-server3" {

Can we created ec2 via custom AMI name as AMI ID is not known to me ?

Upvotes: 0

Views: 1055

Answers (1)

Marcin
Marcin

Reputation: 238209

Your test-server3 should be:

resource "aws_instance" "test-server3" { 

    ami = aws_ami_from_instance.test-ami.id
     
    instance_type = "t2.micro" 
    subnet_id = "subnet-0ab209a91658784cc" 
    key_name = "terraform" 
    tags = { 
        Name = "test-server3" 
    } 
} 

Upvotes: 0

Related Questions