Terraform is the most popular IAC tool among developers and devops engineers created by hashicorp. Anyone can use it freely to create multiple deployment environments and make the deployment procedure faster. In this article we will examine how we can use terraform AWS provider to deploy resources on AWS cloud.
The terraform AWS provider documentation can be found in the below link.
hashicorp/aws | Terraform Registry
The first need we will need to do is to create a user in AWS from IAM in order to create an access token for the deployment. By navigating in the IAM tab you can go and create a new user for terraform. I gave this user the name terraform
Then by pressing the user you can go in the security credentials tab and create a new access key. Those will be needed in the terraform script later on.
When creating the user you must specify the permission policies to attach. This will allow the necessary actions on the infrastructure. As in the provided terraform script below I only create a new vpc I should use the least privilege principal and only provide the permissions that are required. As a result I do not provide administrator access but only AmazonVPCFullAccess for this user. This build in policy rule allow full access on VPCs like creating, updating, deleting etc.
After those steps I will need to run my terraform script to create the resources I need. First you will need to initialize the terraform so that it downloads the providers stated in the files
terraform init
and the second step would be to apply the configuration
terraform apply
when you apply the configuration you will view what is created or deleted
Code:
terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } } required_version = ">= 1.2.0" } provider "aws" { region = "eu-west-1" access_key = "KEY" // access key you generated for the user secret_key = "SECRET" // secret of the key } resource "aws_vpc" "vpc-test" { cidr_block = "10.10.0.0/16" tags = { Name = "ExampleAppServerInstance" } }
When the deployment finishes you can find your vpc in your account.
Build infrastructure | Terraform | HashiCorp Developer
Youtube video: