Terraform is an open-source infrastructure as code software tool that allows you to define and provision infrastructure resources in a safe and repeatable way. In this tutorial, we will guide you on how to install and configure Terraform with AWS Cloud and provide a complete example.
Prerequisites
Before we begin, make sure that you have the following installed on your machine:
Installing Terraform
To install Terraform, follow these steps:
- Open a terminal window and run the following command to download the latest version of Terraform:
curl -O https://releases.hashicorp.com/terraform/1.0.5/terraform_1.0.5_linux_amd64.zip
- Run the following command to extract the downloaded file:
unzip terraform_1.0.5_linux_amd64.zip
- Run the following command to move the Terraform binary to the
/usr/local/bin
directory:
sudo mv terraform /usr/local/bin/
- Run the following command to verify that Terraform has been installed:
terraform --version
Configuring AWS Credentials
To configure AWS credentials for Terraform, follow these steps:
- Open a terminal window and run the following command:
aws configure
- Enter your AWS access key ID and secret access key when prompted.
- Enter the default region name and output format when prompted.
Creating a Terraform Configuration
Once you have installed and configured Terraform with AWS Cloud, you can create a Terraform configuration using the following steps:
- Create a new directory for your Terraform configuration.
- Create a new file named
main.tf
in the directory. - Add the following code to the
main.tf
file:
provider "aws" { region = "us-west-2"}resource "aws_instance" "example" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro"}
In this example, we are using the aws_instance
resource to create a new EC2 instance with the t2.micro
instance type and the ami-0c55b159cbfafe1f0
Amazon Machine Image (AMI).
- Save the
main.tf
file.
Initializing and Applying the Terraform Configuration
Once you have created your Terraform configuration, you can initialize and apply it using the following steps:
- Open a terminal window and navigate to the directory where your
main.tf
file is located. - Run the following command to initialize Terraform:
terraform init
- Run the following command to apply your Terraform configuration:
terraform apply
- When prompted, review the changes that Terraform will make to your infrastructure and type
yes
to apply the changes. - Wait for Terraform to provision the new EC2 instance.
Conclusion
Congratulations! You have successfully installed and configured Terraform with AWS Cloud and created a new EC2 instance using a Terraform configuration. You can now use Terraform to define and provision infrastructure resources in a safe and repeatable way.