How to Create Terraform Custom Job in Spinnaker
- 1.How to configure and set up Terraform stage in Spinnaker as a custom stage job.
- 2.How to execute Terraform operations through.
- 1.Understanding of spinnaker and spinnaker custom job.
- 2.Running spinnaker with Kubernetes cluster account configured in it.
- At OpsMx we have developed open-source TerraSpin micro-service which seamlessly integrates with spinnaker through spinnaker custom job. Which ideally creates three stages in spinnaker ( TSPlanJob, TSApplyJob, and TSDestroyJob ). Each stage has its own input fields and output.
Execute the below steps to configure Spinnaker with TerraSpin Custom Job.
- 1.Create a file ‘artifactaccounts.json’ add the below content and replace values according to your artifact account. The
artifactaccounts.jsoncontains account details from where the job pulls terraform code. The details are:
- accountname : This can be any descriptive name without spaces.
- artifacttype: Github, S3 or any of the supported artifiact repositories.
- Username and Password: These are the credentials for accessing the repo that will be specified during the stage input.
Note: Ensure GIT Username & Password doesn't have ‘@’ included.
2.Create a secret from where TerraSpin Job will read the information provided in
kubectl create cm terraspinbackendconfig --from-file=artifactaccounts.json -n default => ”default”
The default namespace is the one that is planned for using the TerraSpin Jobs. Ensure that access to this namespace is limited as credentials are available here.
3. Create a file
orca-local.ymlwith the following contents and replace ALL occurrences of:
- account: Name of the spinnaker kubernetes account.
- application: Name of the application in Spinnaker.
- credentials: Provide the same Kubernetes Account Name provided above.
- namespace (under metadata): Namespace where the Terraform Job should run. This should be the same namespace where terraspinbackendconfig configmap was created in (a) above.
4. Navigate to the following location:
~/.hal/default/profiles in halyard pod/machine
orca-local.ymlfile created above. In case an
orca-local.ymlalready exists, please append the contents as appropriate.
5. Execute the following in halyard pod or on halyard machine:
hal deploy apply
At this point spinnaker configuration for Terraform Customer Job stage is complete. Please wait for all pods to restart and stabilize.
Once spinnaker configuration for Terraform Customer Job stage is complete, the following stages should be available in pipeline configuration:
This stage does Terraform infra-code initial formal run basically (terraform init and terraform plan). This stage has the following five inputs.
- 1.Tf Script Account : This must be one of the “account” values that is defined in
artifactaccounts.json. You have to choose that account where your tf script is present.
- 2.Terraform plan:
Note: The two // separating the repo and the folder containing the terraform root module script. E.g. for
- This is a location of Terraform Script.
- Provide the location in the form of ‘
github: OpsMx/staging-terraform-pm.git//azure/k8cluster. The credentials for accessing this repo were provided in
- 3.Override file (optional): If present, the file specified here will be applied on the root module. Possible use-case might be to provide a tfvars file.Note: If you would like to ignore this option, ensure the block is empty by removing the help text available
- 4.Tf state account: This must be one of the “account” values that is defined in
artifactaccounts.json.You have to choose that account where you want to store the tf state.
- 5.State repo: This is repo where the terraform state files are stored and retrieved across multiple stages such as between plan and apply, apply and destroy. This is mandatory for the Terraform Custom Job Stage to function.Note: As state-information can contain credentials in plain-text, this repo should have control access. Same account name and their credentials will be used to access this repo (E.g. opsmx/staging-terraform-states.git).
- 6.UUId: This can be any unique string based on user choice to identify the terraform state across multiple stages. It is not mandatory to have all the stages(TSPlan, TSApply, TSDestroy) in the same pipeline. However, they all should have the same UUid.
Output: This stage will show terraform init and plan command output.
Functionality of this stage is to create terraform infra-code( terraform apply ). Here stage output will have properties with terraform infra-code output-values in a key-value format so that user can use those values in the next subsequent stage of pipeline this stage has four inputs.
- 1.Artifact account
- 2.Override file
- 3.Tf State Account
- 4.State repo
Output: This stage will show terraform apply command output.
Functionality of this stage to destroy terraform infra-code basically ( terraform destroy ) this stage has four inputs.
- 1.Tf State Account.
- 2.State repo.
Output: This stage will show terraform destroy command output