跳至內容

How to get the username and password for my terraform-created lightsail instances?

0

Hello community. I can create a lightsail instance using terraform using the code below. But I don't know how to get password from it. I don't see any mentioning of this on the documents either.

resource "aws_lightsail_instance" "instance_singapore" {
  provider          = aws.singapore
  name              = var.instance_name_singapore
  availability_zone = var.instance_availability_zone_asia
  blueprint_id      = var.instance_blueprintid
  bundle_id         = var.instance_bundleid

  tags = {
    Environment = "production"
  }
}

For blueprintid, I am using ubuntu_24_04

已提問 2 年前檢視次數 332 次

1 個回答
1

Hello.

Lightsail normally uses public key authentication instead of password authentication to connect with things like SSH.
Therefore, download the key pair and connect by following the steps in the document below.
https://docs.aws.amazon.com/lightsail/latest/userguide/amazon-lightsail-ssh-using-terminal.html

Also, if you are using the Ubuntu blueprint, the OS user will be "ubuntu".

專家

已回答 2 年前

AWS
專家

已審閱 2 年前

  • Now I couldn't create instance now with key pair generation code added. New code:

    # Create the Lightsail instance for Singapore
    resource "aws_lightsail_instance" "instance_singapore" {
      provider          = aws.singapore
      name              = var.instance_name_singapore
      availability_zone = var.instance_availability_zone_asia
      key_pair_name     = aws_lightsail_key_pair.private_key_1.id
      blueprint_id      = var.instance_blueprintid
      bundle_id         = var.instance_bundleid
    
      depends_on = [ aws_lightsail_key_pair.private_key_1 ]
      tags = {
        Environment = "production"
      }
    }
    
    provider "tls" {
      # The TLS provider is used to generate the key pair
    }
    
    # Generate an RSA private key
    resource "tls_private_key" "example" {
      algorithm   = "RSA"
      rsa_bits    = 4096
    }
    
    # AWS Lightsail key pair resource using the generated public key
    resource "aws_lightsail_key_pair" "private_key_1" {
      name       = var.key_pair_1_name
      public_key = tls_private_key.example.public_key_openssh
    }
    
    # Output the public key
    output "public_key" {
      value = tls_private_key.example.public_key_openssh
    }
    
    # Optionally, you can also output the private key for further use (e.g., for SSH access)
    # Note: Be cautious with handling private keys
    output "private_key" {
      sensitive = true
      value     = tls_private_key.example.private_key_pem
    }
    

    It says:

    `operation error Lightsail: CreateInstances, https response error StatusCode: 400, RequestID: 0eefa119-62f3-4cca-b09b-5ce625ce8dbc, NotFoundException: The

  • The KeyPair does not exist:

  • key_pair_name = aws_lightsail_key_pair.private_key_1.id

    This should be referring to the keypain name as below

    key_pair_name = aws_lightsail_key_pair.private_key_1.name

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。