How to configure Terraform + AWS
03.05.2024
15 min
How to set up Terraform and AWS communication and how to start working with AWS through Terraform
The first thing to do is to create a new User in IAM, we leave everything at the default:
Next, we go to the control panel of this user -> Security credentials -> Access keys -> Create access key and create Access key using the Use case “Command Line Interface (CLI)”, as shown in the screenshots below:
As you can see in the pop-up box, we are told “This is the only time that the secret access key can be viewed or downloaded. You cannot recover it later. However, you can create a new access key any time.”, which means that we need to write down or download the secret key somewhere because if this is not done. We must create a new access key if the secret key is lost. After all, we need this secret key.
Now you need to make sure that you have terraform and aws-cli installed on your computer, you can do this with the commands terraform version and aws –version
If they are not installed, it is very easy to install them using these guides:
https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli
https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
After they are installed, let’s start combining aws and terraform, first we’ll write aws configure and insert the values we received when creating the access key, in our case it turned out like this:
Reminder: the secret access key cannot be shown to anyone because, with its help, other people can gain access to your AWS account, in this tutorial we show them for educational purposes, and after the tutorial, this access key will be deleted.
So we have set up our credentials and can now proceed to terraform.
Let’s create a main.tf file, in which we will place our terraform code, which we will partially take from the terraform documentation:
https://registry.terraform.io/providers/hashicorp/aws/latest/docs
Now we need to write in the console terraform init and we got this result:
Now we will write two commands: terraform fmt, which will format the written code if it is not already formatted, and terraform validate, which will validate our code and, if everything is fine, will notify us about it:
Now, we can write a terraform plan to see everything that Terraform is going to do, and after that, if everything suits us, we write terraform apply(which also will show all changes the same way as the terraform plan):
As we can see, the command was executed successfully and now we will check if this budget setting is present on AWS, for this we will go to Budgets:
And as we can see, the budget was created using terraform.
Thus, we have shown in an easy example how to use Terraform to create an infrastructure on AWS, but of course, everything is not limited to such a simple example, with the help of Terraform you can manage various AWS resources, such as EC2 instances, VPCs, S3 buckets, RDS databases and much more.
Similar post
All posts10 min
Read more