1

This is mostly a research question as I can't seem to find out where I can run Terraform for my use case.

I want to build a web front end which I can enter details for configuration, click a button and the front end would tell Terraform to build the infrastructure. I understand I can use the cli, or create a .tf file and then use Jenkins to run it. But I'm looking for a way to basically call the Terraform cli commands through events like a lambda call.

Should I use cloud provider sdks for this?

Has anyone gone about doing something like this and if so, could you share your experiences?

SmiffyKmc
  • 801
  • 1
  • 16
  • 34
  • I cannot understand what you mean "call the Terraform cli commands through events like a lambda call". Can you explain it more clearly? – Charles Xu Apr 17 '19 at 01:52
  • Hi @Charlew, not sure how better to explain it to be honest. We need the cli of Terraform to actually use it from what I gathered, I want a UI to run Terraform instead via click of a button, run script. I'm trying to make it as serverless as I can but think a container with Terraform seems to be the only option. But how can I invoke the Terraform commands through a click of a button? – SmiffyKmc Apr 17 '19 at 06:41
  • You need to create a front end to show the buttons that you wish to the customer and a backend to make it come true when the customer click the corresponding button. That's OK that you run the backend in a container. Many languages can achieve it. It all dependents you. – Charles Xu Apr 17 '19 at 06:51
  • And how would one run the cli command in the container without manually doing it themselves? – SmiffyKmc Apr 17 '19 at 07:14
  • After the button click. The front end is all fine, I just can't seem to find out how I can get the container to run with the cli. Passing a param to it perhaps? – SmiffyKmc Apr 17 '19 at 07:15
  • For example, the customer clicks the init button, then you should send it to the back end and execute the "terraform init". In python, you can use the os.system('terraform init') to execute it in the local system. This is really an example. I have no experience with it. You can achieve it yourself. – Charles Xu Apr 17 '19 at 07:35
  • The python command is a huge help! That's what I was hoping for! Shame there is no SDK for Terraform. I might look into that actually! – SmiffyKmc Apr 17 '19 at 07:53

2 Answers2

1

You can use SLD (Stack-Lifecycle-Deployment)

It has a very cool ui and rest api

Freack_QAC
  • 21
  • 1
0

I think I found something really useful!

Lambda Terraform Call using Python

This would be what I had in mind, generate a Terraform plan file, drop into an S3 bucket, then invoke the lambda call to spin up the infrastructure. And all can be done with the front end configuration setting and an API call.

I hope this comes in handy for some other people and once I have it working I might come back and add to this.

SmiffyKmc
  • 801
  • 1
  • 16
  • 34
  • You can also have EC2 Instances run terraform plan and apply in your cicd system. –  Apr 17 '19 at 21:06
  • Hey @turtlesallday, thanks for sending on your option. Yeah, that would be my failover if I couldn't get it to work with what I had in mind. I'm trying to make this solution as serverless as possible. Pay for only the compute or resources used. Will be a tricky one but very excited to try and tackle it :) – SmiffyKmc Apr 18 '19 at 08:31
  • 1
    Your problem is going to be when Terraform needs more than the 15 minutes max execution time of a Lambda function because you want to do something like create a Cloudfront distribution or Elasticsearch domain. Then Terraform will be aborted mid execution causing dangling resources not recorded in the state file and the state file still locked (assuming you are using state locking and you should) which can be pretty catastrophic. – ydaetskcoR Apr 24 '19 at 18:12
  • @ydaetskcoR - yeah that sounds like it would be an issue and I can't assume the time period would always be the same. I was also thinking Ansible, but again, the max time period would come in. Starting to direct more towards Containers being run on an event and then executing the TF build :/ – SmiffyKmc Apr 25 '19 at 19:40
  • 1
    I run Terraform as a Gitlab CI job to do deployments (where Terraform happens to run in a container) and it's fine. Our CI infrastructure automatically scales in and out so it's not like it costs us much in engineering time or AWS costs to run. I'd certainly recommend something like this rather than some homebrew thing like in your answer. – ydaetskcoR Apr 25 '19 at 22:14