Create Ubuntu Server AWS EC2 Instance With Terraform

In this AWS and Terraform blog post, we will create an AWS EC2 Instance (VM) that runs Ubuntu Server 20.04 using Terraform.

If you look at the official EC2 Terraform module, you might get really overwhelmed by all the available features and options; however, most of them are optional.

In the post, we will use a normal configuration that will get you up and running and, at the same time, give you a fully functional instance. To get started with AWS visit our category page.

Configuration

We are creating an instance that runs Ubuntu Server 20.04 with a public IP address in the following configuration. We also have a code block for the SSH key (Note: Username to log in is ubuntu) to login into the instance.

Another code will give the instance a name that needs to be unique across AWS. If, after the deployment, your instance doesn’t show a name, it is because the chosen name is not unique.

The AMI name is the latest Ubuntu Server 20.04 image. We are aslo going to deploy a t2.micro instance. This code will work with any Linux image.

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "3.47.0"
    }
  }
}

provider "aws" {
  region = "ap-southeast-2"
}
variable "instance_name" {
    default = "ntweekly0001"
}


resource "aws_instance" "vm" {
  
  ami           = "ami-0567f647e75c7bc05"
  instance_type = "t2.micro"
  key_name = aws_key_pair.login.id

  tags = {
    Name = var.instance_name

} 

}

resource "aws_key_pair" "login" {
  key_name   = "login"
  public_key = "ssh-rsa......paste you public key"
}

For more Terraform articles, visit our sister blog Terraform category page https://www.ntweekly.com/category/terraform


Posted

in

,

by

Comments

One response to “Create Ubuntu Server AWS EC2 Instance With Terraform”

  1. […] Create Ubuntu Server AWS EC2 Instance With Terraform (www.sysopsruntime.com) […]