Links
Comment on page

Customization of existing tf templates

In this document we are customizing the sample-aws terraform template to setup the DIGIT infra in aws.

Pre-requisites:

Customization

  • Clone the DIGIT-DevOps repo
$ git clone https://github.com/egovernments/DIGIT-DevOps.git
  • Here we are using AWS cloud service provider to create terraform infra. So, we are choosing sample-aws module (Terraform module is a collection of standard configuration files in a dedicated directory).
  • Open sample-aws in visual studio using the below command.
$ code DIGIT-DevOps/tree/release/infra-as-code/terraform/sample-aws
  • In that sample-aws module we can find the below terraform templates
1
main.tf
2
providers.tf
3
outputs.tf
4
variables.tf
  • main.tf will contain the main set of configuration for your module.
  • outputs.tf will contain the output definitions for your module. Module outputs are made available to the configuration using the module, so they are often used to pass information about the parts of your infrastructure defined by the module to other parts of your configuration.
  • providers.tf allow terraform to interact with cloud providers,SAAS providers. In this sample-aws our provider is aws.
  • variables.tf will contain the variable definitions for your module. When your module is used by others, the variables will be configured as arguments in the module block. Since all Terraform values must be defined, any variables that are not given a default value will become required arguments. Variables with default values can also be provided as module arguments, overriding the default value.
  • To setup the DIGIT infra we made changes in variables.tf. Open variables.tf in visual studio using the below code.
$ code DIGIT-DevOps/tree/release/infra-as-code/terraform/sample-aws/variables.tf
  • Change the values in variables.tf which are specified to replace based on our requirements.For example: cluster_name, network_availability_zones, availability_zones, ssh_key_name, db_name, db_username.
  • After customizing the values in variables.tf configure the aws credentials using the below commands.
$ aws configure --profile <profile_name>
  • Provide AWS access key id,AWS secret access key,Default region and Default output format.
  • Set aws_session _token using the below command.
$ aws configure --profile <profile_name> set aws_session_token <session_token>
  • To make sure that aws credentials are configured use the below command.
$ aws s3 ls
  • The output should be similar to the below image.
  • After that run the below commands in the terminal one after another.
$ terraform init
$ terraform apply
$ terraform plan
  • terraform init is used to initialize your code to download the requirements mentioned in your code.
  • terraform plan is used to review changes and choose whether to simply accept them or not.
  • terraform apply is used to accept changes and apply them against real infrastructure.
  • After successfully running these commands we are able to set up the infra in aws. We are able to see the config file which is used to deploy the environment.
  • Want to destroy the terraform use the below command.
terraform destroy
​
All content on this page by eGov Foundation is licensed under a Creative Commons Attribution 4.0 International License.