Skip to content

Instantly share code, notes, and snippets.

@ehaselwanter
Created September 21, 2023 14:07
Show Gist options
  • Select an option

  • Save ehaselwanter/dbac037f6f6f279c2306678eb0c6fae5 to your computer and use it in GitHub Desktop.

Select an option

Save ehaselwanter/dbac037f6f6f279c2306678eb0c6fae5 to your computer and use it in GitHub Desktop.
variable "revision" {
default = 1
}
resource "terraform_data" "trigger-update" {
input = var.revision
}
resource "terraform_data" "exec-container" {
triggers_replace = [
terraform_data.trigger-update
]
connection {
type = "ssh"
user = "ubuntu"
private_key = tls_private_key.ssh_key.private_key_pem
host = aws_instance.web.public_ip
}
provisioner "remote-exec" {
inline = [
"echo revision:${var.revision} > terraform_data.txt",
]
}
}
@ehaselwanter
Copy link
Author

but you need to update terraform

wget https://releases.hashicorp.com/terraform/1.5.7/terraform_1.5.7_linux_amd64.zip
unzip terraform_1.5.7_linux_amd64.zip 
./terraform init
./terraform apply

@ehaselwanter
Copy link
Author

sample output:

root@terraform:/home/terraform# ./terraform apply -var "revision=2"
terraform_data.trigger-update: Refreshing state... [id=888a3e15-9705-98c7-0603-720d47627981]
tls_private_key.ssh_key: Refreshing state... [id=f8ede42de11651c63ed474a564978d9bd8dc691c]
local_file.foo: Refreshing state... [id=57b90cb1b41a636519639eeb3d7433b23eedcaab]
data.aws_ami.ubuntu: Reading...
aws_key_pair.ssh_key_pair: Refreshing state... [id=ssh_key]
aws_vpc.lab_vpc: Refreshing state... [id=vpc-05658209205c08040]
data.aws_ami.ubuntu: Read complete after 1s [id=ami-0b6968e5c7117349a]
aws_internet_gateway.default: Refreshing state... [id=igw-0d97b63f94396a9f8]
aws_security_group.allow_ssh: Refreshing state... [id=sg-008d55e2a555a40fd]
aws_subnet.lab_subnet: Refreshing state... [id=subnet-09edb26daf9ab33a4]
aws_default_route_table.route_table: Refreshing state... [id=rtb-06fca971872a9e5de]
aws_instance.web: Refreshing state... [id=i-00a12b8e7ae76dfd5]
terraform_data.exec-container: Refreshing state... [id=b3c0ab5a-0cc4-5782-d93f-129aced31a47]
null_resource.web: Refreshing state... [id=6534807927316689107]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place
-/+ destroy and then create replacement

Terraform will perform the following actions:

  # terraform_data.exec-container must be replaced
-/+ resource "terraform_data" "exec-container" {
      ~ id               = "b3c0ab5a-0cc4-5782-d93f-129aced31a47" -> (known after apply)
      ~ triggers_replace = [
          ~ {
                id               = "888a3e15-9705-98c7-0603-720d47627981"
              ~ input            = 1 -> "2"
              ~ output           = 1 -> (known after apply)
                # (1 unchanged attribute hidden)
            },
        ]
    }

  # terraform_data.trigger-update will be updated in-place
  ~ resource "terraform_data" "trigger-update" {
        id     = "888a3e15-9705-98c7-0603-720d47627981"
      ~ input  = 1 -> "2"
      ~ output = 1 -> (known after apply)
    }

Plan: 1 to add, 1 to change, 1 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

terraform_data.exec-container: Destroying... [id=b3c0ab5a-0cc4-5782-d93f-129aced31a47]
terraform_data.exec-container: Destruction complete after 0s
terraform_data.trigger-update: Modifying... [id=888a3e15-9705-98c7-0603-720d47627981]
terraform_data.trigger-update: Modifications complete after 0s [id=888a3e15-9705-98c7-0603-720d47627981]
terraform_data.exec-container: Creating...
terraform_data.exec-container: Provisioning with 'remote-exec'...
terraform_data.exec-container (remote-exec): Connecting to remote host via SSH...
terraform_data.exec-container (remote-exec):   Host: 18.220.53.214
terraform_data.exec-container (remote-exec):   User: ubuntu
terraform_data.exec-container (remote-exec):   Password: false
terraform_data.exec-container (remote-exec):   Private key: true
terraform_data.exec-container (remote-exec):   Certificate: false
terraform_data.exec-container (remote-exec):   SSH Agent: false
terraform_data.exec-container (remote-exec):   Checking Host Key: false
terraform_data.exec-container (remote-exec):   Target Platform: unix
terraform_data.exec-container (remote-exec): Connected!
terraform_data.exec-container: Creation complete after 3s [id=cfc120d9-f657-aead-34a1-a829d98c4c42]

Apply complete! Resources: 1 added, 1 changed, 1 destroyed.

Outputs:

public_ip = "18.220.53.214"
root@terraform:/home/terraform# ssh -i key.pem ubuntu@18.220.53.214 -- cat terraform_data.txt
revision:2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment