I use Terraform with Github to deploy changes in AWS lambda functions with Jenkins.
What I did so far is below in Github:
repository-folder
./modules/ #My folder with Terraform module for Lambda function
./modules/lambda/ # Here is the folder where I have my main.tf and variables.tf file
./lambda-function-1/ #In each lambda folder I have main.tf file with the module block
./lambda-function-2/
./lambda-function-3/
…
With Github webhook I keep track for any changes in the folders. Once any change is detected Terraform run initialize and apply the changes as part of the Jenkins pipeline.
My module is running correctly and I don’t see any errors but Terraform cannot detect any changes and my lambdas are not rebuild.
I already read all the documentation in Terraform from the modules section but I can’t find the way to complete this. My question is how can I use Terraform to detect any changes in my lambda folders and apply them via Jenkins?
What happens when you apply each lambda root module manually?
I’m assuming they are all independent from each other(i.e. different state files), let me know if not.
Were the lambdas created by the same pipeline or are you implementing this now? If so, how did you apply the changes before?
Out of curiosity, what storage backend are you using for the state file?
If I go the folder and run Terraform everything is fine and the lambdas are build smoothly.
Yes, they are independent and separated to folders as described in my first message.
I’m building this now and the lambdas have to be included as step in big pipeline with frontend/backend test and deployment.
Currently the state files are local but when I’m ready the state files will be uploaded to S3 bucket with DynamoDB key.
Please share your Jenkinsfile and all your Terraform code - otherwise there’s just so much unknown, that it’s not really possible to help. Doing this via a link to GitHub is probably easier than pasting things into the discussion forum.
If I run Terrafor init and apply manually from the lambdas folder from the Jenkins controller server everything is fine and my resources are created successfully. Unfortunately I can’t share with you the pipeline because there are lots of configuration in the block with .env variables.
So at this point I have to assume that you checked in your state file to the git repo (which is a bad idea by the way).
If I got this right, you’re using Github webhooks to trigger the Jenkins pipeline.
But you didn’t say which events specifically and what branch your pipeline is running on
My mistake now it’s removed from the root folder of the repository but there aren’t any changes. I just re-check the module folder configurations if they can be in any help:
Are you running the manual terraform commands from your Jenkins server? If so, is it just for convenience or is there another reason for that?
Note: Please use pre-formatted text when you share source code or terminal output here (for readability)
I can see in the plan above that Terraform will attempt to create all those resources, not update them. So you actually removed the state files from your modules? How are you going to update them now?
Besides, I really need to look at your Jenkinsfile pipeline declaration and your Github Webhook configuration to understand what’s going on.
Yes, this is from the test server with Jenkins and the module is not installed yet because I want to be convenient everything is fine before that.
I apologies, the code is now formatted in the previous post.
Can I share with you my repo and Jenkins pipeline for the test server in private message or this is against the rules here? Basically the file is very very simple with the 3 main steps for Jenkins to run Terraform.
Yesterday I provided you my webhook configuration settings.
You can share anything privately with me as long as you trust me
Just remember this is a community forum, you shouldn’t trust every one. The best practice in this case is to make sure that whatever you want to share does not contain sensitive data or personal information.
Something else to bear in mind: The best way to attract help is to make it easy for people to help you, which means providing as much relevant information as you can, in forms that are easy to read. (Redact truly secret information, but try to do so only when genuinely necessary.)
Right now, you’re making it pretty hard, instead, and macmiranda has been patiently persevering at trying to drag out further details - whereas I mostly just gave up on interacting in this thread once you declined to share your pipeline.