As using any Hashicorp software you're presumably read and agree with following licenses terraform packer
In this example we would:
- Build image with some code to use
- Based on Ubuntu 18.04
Now set some variables to work with packer and then terraform:
unset IMAGE_FAMILY
unset PACKER_APPNAME
unset PACKER_BUILDTIME
unset IMAGE_NAME
unset TF_VAR_packer_buildtime
unset TF_VAR_image_id
unset TF_VAR_image_name
export IMAGE_FAMILY=ubuntu-1804
export PACKER_APPNAME=dwno
export PACKER_BUILDTIME=$(date +%Y-%m-%d--%H-%M)
export IMAGE_NAME=${IMAGE_FAMILY}-${PACKER_APPNAME}-${PACKER_BUILDTIME}
Build image:
packer build -machine-readable image.json | tee output
export TF_VAR_packer_buildtime=${PACKER_BUILDTIME}
export TF_VAR_image_id=$(grep -e "yandex,artifact,0,id," output | cut -d"," -f6)
export TF_VAR_image_name=${IMAGE_NAME}
This terraform code perform following actions: Create/Update:
- Create Service account
- Create VPC
- Create 3 subnets in diffirent (Availability zones(AZ))
- Deploy instance group started at 3 nodes all in diffirent AZ
tf output -json | jq -r '.[]| .value'
Now to the target group part
yc compute ig --id $(tf output -json | jq -r '.[]| .value') list-instances --format json | jq -r '.[].network_interfaces | .[].primary_v4_address | .address'
Watch out, and you need sane defaults for this block, in my case for testing, is OK to some application unavailability
deploy_policy {
max_unavailable = "${var.cluster_size}"
max_creating = "${var.cluster_size}"
max_expansion = "${var.cluster_size}"
max_deleting = "${var.cluster_size}"
}
terraform apply -auto-approve -parallelism=50