Skip to content

Latest commit

 

History

History
139 lines (83 loc) · 4.45 KB

LAB02-Resources-Basic-EC2.md

File metadata and controls

139 lines (83 loc) · 4.45 KB

LAB-02: Resources => Provision Basic EC2 (Ubuntu 22.04)

This scenario shows:

  • how to create EC2 with Ubuntu 22.04

Code: https://github.com/omerbsezer/Fast-Terraform/blob/main/labs/basic-resource-ec2-ubuntu/main.tf

Prerequisite

Steps

  • Create main.tf and copy the code
# main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }

  required_version = ">= 1.2.0"
}

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

resource "aws_instance" "instance" {
	ami           = "ami-0d1ddd83282187d18" # Ubuntu 22.04 eu-central-1 Frankfurt
	instance_type = "t2.nano"

	tags = {
		Name = "Basic Instance"
	}
}

Code: https://github.com/omerbsezer/Fast-Terraform/blob/main/labs/basic-resource-ec2-ubuntu/main.tf

image

  • Run init command:
terraform init

image

  • Init command downloads required executable files from Terraform (.terraform):

image

  • Validate file:
terraform validate

image

  • Run plan command:
terraform plan

image

image

  • Run apply command to create resources. Then, Terraform asks to confirm, write "yes":
terraform apply

image

  • It creates the resources that are defined in the main.tf:

image

  • After apply command, terraform creates state files:

image

  • On AWS, go to EC2 services:

image

  • On EC2 Dashboard:

image

  • Security Group Configuration is configured as default, inbound ports are defined as "all" (this is not good for security, but for now, it's ok!), egress ports are defined as "all"

image

  • Network Configuration is configured as default, but availability zone is defined as "eu-central-1"

image

  • Storage Configuration is configured as default, 8GB EBS (Elastic Block Storage) is attached:

image

  • Delete all resources. Then, Terraform asks to confirm, write "yes"::
terraform destroy

image

image

  • On AWS EC2 Instances, EC2 was terminated, it'll be disappeared in max. 1 hour. Once an EC2 is terminated, that EC2 is deleted permanently:

image

  • Destroy command is important to terminate the resources.
  • If you forget the destroy command, your EC2 runs until termination and you MUST pay the usage price of EC2.

image

  • It is really IMPORTANT to check whether unnecessary paid services are closed or not.

image