Terraform
- 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 which has been developed by HashiCorp.
- A provider is responsible for understanding API interactions and exposing resources.
- Most of the available providers correspond to one cloud or on-premises infrastructure platform and offers resource types that correspond to each of the features of that platform.
Advantages of Terraform:
- Support multiple platforms & has hundreds of providers
- Simple configuration language with fast learning curves
- Easy integration with configuration management tools like Ansible
- Easily extensible with plugins
- Free
Creating your AWS account user and setting up a directory for the terraform manifests
Create a user in your AWS account and note the access key and the security key. Create a directory wherein you will keep the terraform code and create a new file called first_ec2.tf. This will be the file where we will write out configuration into
Creating your terraform manifest
- Considerations:
- - Ensuring authentication to AWS
- - Specifying a region for the resource
- - Specifying the resource
Navigate to https://registry.terraform.io/ to look at the requirements to launch a resource. Navigate to Documentation found in the AWS provider . Here they provide example usage of the provider and resource blocks
Provider Block : Begin building the code for the provider block based of the example and signify the region:
Use your AWS users access-key and secret-key (acquired from IAM users):
Resource Block : Begin building the code for the resource block, this requires an ami and the instance type that you are interested in. You can choose the t2.micro free instance type here on the Amazon Linux 2 operating system. The ami must be selected according to the region and many more.
Full screen shown below
Preparing your terraform directory and executing your manifest with the terraform core commands
- terraform init
The terraform init command read the configuration file that we used and found the provider to be aws and downloaded the terraform plugins needed for this. These can be found in the path .terraform\providers\registry.terraform.io\hashicorp
Each time you configure our provider, ensure to run terraform init
- terraform plan
Perform this command in this directory to see what the configuration file (the manifest) will do and review it
- terraform apply
Perform this to execute the manifest that you have.
Enter yes to execute it after it shows a review of what will happen
You can confirm this in the console
Now You can see your Ubuntu server launched Successfully.
Since this is a test, you can destroy your instance by running the command:
The destroy command can be used to destroy a complete set of cloud infrastructure or a targeted resource.
- terraform destroy
To destroy a specific resources
terraform destroy -target RESOURCE_TYPE.NAME -target RESOURCE_TYPE2.NAME
It terminates all the resources specified in your Terraform configuration file.
Here Resource_type is aws_instance and name is Ubuntu as shown below
terraform destroy -target aws_instance.Ubuntu
Type yes
Now as shown below our resources terminated successfully
Visit this link ,If you want more information ..
https://spacelift.io/blog/how-to-destroy-terraform-resources
Top comments (0)