How to Create Aws Lightsail Instance With Terraform

In this guide we will explore how to launch resources in AWS Lightsail platform using terraform.

Lightsail is an easy-to-use virtual private server (VPS) provider that offers you everything needed to build an application or website for a cost-effective, monthly plan. It is provided by AWS as a simpler way to manage cloud.

Terraform is a tool that allows you to create innfrastructure in most of the popular cloud providers like AWS, GCP, Azure, Digital ocean, etc using code. It is an infrastructure as code (IaC) tool that allows you to build, change, and version infrastructure safely and efficiently. Terraform can manage both existing service providers and custom in-house solutions

To use Terraform you will need to install it. HashiCorp distributes Terraform as a binary package. You can also install Terraform using popular package managers.

The binary package is found in this page, you can download the respective binaryfor your operating system.

Use the following command to move the binary to the executable path in linux or Mac.

# Get the dirs where linux commands are stored
echo $PATH

# Move the binary to the path
mv ~/Downloads/terraform /usr/local/bin/

Verify the installation

➜ terraform
Usage: terraform [global options] <subcommand> [args]

The available commands for execution are listed below.
The primary workflow commands are given first, followed by
less common or more advanced commands.
...

This block launches maps these ports for the launched instance

resource "aws_lightsail_instance_public_ports" "app-server" {
  instance_name = module.app-server.name

  port_info {
    protocol  = "tcp"
    from_port = 3000
    to_port   = 3000
  }

  port_info {
    protocol  = "tcp"
    from_port = 22
    to_port   = 22
  }

  port_info {
    protocol  = "tcp"
    from_port = 80
    to_port   = 80
  }

  port_info {
    protocol  = "tcp"
    from_port = 443
    to_port   = 443
  }
}

Final:

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

resource "aws_lightsail_key_pair" "main_key_pair" {
  name       = "main_key"
  public_key = file("~/.ssh/id_rsa.pub")
}

resource "aws_lightsail_instance" "server0" {
  name              = "server0"
  availability_zone = "eu-west-1b"
  blueprint_id      = "centos_8"
  bundle_id         = "small_2_0"
  key_pair_name     = aws_lightsail_key_pair.main_key_pair.name
  tags = {
    "Env"    = "Dev"
    "Author" = "Eutychus"
  }
}

resource "aws_lightsail_instance_public_ports" "server0" {
  instance_name = aws_lightsail_instance.server0.name

  port_info {
    protocol  = "tcp"
    from_port = 443
    to_port   = 443
  }

  port_info {
    protocol  = "tcp"
    from_port = 80
    to_port   = 80
  }

  port_info {
    protocol  = "tcp"
    from_port = 22
    to_port   = 22
  }

  port_info {
    protocol  = "tcp"
    from_port = 80
    to_port   = 80
  }

  port_info {
    protocol  = "tcp"
    from_port = 443
    to_port   = 443
  }
}

output "instance_id" {
  value = aws_lightsail_instance.server0.id
}

output "instance_arn" {
  value = aws_lightsail_instance.server0.arn
}

output "created_at" {
  value = aws_lightsail_instance.server0.created_at
}

output "ipv6_addresses" {
  value = aws_lightsail_instance.server0.ipv6_addresses
}

Commands:

terraform init

terraform plan

terraform apply

What you get:

terraform init

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v3.54.0...
- Installed hashicorp/aws v3.54.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.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
terraform get
terraform validate
Success! The configuration is valid.

terraform plan -out=tf.plan

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_lightsail_instance.server0 will be created
  + resource "aws_lightsail_instance" "server0" {
      + arn                = (known after apply)
      + availability_zone  = "eu-west-1b"
      + blueprint_id       = "centos_8"
      + bundle_id          = "small_2_0"
      + cpu_count          = (known after apply)
      + created_at         = (known after apply)
      + id                 = (known after apply)
      + ipv6_address       = (known after apply)
      + ipv6_addresses     = (known after apply)
      + is_static_ip       = (known after apply)
      + key_pair_name      = "main_key"
      + name               = "server0"
      + private_ip_address = (known after apply)
      + public_ip_address  = (known after apply)
      + ram_size           = (known after apply)
      + tags               = {
          + "Author" = "Eutychus"
          + "Env"    = "Dev"
        }
      + tags_all           = {
          + "Author" = "Eutychus"
          + "Env"    = "Dev"
        }
      + username           = (known after apply)
    }

  # aws_lightsail_instance_public_ports.server0 will be created
  + resource "aws_lightsail_instance_public_ports" "server0" {
      + id            = (known after apply)
      + instance_name = "server0"

      + port_info {
          + cidrs     = (known after apply)
          + from_port = 22
          + protocol  = "tcp"
          + to_port   = 22
        }
      + port_info {
          + cidrs     = (known after apply)
          + from_port = 443
          + protocol  = "tcp"
          + to_port   = 443
        }
      + port_info {
          + cidrs     = (known after apply)
          + from_port = 80
          + protocol  = "tcp"
          + to_port   = 80
        }
    }

  # aws_lightsail_key_pair.main_key_pair will be created
  + resource "aws_lightsail_key_pair" "main_key_pair" {
      + arn                   = (known after apply)
      + encrypted_fingerprint = (known after apply)
      + encrypted_private_key = (known after apply)
      + fingerprint           = (known after apply)
      + id                    = (known after apply)
      + name                  = "main_key"
      + private_key           = (known after apply)
      + public_key            = <<-EOT
            ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC6gM14zM0+L+ERQFVFYbHBAjcsTGZKKx/N+qsFXxrcJqVqsyn1lp/erPqg3g6WSu/bwiAB+E22RyG9icS7Td8ssWi9vDE73DHgC5NGLm4KeP2FospEFjY6v8XVjkwQZJ+8YyCfXJ4E5cm0FGKZREakDlYeaonTaIjxkXlsZB3Yl93+KZvZ0g1WiBOU6N6NWpEQVvxYccWK4+EuQiCryELL0o4dCNrwLaYOyv/NbSYQ09m3+mvN0VRnTzo7qSOqy1U6oCVA9bhd+tRyoUsUqp3Up8jdfzEGfWr/Pqskjtl8YXySPHLEROXX/Om4AyT62EQxcPMzedPJ6HGLHnlk4EO9cBLawymdWO7AlghujksVBu9S+alOkAmJkkPzeq76WOjCTmoNxlQmEDlucukiujfWKl4hACdNVtARptvuc5+4uMYA4j4Ql+XtQ964UQa4HiGiNpoiDegzDq9GMEsQW4W5frRuOIm4R7thYGatRBkNFw+uemE5HclF8LXOuPkShhFqpDPgI1oH99covroXggV8/ovEf9ZSoshNLMHX5kXWGAWF983Cn2N5RpmqN8rfcGVq6C93njExvHDfl7bHkhT10axOLV/V4vX4lSktWVV4//vq2wMQLi5F1l7ai8scA3eYeSaWnDaJj2jnz6V5JBOPIOH/3lf7qq4oquZhmuWq3w== etowett@tuxm1or.local
        EOT
    }

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

Changes to Outputs:
  + created_at     = (known after apply)
  + instance_arn   = (known after apply)
  + instance_id    = (known after apply)
  + ipv6_addresses = (known after apply)

&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;

Saved the plan to: tf.plan

To perform exactly these actions, run the following command to apply:
    terraform apply "tf.plan"
terraform apply tf.plan
aws_lightsail_key_pair.main_key_pair: Creating...
aws_lightsail_key_pair.main_key_pair: Creation complete after 6s [id=main_key]
aws_lightsail_instance.server0: Creating...
aws_lightsail_instance.server0: Still creating... [10s elapsed]
aws_lightsail_instance.server0: Still creating... [20s elapsed]
aws_lightsail_instance.server0: Creation complete after 29s [id=server0]
aws_lightsail_instance_public_ports.server0: Creating...
aws_lightsail_instance_public_ports.server0: Creation complete after 2s [id=server0-1421794143]

Apply complete! Resources: 3 added, 0 changed, 0 destroyed.

Outputs:

created_at = "2021-08-15T10:37:27Z"
instance_arn = "arn:aws:lightsail:eu-west-1:018879709937:Instance/48d30012-c66d-4b5c-aad6-77f29044d62b"
instance_id = "server0"
ipv6_addresses = tolist([
  "2a05:d018:860:cb01:e834:1bbc:f6bb:3e4d",
])
comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy