• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

instance

AWS Move Instance to Another Zone

December 23, 2021

Here’s how to move an AWS instance to another zone.

Stop the instance first.

aws ec2 stop-instances --instance-ids i-1234567890abcdef0

aws ec2 stop-instances --instance-ids i-1234567890abcdef0

Create an AMI image.

aws ec2 create-image \
--instance-id i-1234567890abcdef0 \
--name "my-ami" \
--description "my-ami"

aws ec2 create-image \ --instance-id i-1234567890abcdef0 \ --name "my-ami" \ --description "my-ami"

Create EC2 instance using Terraform. The contents of main.tf.

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
    }
  }
}
provider "aws" {
  profile = "default"
  region  = "us-east-1"
}
resource "aws_instance" "ulysses" {
  ami                           = "ami-1234567890abcdef0"
  key_name                      = "servers"
  iam_instance_profile          = "machine-role"
  instance_type                 = "t3.micro"
  subnet_id                     = "subnet-1234567890abcdef0"
  security_groups               = ["sg-1234567890abcdef0", "sg-1234567890abcdef1"]
  tags = {
    Name = "moving-instance"
    tag1 = "test1"
    tag2 = "test2"
  }
}

terraform { required_providers { aws = { source = "hashicorp/aws" } } } provider "aws" { profile = "default" region = "us-east-1" } resource "aws_instance" "ulysses" { ami = "ami-1234567890abcdef0" key_name = "servers" iam_instance_profile = "machine-role" instance_type = "t3.micro" subnet_id = "subnet-1234567890abcdef0" security_groups = ["sg-1234567890abcdef0", "sg-1234567890abcdef1"] tags = { Name = "moving-instance" tag1 = "test1" tag2 = "test2" } }

Launch it.

terraform init
terraform plan
terraform apply

terraform init terraform plan terraform apply

Filed Under: Cloud Tagged With: aws, ec2, instance, move, zone

GCP Spot Instance

November 25, 2021

You can save anywhere from 60-91% using a spot instance. The downside is, the instance can be preempted anytime.

Create a spot instance.

gcloud beta compute instances create spot-example \
--provisioning-model=SPOT \
--instance-termination-action=STOP \
--image-project=ubuntu-os-cloud \
--image-family=ubuntu-2004-lts \
--machine-type=e2-micro \
--project=your-project-id \
--zone=us-central1-a

gcloud beta compute instances create spot-example \ --provisioning-model=SPOT \ --instance-termination-action=STOP \ --image-project=ubuntu-os-cloud \ --image-family=ubuntu-2004-lts \ --machine-type=e2-micro \ --project=your-project-id \ --zone=us-central1-a

Delete instance.

gcloud compute instances delete spot-example \
--project=your-project-id \
--zone=us-central1-a

gcloud compute instances delete spot-example \ --project=your-project-id \ --zone=us-central1-a

Filed Under: Cloud Tagged With: create, delete, gcp, instance, preempted, spot

Migrate VM to Other Network

July 20, 2021

Here’s how to move a VM to another network.

Stop the VM.

gcloud compute instances stop server-name \
--zone=us-central1-c \
--project project-id

gcloud compute instances stop server-name \ --zone=us-central1-c \ --project project-id

Migrate the VM. Use self link for network and subnetwork.

gcloud compute instances network-interfaces update server-name \
--zone=us-central1-c \
--network-interface=nic0 \
--network=your-network \
--subnetwork=your-sub-network \
--project project-id

gcloud compute instances network-interfaces update server-name \ --zone=us-central1-c \ --network-interface=nic0 \ --network=your-network \ --subnetwork=your-sub-network \ --project project-id

Start the VM.

gcloud compute instances start server-name \
--zone=us-central1-c \
--project project-id

gcloud compute instances start server-name \ --zone=us-central1-c \ --project project-id

Filed Under: Cloud Tagged With: compute, gcloud, gcp, instance, move, network, network-interface, start, stop

GCP Rename VM

July 16, 2021

There’s a new beta command to rename a GCP VM.

You must stop instance first.

gcloud compute instances stop server-name \
--zone us-central1-c \
--project your-project-id

gcloud compute instances stop server-name \ --zone us-central1-c \ --project your-project-id

Rename the instance.

gcloud beta compute instances set-name old-server-name \
--new-name=new-server-name \
--zone us-central1-c \
--project your-project-id

gcloud beta compute instances set-name old-server-name \ --new-name=new-server-name \ --zone us-central1-c \ --project your-project-id

Start the instance.

gcloud compute instances start server-name \
--zone us-central1-c \
--project your-project-id

gcloud compute instances start server-name \ --zone us-central1-c \ --project your-project-id

Login.

gcloud compute ssh server-name \
--zone us-central1-c \
--project your-project-id \
--internal-ip &

gcloud compute ssh server-name \ --zone us-central1-c \ --project your-project-id \ --internal-ip &

Filed Under: Cloud Tagged With: gcp, instance, rename, set-name, stop, vm

GCP Keep Disk

May 6, 2021

Keep your disks when a VM is deleted.

gcloud compute instances set-disk-auto-delete server --no-auto-delete --disk=server-disk-1
gcloud compute instances set-disk-auto-delete server --no-auto-delete --disk=server-disk-2

gcloud compute instances set-disk-auto-delete server --no-auto-delete --disk=server-disk-1 gcloud compute instances set-disk-auto-delete server --no-auto-delete --disk=server-disk-2

To delete.

gcloud compute instances set-disk-auto-delete server --auto-delete --disk=server-disk-1
gcloud compute instances set-disk-auto-delete server --auto-delete --disk=server-disk-2

gcloud compute instances set-disk-auto-delete server --auto-delete --disk=server-disk-1 gcloud compute instances set-disk-auto-delete server --auto-delete --disk=server-disk-2

Filed Under: Cloud Tagged With: deleted, disk, gcp, instance, keep, vm

Terraform GCP Instance

February 3, 2021

Here’s a Terraform script for launching an instance in Google Cloud Platform.

provider "google" {
  project = "project-id"
  region  = "us-central1"
  zone    = "us-central1-a"
}
resource "google_compute_instance" "wiki" {
  name         = "wiki"
  machine_type = "n2-standard-1"
  zone         = "us-central1-a"
  tags         = ["web-server"]
  labels = {
    name        = "wiki"
    environment = "development"
  }
  boot_disk {
    initialize_params {
      image = "centos-7-v20210122"
    }
  }
  network_interface {
    network = "default"
    access_config {
    }
  }
  service_account {
    email  = "service-account@email.com"
    scopes = ["cloud-platform"]
  }
}

provider "google" { project = "project-id" region = "us-central1" zone = "us-central1-a" } resource "google_compute_instance" "wiki" { name = "wiki" machine_type = "n2-standard-1" zone = "us-central1-a" tags = ["web-server"] labels = { name = "wiki" environment = "development" } boot_disk { initialize_params { image = "centos-7-v20210122" } } network_interface { network = "default" access_config { } } service_account { email = "service-account@email.com" scopes = ["cloud-platform"] } }

Here’s a few Terraform commands.

terraform init
terraform plan
terraform apply
terraform destroy

terraform init terraform plan terraform apply terraform destroy

Filed Under: Cloud Tagged With: compute, gcp, google, instance, launch, terraform

AWS LightSail Create Terraform

January 18, 2021

Here’s how to launch a LightSail instance using Terraform. Create a main.tf file.

terraform {
  required_providers {
    aws = {
      version = >= 3.22.0"
      source = "hashicorp/aws"
    }
  }
}
provider "aws" {
  profile = "default"
  region  = "us-east-1"
}
resource "aws_lightsail_instance" "yourinstance" {
  name              = "yourinstance"
  availability_zone = "us-east-1a"
  blueprint_id      = "amazon_linux_2"
  bundle_id         = "nano_2_0"
}

terraform { required_providers { aws = { version = >= 3.22.0" source = "hashicorp/aws" } } } provider "aws" { profile = "default" region = "us-east-1" } resource "aws_lightsail_instance" "yourinstance" { name = "yourinstance" availability_zone = "us-east-1a" blueprint_id = "amazon_linux_2" bundle_id = "nano_2_0" }

To launch, run the following Terraform commands.

terraform init
terraform apply

terraform init terraform apply

Filed Under: Cloud Tagged With: aws, create, instance, lightsail, terraform

Create Instance With Alias IP

January 14, 2021

How to create an instance from a snapshot with alias IP and reserved IPs.

#!/bin/bash
gcloud beta compute instances create jump-server \
--network-interface=aliases=10.128.1.0/24,private-network-ip=jump-server,subnet=default \
--machine-type=n1-standard-1 \``
--network-tier=PREMIUM \
--maintenance-policy=MIGRATE \
--service-account=xxxxxxxxxxxxx-compute@developer.gserviceaccount.com \
--tags=int-webserver \
--image=debian-10-buster-v20201216 \
--image-project=debian-cloud \
--boot-disk-size=20GB \
--boot-disk-type=pd-standard \
--boot-disk-device-name=jump-server-1 \
--no-shielded-secure-boot \
--shielded-vtpm \
--shielded-integrity-monitoring \
--labels=name=jump-server \
--reservation-affinity=any \
--zone=us-central1-a \
--project=airy-totality-151318

#!/bin/bash gcloud beta compute instances create jump-server \ --network-interface=aliases=10.128.1.0/24,private-network-ip=jump-server,subnet=default \ --machine-type=n1-standard-1 \`` --network-tier=PREMIUM \ --maintenance-policy=MIGRATE \ --service-account=xxxxxxxxxxxxx-compute@developer.gserviceaccount.com \ --tags=int-webserver \ --image=debian-10-buster-v20201216 \ --image-project=debian-cloud \ --boot-disk-size=20GB \ --boot-disk-type=pd-standard \ --boot-disk-device-name=jump-server-1 \ --no-shielded-secure-boot \ --shielded-vtpm \ --shielded-integrity-monitoring \ --labels=name=jump-server \ --reservation-affinity=any \ --zone=us-central1-a \ --project=airy-totality-151318

The default command uses –private-network-ip and –subnet options separately.

--private-network-ip 10.0.0.24 \
--subnet=default \

--private-network-ip 10.0.0.24 \ --subnet=default \

But when dealing with aliases, reserved IPs and subnets, use a single –network-interface option instead.

--network-interface=aliases=10.128.1.0/24,private-network-ip=jump-server,subnet=default \

--network-interface=aliases=10.128.1.0/24,private-network-ip=jump-server,subnet=default \

Filed Under: Cloud Tagged With: create, gcloud, gcp, instance, sdk, snapshot

  • Go to page 1
  • Go to page 2
  • Go to page 3
  • Go to Next Page »
  • Home
  • About
  • Archives

Copyright © 2023