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
- You should have a look following lab:
- 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
- Run init command:
terraform init
- Init command downloads required executable files from Terraform (.terraform):
- Validate file:
terraform validate
- Run plan command:
terraform plan
- Run apply command to create resources. Then, Terraform asks to confirm, write "yes":
terraform apply
- It creates the resources that are defined in the main.tf:
- After apply command, terraform creates state files:
- On AWS, go to EC2 services:
- On EC2 Dashboard:
- 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"
- Network Configuration is configured as default, but availability zone is defined as "eu-central-1"
- Storage Configuration is configured as default, 8GB EBS (Elastic Block Storage) is attached:
- Delete all resources. Then, Terraform asks to confirm, write "yes"::
terraform destroy
- 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:
- 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.
- It is really IMPORTANT to check whether unnecessary paid services are closed or not.