Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions.
Configuration files describe to Terraform the components needed to run a single application or your entire datacenter. Terraform generates an execution plan describing what it will do to reach the desired state, and then executes it to build the described infrastructure. As the configuration changes, Terraform is able to determine what changed and create incremental execution plans which can be applied.
brew install terraform
Verify the installation
terraform version
Clone git repo in your computer
git clone https://github.com/rahulit1991/terraform-demo.git
Once repo clone, switch into repo and generate key pair using following command:
ssh-keygen -f <keypair-name>
Once Keypair generated, rename private key it with extention
mv <keypair-name> <keypair-name>.pem
Generate programmatic Access using this link:
https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.html#id_users_create_console
create terraform.tfvars file with following variables and assign approprate values:
1. AWS_REGION
2. AWS_ACCESS_KEY
3. AWS_SECRET_KEY
4. vpc-cidr-block
5. key_path
6. az1
7. az2
8. az3
9. public-subnet1
10. public-subnet2
11. public-subnet3
12. private-subnet1
13 private-subnet2
14. private-subnet3
Initialize provider before apply configuration. move into project directory and run below command
terraform init
Note: above command required only once for particular directory.It may takes 5-10 minutes to complete successfully.
Before apply changes. best practice, plan terraform configuration
terraform plan
Apply configuration for deploy infrastructure
terraform apply
Note output of this command.
terraform output
ssh-add -K <keypair-name>.pem
ssh -A ubuntu@<instance_eip>
If you want to destroy all configuration please run below command:
terraform destroy
Note: this command not recommanded. if you run this command it will remove all deployment at once.