Jenkins Terraform AWS Pipeline (Basic Demo)

If Jenkins is already running and have Terraform installed then no need to spin up the Jenkins using jenkins_script. In case, if you need to setup the Jenkins too then you should follow the instructions in Jenkins Setup.

When Terraform is installed in Jenkins then we could use this demo code to spin up a new EC2 instance in AWS.

Prerequisites

Update the AWS access and secret keys in provider.tf file.

Quick Setup

  1. Configure Jenkins with Jenkins Groovy script. Jenkins Pipeline
  2. Run the build. Jenkins Build
  3. If the build is successful then a new EC2 instance will be available in your AWS account.

Important Notes

  • Update the correct AWS credentials in the provider.tf file.
  • We may try this terraform repo by installing terraform on local machine.

Important Commands

  • terraform init -input=false -var-file=vars.tfvars To initialize a working directory containing Terraform configuration files.
  • terraform plan -input=false -var-file=vars.tfvars Create an execution plan.
  • terraform apply -input=false -var-file=vars.tfvars To apply the changes required to reach the desired state of the configuration.
  • terraform plan -input=false -var-file=vars.tfvars -out changes.terraform Capture the required changes to a separate terraform file to reach the desired state of the configuration.
  • terraform apply changes.terraform To apply the changes of pre-determined set of actions generated by a terraform plan execution plan.
  • terraform apply changes.terraform Terminates resources defined in your Terraform configuration.

References