Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Providers and Project Initialization  ›  Lesson

Configuring Providers

Configuring Providers 12 minA provider is declared in required_providers and set up in a provider block, which holds settings like the region, can be given an alias for a second configuration, and should get credentials from the environment rather than the file.

Configuring Providers

Knowing the provider's address is half the job. You also have to tell it where to work, which account to use, and sometimes run it twice with different settings.

The rule

Declare it in required_providers, set it in a provider block, keep secrets out.

There are two separate pieces.

1. Declare which provider the configuration needs, inside the terraform block:

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

The key aws is the provider's local name. Resource types that start with aws_ use it automatically.

2. Configure it with a provider block:

provider "aws" {
  region = "ap-south-1"
}

Which settings exist depends on the provider; for AWS, region is the main one. A provider block with no alias is the default configuration.

Aliases give you more than one configuration of the same provider, for example a second region:

provider "aws" {
  alias  = "virginia"
  region = "us-east-1"
}

resource "aws_acm_certificate" "site" {
  provider    = aws.virginia
  domain_name = "crumb.example"
}

provider = aws.virginia (no quotes) picks the aliased one. Every other aws_ resource still uses the default.

A few providers need no settings at all; leave out their provider block and Terraform uses an empty default configuration. Many settings can also come from the environment: the AWS provider reads AWS_REGION if region is not set in the block, which is handy when one pipeline deploys to several regions.

Credentials do not belong in .tf files, which end up in Git. Providers read them from the environment: for AWS, AWS_PROFILE, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, the shared credentials file, or a role attached to the machine running Terraform.

The shortcut

For any provider question, ask "Declare, configure, or credential?" The answer tells you which place to look: required_providers, the provider block, or the environment.

Setting up AWS for CrumbStep 1 of 4
  1. Declare

    Add aws = { source = "hashicorp/aws" } under required_providers.

The trap: pasting an access key into the provider block "just to test". It gets committed, and leaked cloud keys are abused within minutes.

Drills

Answer fast — trust the rule.

quick drill

Where do you state that a configuration needs the hashicorp/aws provider?

quick drill

How does one resource use an aliased provider named virginia?

quick drill

A config has a default aws provider and an aliased one. A resource sets no provider. Which does it use?

quick drill

Where should AWS access keys come from?

Remember

  • Declare in required_providers, configure in provider, credential from the environment.
  • Shortcut: ask "declare, configure, or credential?"
  • Trap: keys in .tf files get committed and leaked.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainTwo regions, one configuration

Crumb's main infrastructure lives in ap-south-1, but one certificate must be created in us-east-1. Explain how to configure the AWS provider for both, how the certificate resource picks the second one, and where the AWS credentials should come from.