Site icon installvirtual

How to deploy EC2 instance with terraform

Terraform apply will the manifest and spin up the EC2 instance and output the instance_id and instance_public_ip. 

Destroy the EC2 Instance

You can destroy the Ec2 using the following command to avoid charges on AWS.

$ terraform destroy

Type yes in the prompt to confirm the destroy. 

So, now you know how to deploy EC2 instances with terraform. This is a super simple example there is a lot more than you can do with terraform. Stay tuned for more tutorials and articles. 

Terraform

In this article, I will show you how to deploy EC2 instances with terraform. This is 3rd article in this terraform series. You can find links to other articles down below.

Terraform Series:

How to install Terraform on macOS Big Sur with Homebrew
How to uninstall terraform macOS with brew

Deploy EC2 instance with terraform

Prerequisites

  1. AWS Credentials should be configured
  2. Terraform must be installed or go to this link

The first step is to create terraform manifest to deploy EC2 instance using terraform.
Create a file called main.tf and paste the following content. 

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.27"
    }
  }
}

provider "aws" {
  profile = "default"
  region  = "eu-central-1"
}

resource "aws_instance" "installvirtual" {
  ami           = "ami-0233214e13e500f77"
  instance_type = "t2.micro"
  tags = {
    Name = "Installvirtual"
  }
}

The above manifest will create an EC2 instance in the eu-central-1 region. It will be a t2.micro instance.

Note: Creating Infrastructure will cost you on AWS.

First of all, you need to initialize the terraform plugins.

$ terraform init

Initializing the backend...

Initializing provider plugins...
- Finding hashicorp/aws versions matching "~> 3.27"...
- Installing hashicorp/aws v3.32.0...
- Installed hashicorp/aws v3.32.0 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

 

 

The next step is to plan. With the terraform plan command you can what is going to change and what is going to be created. Let’s try:

terraform plan

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:

# aws_instance.installvirtual will be created
+ resource "aws_instance" "installvirtual" {
+ ami = "ami-0233214e13e500f77"
+ arn = (known after apply)
+ associate_public_ip_address = (known after apply)
+ availability_zone = (known after apply)
+ cpu_core_count = (known after apply)
+ cpu_threads_per_core = (known after apply)
+ get_password_data = false
+ host_id = (known after apply)
+ id = (known after apply)
+ instance_state = (known after apply)
+ instance_type = "t2.micro"
+ ipv6_address_count = (known after apply)
+ ipv6_addresses = (known after apply)
+ key_name = (known after apply)
+ outpost_arn = (known after apply)
+ password_data = (known after apply)
+ placement_group = (known after apply)
+ primary_network_interface_id = (known after apply)
+ private_dns = (known after apply)
+ private_ip = (known after apply)
+ public_dns = (known after apply)
+ public_ip = (known after apply)
+ secondary_private_ips = (known after apply)
+ security_groups = (known after apply)
+ source_dest_check = true
+ subnet_id = (known after apply)
+ tags = {
+ "Name" = "installvirtual"
}
+ tenancy = (known after apply)
+ vpc_security_group_ids = (known after apply)

+ ebs_block_device {
+ delete_on_termination = (known after apply)
+ device_name = (known after apply)
+ encrypted = (known after apply)
+ iops = (known after apply)
+ kms_key_id = (known after apply)
+ snapshot_id = (known after apply)
+ tags = (known after apply)
+ throughput = (known after apply)
+ volume_id = (known after apply)
+ volume_size = (known after apply)
+ volume_type = (known after apply)
}

+ enclave_options {
+ enabled = (known after apply)
}

+ ephemeral_block_device {
+ device_name = (known after apply)
+ no_device = (known after apply)
+ virtual_name = (known after apply)
}

+ metadata_options {
+ http_endpoint = (known after apply)
+ http_put_response_hop_limit = (known after apply)
+ http_tokens = (known after apply)
}

+ network_interface {
+ delete_on_termination = (known after apply)
+ device_index = (known after apply)
+ network_interface_id = (known after apply)
}

+ root_block_device {
+ delete_on_termination = (known after apply)
+ device_name = (known after apply)
+ encrypted = (known after apply)
+ iops = (known after apply)
+ kms_key_id = (known after apply)
+ tags = (known after apply)
+ throughput = (known after apply)
+ volume_id = (known after apply)
+ volume_size = (known after apply)
+ volume_type = (known after apply)
}
}

Plan: 1 to add, 0 to change, 0 to destroy.

Changes to Outputs:
+ instance_id = (known after apply)
+ instance_public_ip = (known after apply)

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

Releasing state lock. This may take a few moments...

$ terraform apply

Terraform apply will the manifest and spin up the EC2 instance and output the instance_id and instance_public_ip. 

Destroy the EC2 Instance

You can destroy the Ec2 using the following command to avoid charges on AWS.

$ terraform destroy

Type yes in the prompt to confirm the destroy. 

So, now you know how to deploy EC2 instances with terraform. This is a super simple example there is a lot more than you can do with terraform. Stay tuned for more tutorials and articles. 

Terraform

Exit mobile version