1
votes

How to run a specific terminal command after successful terraform apply?

More specifically, I would like to save output variables to some S3 bucket (the state is already in S3, but it contains sensitive output, so the bucket has a restricted access), and while this feature request is still open, I'm using this workaround:

terraform apply && terraform output -json | aws s3 cp - s3://bucket/output.json

But would it be possible to force terraform to call this command automatically?

1
How do you run Terraform? Via CI? Locally? Do you use wrapper scripts or directly call the Terraform commands?ydaetskcoR
I use a plain CLI. Essentially I have a Makefile with the command terraform apply && terraform output ... for deployment target. It is ok, but not elegant. Also it adds build time, as terraform output takes considerable time when the state is in the S3 bucket.Vladimir

1 Answers

-1
votes

Though it may seem like more upfront work and isn't exactly uploading to s3 using terraform, I would use a tool such as Jenkins which has both terraform and AWS plugins. Then you can continually add to your pipelines and see past runs.

See: https://wiki.jenkins.io/display/JENKINS/Terraform+Plugin https://wiki.jenkins.io/display/JENKINS/S3+Plugin

You could configure Jenkins to run the terraform and then you should be able to use the s3 plugin to upload to AWS.