# How to Trigger Terraform from AWS Lambda Function?

**URL:** <https://discuss.hashicorp.com/t/how-to-trigger-terraform-from-aws-lambda-function/49272>\
**Category:** AWS\
**Created:** [January 24, 2023, 8:38pm UTC](https://discuss.hashicorp.com/t/how-to-trigger-terraform-from-aws-lambda-function/49272 "2023-01-24T20:38:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jim420](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/jim420/32/1833_2.png) [@Jim420](https://discuss.hashicorp.com/u/Jim420)\
**Post date:** [January 24, 2023, 8:38pm UTC](https://discuss.hashicorp.com/t/how-to-trigger-terraform-from-aws-lambda-function/49272/1 "2023-01-24T20:38:43Z")

</div>

Hello,

I am thinking of automating a small task in my personal dev AWS account. I have a working Terraform script and it does what I want. The only problem is that I have to remember to destroy else I end up paying a good amount to AWS. _**It would be great if I had a Lambda which will stop/start the terraform script ( basically - terraform apply and terraform destroy )as I can schedule my Lambda easily**_. This also help me in improving my knowledge as well ( I know it may not be a recommended solution).

Really APPRECIATE if someone could help me with some starters code/ideas .

THANKS !

---

<div class="post-metadata">

**Author:** ![stuart-c](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/stuart-c/32/10789_2.png) [@stuart-c](https://discuss.hashicorp.com/u/stuart-c)\
**Post date:** [January 25, 2023, 8:16am UTC](https://discuss.hashicorp.com/t/how-to-trigger-terraform-from-aws-lambda-function/49272/2 "2023-01-25T08:16:04Z")

</div>

Yes that’s doable. Setup a lambda triggered by a time based Cloudwatch event, store your state in something like S3 and then have the code run the “terraform destroy” command. You’d need to ensure you include the terraform binary in the lambda code and have the right permissions attached to be able to access the state bucket & perform changes.

---

<div class="post-metadata">

**Author:** ![Jim420](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/jim420/32/1833_2.png) [@Jim420](https://discuss.hashicorp.com/u/Jim420)\
**Post date:** [January 25, 2023, 2:35pm UTC](https://discuss.hashicorp.com/t/how-to-trigger-terraform-from-aws-lambda-function/49272/3 "2023-01-25T14:35:43Z")

</div>

Thanks @stuart-c . I can setup the lambda with appropriate IAM permissions. I can keep the state file with all the configs in S# ( no problem until now). However, How will the lambda access the Terraform binaries – **I am unable to process**.

**Appreciate your guidance here**.

---

<div class="post-metadata">

**Author:** ![stuart-c](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/stuart-c/32/10789_2.png) [@stuart-c](https://discuss.hashicorp.com/u/stuart-c)\
**Post date:** [January 25, 2023, 4:02pm UTC](https://discuss.hashicorp.com/t/how-to-trigger-terraform-from-aws-lambda-function/49272/4 "2023-01-25T16:02:38Z")

</div>

You need to include the Terraform binary in the bundle that you are uploading to the lambda.

So for example if you were creating your lambda in Python you’d include your Python code, and libraries and the Terraform binary.

---

<div class="post-metadata">

**Author:** ![Jim420](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/jim420/32/1833_2.png) [@Jim420](https://discuss.hashicorp.com/u/Jim420)\
**Post date:** [January 26, 2023, 1:34pm UTC](https://discuss.hashicorp.com/t/how-to-trigger-terraform-from-aws-lambda-function/49272/5 "2023-01-26T13:34:45Z")

</div>

I was wondering if you could point me to some starter’s code OR share your code if you have done. I have done something similar using CloudFormation but don’t want to convert my Terraform to CloudFormation now. Something like the below link will be very helpful.

> **[How to Launch AWS CloudFormation using Lamba function](https://blog.devgenius.io/how-to-launch-aws-cloudformation-using-lamba-function-3182101fa1da)**
>
> Intro to Infrastructure as Code | AWS Automation | Python 3
