Quantcast
Channel: Terraform - HashiCorp Discuss
Viewing all 11445 articles
Browse latest View live

Error: error deleting Batch Compute Environment : Cannot delete, found existing JobQueue relationship

$
0
0

@mshah07 wrote:

Terraform version:- 0.12.8
Whenever we change the compute env template, we have to delete JobQueue as we get following error - “Error: error deleting Batch Compute Environment : Cannot delete, found existing JobQueue relationship”
Someone posted the following solution:

resource "random_string" "name" {
  length  = 16
  special = false
  upper   = false
}
resource "aws_batch_compute_environment" "this" {
  compute_environment_name = "${var.name}-${random_string.name.result}"
  # removed for brevity

  lifecycle {
    create_before_destroy = true
  }
}

This has resolved our issue, where we don’t have to delete JobQueue anymore, but as it’s creating a new compute env, every time, the old ones still exist. What is the best practice of cleaning up old compute env, do we need to run “terraform taint” every time we do an apply?

Posts: 1

Participants: 1

Read full topic


How can I reference a module's source within the module?

$
0
0

@phillipjf wrote:

I would like to be able to reference the module’s source value within the module. In this specific case, I’d like to be able to pass in a source value and use it for resource tagging. We are using modules extensively as a governance tool and would like to be able to track AWS resources built using modules and ensuring they have been built with specific versions of the module. Although, access to meta arguments such as source, version, etc. could be useful for other purposes.

module {
    source = "git::https://example.com/vpc.git?ref=v1.2.0"
}

Then, within the module, be able to parse that value:

locals {
    module_source = reverse(split("/", self.source))[0]  # for example
    module_name = split(".git?ref=", local.module_source)[0]
    module_version = split(".git?ref=", local.module_source)[1]
}

And later use it to tag a resource:

resource aws_instance {
    ...
    tags = {
        module_version = local.module_version
        module_name = local.module_name
    }
}

Is this possible natively? If not, is there a good suggestion for achieving the end-result?

Posts: 3

Participants: 2

Read full topic

Differentiating between terraform providers in modules

$
0
0

@pkaramol wrote:

I have a Terraform module (let’s call it module1) that utilizes a google and google-beta provider of version 2.20.

I intend to call this module from another one (let’s call it module2) that (for some reason) has a restriction on the google and google-beta providers’ configuration, as follows:

provider "google-beta" {
  version = "< 2.12"
}

provider "google" {
  version = "< 2.12"
}

Is there a way to make module2 instantiate module1 (more or less as follows) and keep its providers’ restriction?

(let’s assume the below snippet is from main.tf of module2


module mymodule {
  source        = "../../../path/to/module1"
}

provider "google-beta" {
  version = "< 2.12"
}

provider "google" {
  version = "< 2.12"
}

…while at the same time, module1 gets to keep its own version of google providers?

Posts: 1

Participants: 1

Read full topic

Each.value context error when attempting to create multiple resources based on for_each on a map of objects

$
0
0

@qjqdave1 wrote:

Hello,

I am trying create a number of resources using for_each and a map of objects.
However when I apply terraform I get an invalid context errror for each.value.

Steps:
1- read the content of repos.json file and run jsondecode on it
2- extract the json object containing data and assign to reposmap variable
3- call the module that generates the repositories and pass to it reposmap variable

repos.json

{
“repos”: {
“test_repo1”: {
“key”: “test_repo1”,
“name”: “test_repo1”,
“package_type”: “docker”
},
“test_repo2”: {
“key”: “test_repo2”,
“name”: “test_repo2”,
“package_type”: “docker”
}
}
}

main.tf

locals {
reposdata = jsondecode(file("${path.module}/repos.json"))
}

module “artifactory” {
source = “./test”
reposmap = local.reposdata.repos

module artifactory

artifactory/variable.tf

variable “reposmap” {
type = map(object({
key = string
name = string
package_type = string
}))
}

Here is where defining multiple instances of repo using for_each
resource “artifactory_local_repository” “createrepos” {
for_each = var.reposmap
key = each.value.key
name = each.value.name
package_type = each.value.package_type
}

Error: each.value cannot be used in this context
on artifactory/test_repo.tf line 48, in resource “artifactory_local_repository” “createrepos”:
48: key = each.value.key
A reference to “each.value” has been used in a context in which it
unavailable, such as when the configuration no longer contains the value in
its “for_each” expression. Remove this reference to each.value in your
configuration to work around this error.
Error: each.value cannot be used in this context

Any input on this appreciated.

Posts: 1

Participants: 1

Read full topic

How can I create multiple instances with 2 network interfaces on AWS?

$
0
0

@luixtao wrote:

Hi!

I’m just trying to create some AWS instances (count > 1) but I’m just wondering how could I create multiple instances based on count variable with 2 network interfaces each one:

My TF code is like:

resource "aws_instance" "myinstance"{
 count = "2"
 ....
}

resource "aws_network_interface" "firstnic" {
 subnet_id       = xxxx
 security_groups = xxxx
 attachment {
  instance     = "${aws_instance.myinstance[count.index].id}"
  device_index = "0"
 }
}

resource "aws_network_interface" "secondnic" {
 subnet_id       = xxxxx
 security_groups = xxxxx
 attachment {
  instance     = "${aws_instance.myinstance[count.index].id}"
  device_index = "1"
 }
}

So, if I try to validate this code I get this error for each network instance:

Error: Reference to "count" in non-counted context

  on xxxx.tf line 107, in resource "aws_network_interface" "firstnic":
 107:     instance     = "${aws_instance.myinstance[count.index].id}"

The "count" object can be used only in "resource" and "data" blocks, and only  when the "count" argument is set.

Any advice? How could I create two NICs for each instance?

Thx u all!

Posts: 1

Participants: 1

Read full topic

Verify module version

$
0
0

@razvan-ig wrote:

Hi - is there some common way to identify and use the module version as a variable? The use case I’m thinking of is to block “apply” for a module pulled from git if the reference parameter is empty/HEAD, rather than a semver tag.

Posts: 2

Participants: 2

Read full topic

How to handle secret variables in Terraform? Environment variable?

$
0
0

@sulemanb wrote:

Hi All,

How can I make the values of variables as secrets or access them as environment variables in Terraform?

For example, I am provisioning a datasource resource, e.g.

resource "aws_rds_cluster" "aurora" {

    cluster_identifier            = var.db_cluster_identifier
    database_name                 = var.rds_db_name
    master_username               = var.rds_master_username
    master_password               = var.rds_master_password
    backup_retention_period       = var.backup_retention_period
    preferred_backup_window       = var.preferred_backup_window
    preferred_maintenance_window  = var.preferred_maintenance_window
    db_subnet_group_name          = aws_db_subnet_group.aurora.name
    #final_snapshot_identifier     = var.db_snapshot_cluster_identifier
    vpc_security_group_ids        = [aws_security_group.allow-aurora-db.id]
    skip_final_snapshot           = true
    ...

In here, i have a parameter like:
master_password = var.rds_master_password

whose value I am getting as plain text stored in my “terraform.tfvars”, e.g.
rds_master_password = "myDBPassword123"

My question is if I store the encoded value in the “terraform.tfvars”, is there any way i can decode it in my resource file?

I am using Gitlab as my repo. I can also store the value as an environment variable in Gitlab. Then my question is how can I get the environment variable in my Terraform resource, any pointers?

Thanks

Posts: 5

Participants: 4

Read full topic

Using community providers with Terraform Cloud API


Vcd_network_routed (TERRAFORM CRASH)

Terraform Module

Stuck on getting 'outputs.tf' to work for multiple instances generated via for_each on display name list

$
0
0

@ASG-Github-Admin wrote:

Using this as a reference: Terraform module

I have created a module that generates multiple computer instances in an Oracle Cloud Infrastructure tenancy, per display name that it receives as a list. However, I am stuck with the output component.

As per this YouTube video: HashiCorp 0.12 tour (the link is set to the timestamp I am referring to), you do not need to output specific attributes, and can just output the entire thing. This works fine for me when creating a single computer, as I can reference the output, and attributes I require.

Example

# Compute module main.tf
resource "oci_core_instance" "test-instance" {
    ...
}

# Compute module outputs.tf
output "instance" {
    value = oci_core_instance.test-instance
}

...

# main.tf #

module "test_instance" {
    ...
}

resource "test_volume" {
    ...
}

resource "oci_core_volume_attachment" "test_volume_attachment" {
    ...
    instance_id = "${module.test_instance.instance.id}"
}

However, when trying to to do this when it generates multiple computers - as shown in the code as show below, I get errors such as “the key expression produced an invalid result” for the ‘outputs.tf’ file.

Can someone tell me where I have gone wrong in the output, please?

Example code would be really helpful.

modules/compute/main.tf

resource "oci_core_instance" "test-instance" {

    for_each = local.display_name

    availability_domain = "${var.availability_domain}"
    compartment_id = "${var.compartment_id}"
    shape = "${var.shape}"
    subnet_id = "${var.subnet_id}"
    display_name = "${each.key}"
    source_details {

        source_id = "${var.source_details-source_id}"
        source_type = "image"
    }
}

modules/compute/variables.tf

variable "availability_domain" { default = "NZWJ:AP-SYDNEY-1-AD-1" 
variable "compartment_id" {}
variable "shape" {}
variable "subnet_id" {}
variable "display_name" {

    type = list(string)
    default = []
}
locals { display_name = { for v in var.display_name: v => v } }
variable "preserve_boot_volume" { default = false }
variable "source_details-source_id" {}

modules/compute/outputs.tf

output "instance" {

    value = {

        for instance in oci_core_instance.test-instance:
        instance => instance
    }
}

main.tf

...

module "test_instance" {

    source = "./modules/compute"
    compartment_id = "${oci_identity_compartment.test_compartment.compartment_id}"
    shape = "VM.Standard.E2.1"
    subnet_id = "${oci_core_subnet.test_subnet.id}"
    display_name = list("Test One", "Test Two")
    source_details-source_id = "ocid1.image.oc1.ap-sydney-1.aaaaaaaazy24niulp5e5a5oyaadjrwnwoa2g6f2hay2f26dqy63pn5sljjma"
}

resource "oci_core_volume" "test_volume" {

    availability_domain = "NZWJ:AP-SYDNEY-1-AD-1"
    compartment_id = "${oci_identity_compartment.test_compartment.compartment_id}"
    display_name = "Test Volume"
    size_in_gbs = 50
}

resource "oci_core_volume_attachment" "test_volume_attachment" {

    attachment_type = "paravirtualized"
    instance_id = "${module.test_instance.id}"
    volume_id = "${oci_core_volume.test_volume.id}"
    display_name = "Test Volume Attachment"
}

Posts: 2

Participants: 2

Read full topic

How manage multiple repositories with same state and same cloud project

$
0
0

@Narfware wrote:

Hi, i tried to deploy multiple repositories with the same remote state in the same google cloud project, the problem is when one project has a vm_instance and another one does not, the repository without this module, does a plan to destroy the vm_instance.

I wonder if its a good solution to separate the global state into single states for each repository for then import the infrastructure state from the google cloud project before do any terraform operation. (Share the same google project between the repositories).

Why I pretend by doing this is to make each repository read the state of other repos by only be able to write in its single state.

Posts: 3

Participants: 3

Read full topic

Accessing output from module in Terraform 12

$
0
0

@suaswe wrote:

HI all!

I have a module that creates a lambda function and some related stuff. I want this module to output the ARN of the lambda it creates, however I can’t seem to get it to output anything.

At present I have configured the following output in the module for testing purposes (the variable testing is defined elsewhere):

# cat outputs.tf 
output "testing" {
  value = var.testing
}

I’m calling the module as follows:

module "function_deploy" {
  source = "git::https://module/"

  function_name = "name"
  function_handler = "lambda.lambda_handler"
  function_runtime = "python3.6"
  vpc_function = "false"
}

It applies fine, the function is built using the newest version of the module, there are no errors and everything is dandy, except no output is produced. Statefile shows:

  "outputs": {},

What am I missing here?

Thanks!

Posts: 2

Participants: 2

Read full topic

How can I redeploy again ? the same resource?

How do I restrict the input variable to an allowed list, like in ARM Templates

$
0
0

@RussellMaycock wrote:

Hi,
How do I restrict the input variable to an allowed list, like in ARM Templates?

“allowedValues”: [ “” ],

Thanks

Posts: 1

Participants: 1

Read full topic


Terraform Cloud Free tier

$
0
0

@arunjan2019 wrote:

I have used terraform cloud as backend to store the statefle however I used terraform cloud as Free tier account. My question, once this free tier window is completed. Shall I still access my state file or I can’t access the free tier of Terraform cloud until I purchase the licensed edition?

Please clarify

Posts: 2

Participants: 2

Read full topic

How to automate user and password creation(RDP) using terraform in GCP

$
0
0

@arvindpai wrote:

HI All,

I wanted to have a username and its password automatically generated when VM instance is created which has OS of Windows versions and to RDP in that.
Could you suggest how terraform can be applicable?.
I tried using startup scripts with terraform version 0.11 but no luck.

Posts: 1

Participants: 1

Read full topic

Terraform Cloud organization global variables?

$
0
0

@binaryfire wrote:

Hi all,

Is there a way to specify global variables that can be used across all workspaces in an organization? This would be useful for API tokens which are the same for every workspace.

Posts: 2

Participants: 2

Read full topic

Run multiple userdata scripts on EC2 instance | AWS

$
0
0

@RamanujShastri wrote:

I have multiple powershell scripts which i need to run in ec2 userdata.

data “template_file” “userdata” {
template = file(“userdata.ps1”)
}
data “template_file” “userdata1” {
template = file(“userdata1.ps1”)
}
data “template_cloudinit_config” “cloudinit-example” {
gzip = false
base64_encode = false
part {
filename = “userdata.ps1”
content_type = “text/cloud-config”
content = data.template_file.userdata.rendered
}
part {
filename = “userdata1.ps1”
content_type = “text/cloud-config”
content = data.template_file.userdata1.rendered
}
}
resource “aws_instance” “ec2” {
ami = “ami-0e484c84e6d59f3a3”
instance_type = “t2.medium”
key_name = “cp-dev-GA”
iam_instance_profile = “admin-access-test”
user_data = data.template_cloudinit_config.cloudinit-example.rendered
}

none of the powershell scripts gets executed. Anything i am missing here

Posts: 1

Participants: 1

Read full topic

Best place for a global terraform configuration option?

$
0
0

@bocan wrote:

Hello, I’m trying to remedy an issue with Terraform that irks me: Having to type “yes” to apply, destroy, or clear a lock… I want that word to be overridable. Maybe to “aye” or “make it so” or “sure” or “oui” or “sí” or whatever else…

(I should quickly add this is mostly for my personal amusement and to play with “go” a bit)

So, I have the Terraform code and adding a CLI parameter to override it seems easy enough, and maybe adding it to individual project source somehow, but is there any way to add to add a global configuration for Terraform that I’ve never come across? You know, like a ~/.terraform/config type of thing?

Posts: 1

Participants: 1

Read full topic

Viewing all 11445 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>