Terraform Cloud:- Terraform Cloud enables infrastructure automation for provisioning, compliance, and management of any cloud, datacenter, and service.
We are a preferred AWS consultant and offers the best cloud AWS consulting service. Our AWS-certified expert consultants conduct a thorough review and evaluation of your existing IT infrastructure and service interaction model to provide top-notch solutions.
The set of files used to describe infrastructure in Terraform is known as a Terraform configuration. You will write your first configuration to define a configure VPC and launch a EC2 instance. Each Terraform configuration must be in its own working directory. Create a directory for your configuration.
Change into the directory.
Create a file to define your infrastructure.
Open main.tf in your text editor, paste in the configuration below, and save the file.
The input file for terraform is known as Terraform Configuration. Terraform configuration is written in a specific language named Hashicorp Configuration Language and it can optionally be written in JSON as well.
Here is the sample Terraform Configuration file saved with *.tf extension
The following file presumes that you are using the AWS Config profile. So it refers to the profile: default for the authentication.
Terraform configuration file would ideally have lot of elements known as blocks such as provider , resource etc.
This is a Syntax of how Terraform Configuration file block is formatted
There are sample amount of BLOCK_TYPE available in Terraform and the resource is primary and all others are to support building that specified resource. Some of the Terraform blocks (elements) and their purpose is given below.
The provider block configures the specified provider, in this case aws. A provider is a plugin that Terraform uses to create and manage your resources.
You can use multiple provider blocks in your Terraform configuration to manage resources from different providers. You can even use different providers together. For example, you could pass aws credentials like access key and secret key.
Use resource blocks to define components of your infrastructure. A resource might be a physical or virtual component such as an EC2 instance, or it can be a logical resource such as a Heroku application.
Resource blocks have two strings before the block: the resource type and the resource name. In this example, the resource type is aws_instance and the name is web-server. The prefix of the type maps to the name of the provider. In the example configuration, Terraform manages the aws_instance resource with the aws provider. Together, the resource type and resource name form a unique ID for the resource. For example, the ID for your EC2 instance is aws_instance.web-server.
Resource blocks contain arguments which you use to configure the resource. Arguments can include things like machine sizes, disk image names, or VPC IDs. Our providers reference lists the required and optional arguments for each resource. For your EC2 instance, the example configuration sets the AMI ID to an Ubuntu image, and the instance type to t2.micro, which qualifies for AWS' free tier. It also sets a tag to give the instance a name.
To declare input variables.
To declare output variables which would be retained the Terraform state file.
To assign value to an expression, these are local temporary variables work with in a modul.
A module is a container for multiple resources that are used together.
To Collect data from the remote provider and save it as a data source.
- Step1:- Need to define provider as shown in the attachemnets.
- Step2:- for create a vpc we need to define terraform aws resouces.
In VPC we need to define CIDR and after defining we need to run the terraform plan command to review the all changes.
Than run the terraform apply command.
Now go to aws console and check in vpc service .
- Step3:- For create a internet Gateway we need to define terraform internet gateway resouces as shown below.
After define Internet Gateway we need to run the terraform plan and terraform apply commands and check in the aws vpc service console.
- Step4:- For cetare a route table we need to define terraform aws route table resouces.
After defining Route Table we need to run the terraform plan and terraform apply commands and check in the aws vpc service console.
- Step5:- For create a subnet we need to define terraform aws subnet resouces.
After defining Subnet we need to run the terraform plan and terraform apply commands and check in the aws vpc service console.
- Step6:- For associate route table with subnet we need to define terraform aws associate route table with subnet resource.
After defining associate route table with subnet resource we need to run the terraform plan and terraform apply commands and check in the aws vpc service console.
- Step7:- For create a security group we need to define terraform aws secuirity group resource.
After defining secuirity group resource we need to run the terraform plan and terraform apply commands and check in the aws vpc service console.
- Step8:- For create network interface with an IP that was created on step 5 we need to define terraform aws network interface resource.
After defining network interface resource we need to run the terraform plan and terraform apply commands and check in the aws vpc service console.
- Step9:- For Assign an elastic IP to network interface created in step 8 we need to define terraform aws Assign an elastic IP resource.
After defining Assign an elastic IP resource we need to run the terraform plan and terraform apply commands and check in the aws vpc service console.
- Step10:- For create a EC2 instance ubuntu server and install/enable apache2 we need to define terraform aws EC2 instanc resource.
After defining EC2 instance resource we need to run the terraform plan and terraform apply commands and check in the aws EC2 service console.
We recommend using consistent formatting in all of your configuration files. The terraform fmt command automatically updates configurations in the current directory for readability and consistency.
Format your configuration. Terraform will print out the names of the files it modified, if any. In this case, your configuration file was already formatted correctly, so Terraform won't return any file names.
Validate your configuration. The example configuration provided above is valid, so Terraform will return a success message.
Apply the configuration now with the terraform apply command. Terraform will print output similar to what is shown below. We have truncated some of the output to save space.
Before it applies any changes, Terraform prints out the execution plan which describes the actions Terraform will take in order to change your infrastructure to match the configuration.
The output format is similar to the diff format generated by tools such as Git. The output has a + next to aws_instance.app_server, meaning that Terraform will create this resource. Beneath that, it shows the attributes that will be set. When the value displayed is (known after apply), it means that the value will not be known until the resource is created. For example, AWS assigns Amazon Resource Names (ARNs) to instances upon creation, so Terraform cannot know the value of the arn attribute until you apply the change and the AWS provider returns that value from the AWS API.
Terraform will now pause and wait for your approval before proceeding. If anything in the plan seems incorrect or dangerous, it is safe to abort here before Terraform modifies your infrastructure.
In this case the plan is acceptable, so type yes at the confirmation prompt to proceed. Executing the plan will take a few minutes since Terraform waits for the EC2 instance to become available.
When you applied your configuration, Terraform wrote data into a file called terraform.tfstate. Terraform stores the IDs and properties of the resources it manages in this file, so that it can update or destroy those resources going forward.
The Terraform state file is the only way Terraform can track which resources it manages, and often contains sensitive information, so you must store your state file securely and restrict access to only trusted team members who need to manage your infrastructure. In production, we recommend storing your state remotely with Terraform Cloud or Terraform Enterprise. Terraform also supports several other remote backends you can use to store and manage your state.
Inspect the current state using terraform show.
When Terraform created this EC2 instance, it also gathered the resource's metadata from the AWS provider and wrote the metadata to the state file. Later in this collection, you will modify your configuration to reference these values to configure other resources and output values.
Terraform has a built-in command called terraform state for advanced state management. Use the list subcommand to list of the resources in your project's state.
Terraform has a built-in command called terraform destroy .Use the terraform destroy comand we can delete all resouces in main.tf file.
Here --auto-apporve use for appove automatically .
If terraform validate was successful and your apply still failed, you may be encountering one of these common errors.
If you use a region other than us-west-2, you will also need to change your ami, since AMI IDs are region-specific. Choose an AMI ID specific to your region by following these instructions, and modify main.tf with this ID. Then re-run terraform apply.
If you do not have a default VPC in your AWS account in the correct region, navigate to the AWS VPC Dashboard in the web UI, create a new VPC in your region, and associate a subnet and security group to that VPC. Then add the security group ID (vpc_security_group_ids) and subnet ID (subnet_id) arguments to your aws_instance resource, and replace the values with the ones from your new security group and subnet.
Save the changes to main.tf, and re-run terraform apply.