Terraform in YC

Install yandex cloud CLI:

curl https://storage.yandexcloud.net/yandexcloud-yc/install.sh | bash

Initialize cloud:

yc init

Assuming that you've created a directory already. If not, do this.

Create service account:

SVC_ACCT="berendyaev-terraform"
FOLDER_ID="REPLACE_TO_YOUR_OWN"
yc iam service-account create --name $SVC_ACCT --folder-id $FOLDER_ID

Assign role:

ACCT_ID=$(yc iam service-account get "berendyaev-terraform" | \
                        grep ^id | \
                        awk '{print $2}')
yc resource-manager folder add-access-binding --id $FOLDER_ID \
    --role editor \
    --service-account-id $ACCT_ID

Create IAM token:

mkdir ~/.yandex
yc iam key create --service-account-id $ACCT_ID --output ~/.yandex/key.json

Change current directory to ./terraform

cd ./terraform

Run Terraform initialization

terraform init

Create your own tvfars file

cp terraform.tfvars.example terraform.tfvars

Set your own cloud/folder ID in that file. You can get these ID with yc config list. Don't forget to generate SSH keys.

Apply infra.

terraform plan
# If no error present run:
terraform apply
yes

Terraform ouputs public IP address of the instance. Use it to ssh to the host:

ssh -i ~/.ssh/<username> <username>@<ip address>

Ensure that hostname has been changed according to the hostname argument in the instance resource.

Do not forget to destroy everything:

terraform destroy -auto-approve
Docker-machine in YC

Install go

sudo add-apt-repository ppa:longsleep/golang-backports
sudo apt update
sudo apt install golang-go

Install docker-machine plugin

go get -u github.com/yandex-cloud/docker-machine-driver-yandex

The plugin has been installed in $HOME/go/bin. Make sure this path is in your PATH environment variable.

Set your YC folder ID and SA key path (see Terraform section):

FOLDER_ID="SET_YOUR_OWN_ID"
SA_KEY_PATH="/SET/YOUR/OWN/PATH"

Create Docker host

docker-machine create \
    --driver yandex \
    --yandex-image-family "ubuntu-1804-lts" \
    --yandex-platform-id "standard-v1" \
    --yandex-folder-id $FOLDER_ID \
    --yandex-sa-key-file $SA_KEY_PATH \
    docker-host

Connect to docker-host docker engine:

eval $(docker-machine env docker-host)

Run docker run hello-world to make sure that everything is working fine.

Packer in YC

Run cd packer

Run cp variables.json.example variables.json and put your own variable values

To validate template, run:

packer validate -var-file=variables.json ubuntu16.json

To build image, run:

packer build -var-file=variables.json ubuntu16.json

To build VM from image, run:

cd validate-image
cp terraform.tfvars.example terraform.tfvars
# Populate terraform.tfvars with your own values
terraform plan
terraform apply