Post date: 19/06/24

Current Update: Terraform EC2 Deployment

Next: Complete VPC Infrastructure
EC2 Instance in Existing Subnet

Pre-req

  • Ensure installation of Terraform and AWS Cli
  • "terrafoam version" "aws --version"
  • Used aws single sign on(sso) for credentials instead of access-keys
  • aws recommends sso over access-key

Setting Up

  • creates project directory as terraform project folder
  • creates main.tf file as the entry point
  • creates variables.tf files for declaring variables
  • creates terraform.tfvars file for assigning variables

main.tf

 provider "aws" {
                            region = "eu-west-2"
                        }
                        resource "aws_instance" "paymentServer" {
                            ami                         = var.ami_id
                            instance_type               = var.instance
                            subnet_id                   = var.subnetID
                            security_groups             = [var.securityGroup]
                            associate_public_ip_address = true
                            key_name                    = var.serverConnectKey
                            tags                        = { Name = "paymentServer" }
                        }
            

Description: Not using requirement block hera, by default provider aws will use the latest version

variables.tf

 variable "ami_id" {
                          description = "paymentServer ami"
                          type = string
                        }
                        variable "instance" {
                          description = "paymentServer instance type"
                          type = string
                        }
                        variable "subnetID" {
                          description = "paymentServer subnet"
                          type = string
                        }
                        variable "securityGroup" {
                          description = "paymentServer instance type"
                          type = string
                        }
                        variable "serverConnectKey" {
                          description = "paymentServer instance type"
                          type = string
                        }
            

terraform.tfvars

 ami_id = "actual ami id"
                        instance = "t2.micro"
                        subnetID = "actual subnet id attached to a existing vpc"
                        securityGroup = ["actual id attached to a vpc"]
                        serverConnectKey = "ssh key-pair name to connect to instance"
                

Execution

In terminal ;
  • running terraform init
  • downloads the provider to use to connect to the cloud with LICENSE into .terraform directory

  • running terraform fmt
  • formats the contents to meet structural standard

  • running terraform validate
  • checks the code for error

  • running terraform plan
  • SCREENSHOT image

    Description: Showing what is to be executed to create the resource (ec2 instance).



  • running terraform apply
  • SCREENSHOT image

    Description Applies the plan by creating the resource in the cloud. An existing resource needed to be destroyed, it destroys that and creates the new one.

Connecting To Instance via ssh

SCREENSHOT image

Description: Using the public IP address of the created instance to connect via ssh. terraform state show "instance ADDRESS" provides info on instance including public IP.