Terraform modules aws access keys

Viewed 2581

I'm using the following:

  • Windows 10
  • Visual Studio Code v1.17.0
  • Terraform v0.10.3
  • AWS

I'm very new to Terraform. I've been reading a series of posts here which have been very helpful

I'm struggling when it comes to passing AWS access keys to my modules. I have a very basic folder structure:

root
|__terraform.tfvars
|__management
    |__network
        |__main.tf
        |__vars.tf
|__modules
    |__network
        |__main.tf
        |__vars.tf

The contents of the relevant files are at the bottom of the post.

From the root/management/network directory I've run the terraform get command to import the modules.

I then run the following command, expecting everything to work:

terraform plan -var-file="../../terraform.tfvars" -state="../management.tfstate"

However, I get the following error:

Error asking for user input: 1 error(s) occurred:

* module.mgmt-network.provider.aws: 1:3: unknown variable accessed: 
var.aws_region in:

${var.aws_region}

I tried hardcoding the region, but then it just throws the same error but for the aws_access_key

In essence I'm just trying to understand how I can specify these variables to use in all my modules.

root/terraform.tfvars

aws_access_key  = "XXX"
aws_secret_key  = "YYYYYY"
aws_region      = "eu-west-2"

root/management/network/main.tf

module "mgmt-network" {
    source                          = "../../modules/network"

    network_address_space           = "${var.network_address_space}"
    enable_dns_hostnames            = true
    public_subnet_1_address_space   = "${var.public_subnet_1_address_space}"
    map_public_ip_on_launch         = true
}

root/modules/network/main.tf

--   PROVIDER
provider "aws" {
    access_key  = "${var.aws_access_key}"
    secret_key  = "${var.aws_secret_key}"
    region      = "${var.aws_region}"
}

--   DATA
data "aws_availability_zones" "availability_zones" {}

resource "aws_vpc" "vpc" {
    cidr_block              = "${var.network_address_space}"
    enable_dns_hostnames    = "${var.enable_dns_hostnames}"
}

resource "aws_subnet" "public-subnet1" {
    cidr_block              = "${var.public_subnet_1_address_space}"
    vpc_id                  = "${aws_vpc.vpc.id}"
    map_public_ip_on_launch = "${var.map_public_ip_on_launch}"
    availability_zone       = "${data.aws_availability_zones.availability_zones.names[0]}"
}

root/modules/network/vars.tf

variable "aws_access_key" {}
variable "aws_secret_key" {}
variable "aws_region" {}

variable "network_address_space" {
    description = "cidr block of available ips in the network"
}

variable "enable_dns_hostnames" {
    description = "boolean to enable dns names"
}

variable "public_subnet_1_address_space" {
    description = "cidr block of available ips in the first public subnet"
}

variable "map_public_ip_on_launch" {
    description = "boolean to enable public ips in subnet"
}
1 Answers

I believe what you are missing is declaring your variables before using them. I would expect those definitions to be in your vars.tf file in the modules/network and root/management folder.

Looking something like this:

variable "aws_access_key" { }
variable "aws_secret_key" { }
variable "aws_region"     { }

Here is an example of variables.tf file which I am guessing is what vars.tf is supposed to be in this project: https://github.com/terraform-aws-modules/terraform-aws-vpc/blob/master/variables.tf

And more details on variables: https://www.terraform.io/docs/configuration/variables.html

Related