while running terraform init after doing terraform import for existing GCP infrastructure i am getting
error:-Invalid multi-line string
│ Quoted strings may not be split over multiple lines. To produce a
│ multi-line string, either use the \n escape to represent a newline
│ character or use the "heredoc" multi-line template syntax.
even though if i check the file for which this error is thrown i can see terraform import had "\n" post each line in .tf file yet error comes .
We can edit .tf file if created by us but since its generated by terraform import not sure if its good to update even when there is already \n used .
Terraform v1.3.6
on linux_amd64
i can try heredoc but since there are so many \n i am sure it would mess up
We can edit .tf file if created by us but since its generated by terraform import not sure if its good to update even when there is already \n used .
I am not sure what you mean by this. Even while doing import one has to write the terraform config manually beforehand and then run terraform import .... command on the respective resource.
However, as you stated the issue is most likely in the user_data attribute of the resource. I would recommend using templatefile function for user_data which should avoid your new lines issue out of the box.
You can refer to an example for this at https://github.com/ishuar/terraform-eks/blob/main/examples/private_cluster/eks-ec2-private-jump-host.tf#L262 from me.
Related
I am new to Terraform and its CDK. I am confuse about the following:
When I try to run the tf.json generated through cdktf synth using cdktf deploy, terraform plan or terraform apply, the console keeps telling me that all attributes inside the access_config are required and emit errors, but I checked the documentation, it is said that these field can be optional.
So, I want to know is it a bug or the documentation is wrong ?
If you are checking the correct version of Terraform documentation and still see in tf plan/apply these attributes as required then you should add these attributes in your config. Might happen that the documentation is not up to date
After discuss with my colleagues, I managed to solve the problem. For the access_config, you have to fill in the attributes while leave them blank if you don't want to give any value to them:
"access_config":[{
"nat_ip":"google_compute_address.some_name.address",
"public_ptr_domain_name":"",
"network_tier":""
}]
The use for access_config is required in terraform cdk in comparison to terraform hcl. In terraform where we use HCL to write the configurations,
access_config can be left empty but for terraform cdk is needs to be populated with parameters which can be left empty.
I'm relatively new to Terraform My current $employer uses Terraform and we have init.tf files in each project.
It has:
a terraform block,
provider blocks,
data terraform_remote_state blocks
I want to understand what is this file for.
I don't see any mention of it in documentation/guides for structuring Terraform projects, e.g.:
https://www.terraform.io/language/modules/develop/structure
Terraform folder structure
https://www.digitalocean.com/community/tutorials/how-to-structure-a-terraform-project
https://www.terraform-best-practices.com/code-structure
I do see some (scant) mentions of other people having this file, e.g. https://github.com/hashicorp/terraform/issues/21722
I don't see any mention of it in the terraform code base:
[~/git/terraform] main ± git log -S init.tf
[~/git/terraform] main ±
I also checked "Up and Running with Terraform" (3rd edition), no mention of init.tf
Did this used to be a convention? What's going on here? :)
It is more likely that the person who named it as init.tf wants to convey a message that it is required to initialize terraform.
you can continue to work by run the commands - terraform init, terraform plan etc.
The norm is to name the file as provider.tf, although it doesn't matter how you name the terraform files. It can be whatever.tf as long as it ends with .tf
i was following this tutorial https://learn.hashicorp.com/tutorials/terraform/state-cli?in=terraform/cli and i came across a problem.
I'm using cmd.exe
i copied&pasted command terraform import aws_security_group.sg_8080 $(terraform output -raw security_group) from article Remove resource from the state, and i got error visible below, What should i do?
The import command expects two arguments.
Usage: terraform [global options] import [options] ADDR ID
Import existing infrastructure into your Terraform state.
This will find and import the specified resource into your Terraform
state, allowing existing infrastructure to come under Terraform
management without having to be initially created by Terraform.
The ADDR specified is the address to import the resource to. Please
see the documentation online for resource addresses. The ID is a
resource-specific ID to identify that resource being imported. Please
reference the documentation for the resource type you're importing to
determine the ID syntax to use. It typically matches directly to the ID
The tutorial you referred to is giving an example command line which relies on some expansion capabilities of typical Unix shells.
One way to achieve a similar result in the Windows Command Prompt (cmd.exe) is to manually run the nested command and then copy-paste its result into the argument of the second command.
For example, first run the command inside the $( ... ) sequence:
C:\example> terraform output -raw security_group
sg-0096a764b1e76f7fd
Here I've assumed that the output would be the same as the ID shown in the example in the tutorial, but of course in your case you will have a different ID with an sg- prefix reflecting the actual security object in your AWS account.
You can then place that security group ID into the outer command line instead of the $( ... ) sequence:
C:\example> terraform import aws_security_group.sg_8080 sg-0096a764b1e76f7fd
This two-step process should reproduce the same effect that a Unix-style shell would've achieved with the command line shown in the tutorial.
I'm not familiar enough with cmd.exe to suggest a direct single-step command similar to the one in the tutorial, and I don't have a Windows system to test on, but there is a question on the SuperUser StackExchange which is asking the same thing you asked in more general terms, agnostic of Terraform; perhaps the answers there will be helpful.
Why do I get this error (sorry a newbee)
" To specify a working directory for the plan, use the global -chdir flag"
I have my tfvars file in a folder called env-vars/dev.tfvars. So when I run Terraform plan -var-file=dev.tfvars or Terraform plan -var-file=env-vars\dev.tfvars I would like to run those set variables .
The parent directory is TfTest which contains main.tf, variables.tf etc
I'm not sure I understand the working directory vs workspace when using Vscode
You have a space between -var-file= and dev.tfvars. That means terraform sees dev.tfvars as a separate argument, hence the big bold error message "Too many command line arguments".
I was able to solve this on my own using this code
terraform plan -var-file="env-vars/dev.tfvars"
I have a code which checks if key in the loop has word Ops and if yes then assigns value to provider either aws.peer or aws.default.
provider = "${replace(each.key, "Ops", "") != each.key ? "aws.peer" : "aws.default"}"
After I run it it returns:
Error: Invalid provider reference
On ../../modules/Stack/Peering/main.tf line 13: Provider argument requires a provider name followed by an optional alias, like "aws.foo".
Not sure why
Provider selection is not allowed to be dynamic in Terraform. If you share more of your script, we might be able to give you a workaround that is specific to the solution you are building.
Provider selections cannot be dynamic like this. Although it didn’t produce an error in Terraform 0.11, it also didn’t work: Terraform 0.11 just ignored it and treated it as a literal string, just as the terraform 0.12upgrade tool showed. Terraform 0.12 has an explicit validation check for it to give you better feedback that it’s not supported.
The connections between resources and their providers happens too early for Terraform to be able to evaluate expressions in that context, because the provider must be known in order to understand the other contents of the block.
Resource w/ possible work around:
https://discuss.hashicorp.com/t/defining-provider-aliases-with-string-interpolation-not-working-in-terraform-0-12/1569/4