top of page
Search

Terraform — Create an EC2 Instance


We can create an instance resource faster. In this example, we create variables for instance ami, type and name. And we call them in the instance file. We also add an elastic-ip to this instance. To do that, we create a “aws_eip” resource. If we don’t enter a content in the body, terraform creates an elastic IP but it doesn’t bind to an instance. So, we write instance parameter and call the instance with it’s instance id. So, our new instance has an elastic IP when we apply this configuration.




variable "ins_ami" {
default = "ami-000959cf06650d13f"
}
variable "ins_type" {
default = "t2.nano"
}
variable "ins_name" {
default = "ec2-instance-1"
}







resource "aws_instance" "new_instance" {
  ami = var.ins_ami
  instance_type = var.ins_type
  tags = {
      Name = var.ins_name
  }
  key_name = "terra-key"
}

resource "aws_eip" "elastic-ip" {
    instance = aws_instance.new_instance.id
}


 
 
 

Comments


Register our Newsletters

Thanks for submitting!

© 2023 by NetworksAutomation.com

Have Any Questions?

Thanks for submitting!

bottom of page