Lesson 5 of 33
In Progress

Getting started with the AWS provider

Tyler August 22, 2024

In this lesson, we’ll configure our AWS provider, and then, in the next lesson, we’ll use that provider to launch our first AWS resource by creating an S3 bucket.

We’ll refer to the AWS Provider’s resource documentation to learn how to do this, but first, we need to set up our local development environment.

Step 1: Create a new directory

Create a new directory for your Terraform code and navigate to it.

❯ mkdir cybr-terraform-code && cd $_
Code language: PHP (php)

Step 2: Create a new file

Create a new file named main.tf and open it in your code editor.

code main.tf
Code language: CSS (css)

Pro tip: If that doesn’t work, it’s because you don’t have the code shortcut set up. You can open Visual Studio Code, hit CMD + SHIFT + P, search for Shell Command: Install 'code' command in PATH, and hit enter.

Step 3: Writing the Terraform code – specify a provider

To get started, we need to specify which provider we’re writing this code for. Since this is a course about how to use Terraform with AWS, we want to use the AWS provider. To do that, we can go back to the AWS provider’s page, and then click on Use Provider in the top right corner. The dropdown will provide you with code that you can copy/paste into your code editor, like this:

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "5.63.0"
    }
  }
}

provider "aws" {
 # Configuration options
}
Code language: PHP (php)

Keep in mind that your version number may be different from mine depending on when you’re going through the course — but that’s not a problem!

To understand how to further configure this provider, we can click on Documentation right next to Use Provider.

Looking at example usage, we can see an example that provides a region and sets it to us-east-1 which is what we want for Cybr’s lab environments. If you are using your own AWS account and prefer using a different region, you can of course modify this value:

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "5.63.0"
    }
  }
}

provider "aws" {
	region = "us-east-1"
}
Code language: JavaScript (javascript)

If you plan on using a profile name when configuring your AWS CLI, like this:

❯ aws configure --profile example

Then you could also specify that profile name for this provider:

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "5.58.0"
    }
  }
}

provider "aws" {
  region = "us-east-1"
  # profile = "example" # Uncomment this line if you are using a named profile
}

Code language: PHP (php)

But if you leave off the profile name, you don’t have to specify it. Terraform will automatically look for the default profile name credentials.

(Note: if you’re not sure what you’ll want to do yet, go ahead and comment out the line in your code. You can always change it when we get to that step)

Conclusion

Go ahead and save your code changes, and then complete this lesson and I’ll see you in the next where we’ll configure our first AWS resource.

Responses

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.