Terraform and AWS

AWS
Terraform

One of the main use cases for using Terraform is for building, changing and versioning infrastructures in Public Clouds such as AWS, Azure, GCP to named a few. Users can defined their configuration file by leveraging Terraform and workloads can be deploy in any of those clouds, this is how Terraform can help build a true multi-cloud application.

In this section of the lab, we will be building an instance in AWS to showcase how easy is to achieve this. It is important the user has an AWS account to complete this portion of the lab. If not please contact the proctor to find a solution.

The Terraform and AWS documentation is located here.

The terraform integration with AWS requires the installation of AWS CLI. To install this software you need to check the following documentation if installing on Linux. You can select the version for each of the specific platforms. For this lab we have already install aws cli in your shell for your usage.

Step 1 - Create the Terraform file under the AWS directory

When you click on the AWS directory create ICONs would appear. Click on the New File and name the file aws.tf

Step 2 - Configure AWS Credentials.

If you dont have an AWS account please contact the Proctor, if you do please get the following information from your account:

  • Access Key ID
  • Secret Access Key
  • Region
  • Format

aws configure

labuser@terra-vm-pod01:~#  aws configure
AWS Access Key ID [None]: Student Info
AWS Secret Access Key [None]: Student Info
Default region name [None]:Student Info
Default output format [None]: JSON

Step 3 - Create an AWS instance

During this step we will be creating our first AWS instance by leveraging the AWS Terraform resources.


provider "aws" {
  profile = "default"
  region  = "us-east-1"
}
  
  resource "aws_instance" "my_first_terraform_instance" {
  ami           = "ami-b374d5a5"
  instance_type = "t2.micro"

  tags = {
    Name = "MyTerraformInstance"
  }
}


Step 4 - Initialize terraform

With the terraform file in place we can initialize terraform for this directory on tenants. In the terminal window make sure you are in the correct directory and then execute terraform init


cd ~/terraform/AWS
terraform init

labuser@terra-vm-pod01:~/terraform/AWS$terraform init

Initializing the backend...

Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "aws" (hashicorp/aws) 3.2.0...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.aws: version = "~> 3.2"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Step 5 - Apply Terraform

Now execute terraform.


  terraform plan -out aws.plan
  terraform apply "aws.plan"
  
  
labuser@terra-vm-pod01:~/terraform/AWS$terraform apply "aws.plan"

aws_instance.my_first_terraform_instance: Creating...
aws_instance.my_first_terraform_instance: Still creating... [10s elapsed]
aws_instance.my_first_terraform_instance: Still creating... [20s elapsed]
aws_instance.my_first_terraform_instance: Still creating... [30s elapsed]
aws_instance.my_first_terraform_instance: Creation complete after 36s [id=i-07f66c6bf61497e50]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

The state of your infrastructure has been saved to the path
below. This state is required to modify and destroy your
infrastructure, so keep it safe. To inspect the complete state
use the `terraform show` command.

State path: terraform.tfstate

Step 6 - Check AWS Instance

After terraform has succesful created the AWS instance, you could log to your AWS console and check the newly created instance. This should look similar to this.

Step 7 - Destroy MyTerraform Instance

It would be wise to destroy the newly created instance to avoid potential AWS charges.


  terraform destroy
  
  
labuser@terra-vm-pod01:~/terraform/AWS$terraform terraform
aws_instance.my_first_terraform_instance: Refreshing state... [id=i-0788c413f4a7ae1b6]

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:

 - destroy