Reference Terraform variable in shell script using templatefile function - terraform

I have a shell script to download a http link using wget:
wget -qO - "${mojang_server_url}"
I want to enter this link in a tfvars file, and then reference this in the shell script using Terraform.
I found a good solution here using template_file data source (it works)
data "template_file" "setup_script" {
template = file("setup.sh")
vars = {
mojang_server_url = "${var.mojang_server_url}"
}
}
However, the official Terraform documentation for template_file tells me I should be using the templatefile function instead?
I can't seem to figure out the syntax for this function, here's what I've got:
templatefile("setup.sh",{"mojang_server_url"="${var.mojang_server_url}")
it highlights templatefile, saying:
Argument or block definition required: An argument or block definition
is required here. To set an argument, use the equals sign "=" to
introduce the argument value.
Is there a way to make it so this can reference the tfvars variable ina shell script?
thank you!

You just simply have to provide a map as an input for the second argument:
data "template_file" "setup_script" {
template = templatefile("setup.sh",
{
mojang_server_url = var.mojang_server_url
}
)
}

Related

Error: Invalid multi-line string terraform

I am trying to define a resource but Terraform complains
resource "aws_cloudwatch_event_target" "my-functions-order-machine-completion-target" {
target_id = "some target id"
rule = aws_cloudwatch_event_rule.my-functions-order-machine-completion-rule.name
arn = module.lambda["myLambda"].function_arn
}
error:
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.
I saw Heredoc Strings but not sure how to use this after "resource"
Although not a great answer, shortening the name of the resource sorted out the issue for me.
I also tried interpolation syntax like so which did not work:
resource "resource" "${var.name}"

How to use format function in Terraform

I am trying to register an application in Azure AD using TF and i would like to define some redirect URIs using the arg as mentioned in the link --> https://registry.terraform.io/providers/hashicorp/azuread/latest/docs/resources/application :
reply_urls = ["https://replyurl"]
But i have to put 3 URIs and the format of the URI is like this below :
https://(project code)-yellow-(resourcegroup).azurewebsites.net/oauth2-redirect.html
https://(project code)-blue-(resourcegroup).azurewebsites.net/oauth2-redirect.html
https://(project code)-green-(resourcegroup).azurewebsites.net/oauth2-redirect.html
So the second part is changing only and the other part is constant. We cannot hardcode URIs for best practices
Also, we need to use format function
So can anyone tell how to put the reply_urls using the format function so that only the middle part changes as described above
Also, can this be done in any way using concat for that matter
You can pass the project_id and resourcegroup as input parameters to your TF code:
variable "project_id" {}
variable "resource_group" {}
Then your reply_urls could be:
reply_urls = [
format("https://%s-yellow-%s.azurewebsites.net/oauth2-redirect.html", var.project_id, var.resource_group),
format("https://%s-blue-%s.azurewebsites.net/oauth2-redirect.html", var.project_id, var.resource_group),
format("https://%s-green-%s.azurewebsites.net/oauth2-redirect.html", var.project_id, var.resource_group)
]

How to remove milliseconds from utcnow() result in Azure data factory

I want to pass a value for parameter usertime, the value should be like 2020-07-23T13:19:31Z , which will be used in my source connection url.
For this i supplied utcnow() function in the value tab. But i realized utcnow() will return the value as "2018-04-15T13:00:00.0000000Z"
To remove the millisecond part i have used the expression substring(utcnow(),1,20).
and also used expression formatDateTime('utcnow()', 'yyyy-MM-ddTHH:mm:ss').
Both my trails are useless where my expression returning error ass invalid parameter.
Could you please help me how can i supply the value 2020-07-23T13:19:31Z in Azure data factory datasource parameters.
You don't want utcNow inside quotes, here is an example from one of my pipelines using your format:
#formatDateTime(utcnow(), 'yyyy-MM-ddTHH:mm:ss')
which gives this result, setting a variable named x:
{
"name": "x",
"value": "2020-07-24T13:44:42Z"
}
Build it in 'Add dynamic content' as you can pick the functions and it will format properly if you aren't familiar.
Your substring won't work because it requires a string for the first parameter and utcnow is a timestamp.

Variable Interpolation in Terraform

I am having trouble in variable interpolation in terraform. Here is what my terraform configuration looks like. i.e variable inside builtin function
variable "key" {}
ssh_keys {
path = "/home/${var.provider["user"]}/.ssh/authorized_keys"
key_data = "${file(${var.key})}"
}
Command: terraform apply -var 'key=~/.ssh/id_rsa.pub'
It's not reading the value of "key" from command line argument or from env variable. However when i hardcore the value in .tf file, it works. Like below.
key_data = "${file("~/.ssh/id_rsa.pub")}"
The ${ ... } syntax is only used when embedding an expression into a quoted string. In this case, where your var.key variable is just being passed as an argument to a function already within a ${ ... } sequence, you can just reference the variable name directly like this:
key_data = "${file(var.key)}"
Nested ${ ... } sequences are sometimes used to pass an interpolated string to a function. In that case there would first be a nested set of quotes to return to string context. For example:
key_data = "${file("${path.module}/${var.key_filename}")}"
In this more complicated case, the innermost string expression is first evaluated to join together the two variables with a /, then that whole string is passed to the file function, with the result finally returned as the value of key_data.
It doesn't work because you were using the wrong flag for the scenario you described above.
If you want to specify a path to a file use the "-var-file" flag as follow:
terraform apply -var-file=~/.ssh/id_rsa.pub
If you must use the "-var" flag then you must specify the content of the file as follow:
terraform apply -var 'key=contenctOFPublicKey'
ssh_keys - (Optional) Specifies a collection of path and key_data to be placed on the virtual machine.
Note: Please note that the only allowed path is /home/<username>/.ssh/authorized_keys due to a limitation of Azure.
refer: AZURERM_VIRTUAL_MACHINE

Evaluate variable value from string dynamically

We need to get the value of dynamically constructed variables.
What I mean is we have a variable loaded from a property file called data8967677878788node. So when we run echo $data8967677878788node we get the output test.
Now in data8967677878788node the number part 8967677878788 needs to be dynamic. That means there could be variables like
data1234node
data346346367node
and such.
The number is an input argument to the script. So we need something like this to work
TESTVAR="data`echo $DATANUMBER`node"
echo $$TESTVAR #This line gives the value "test"
Any idea on how this can be accomplished
You can use BASH's indirect variable expansion:
data346346367node='test'
myfunc() {
datanumber="$1"
var1="data${datanumber}node"
echo "${!var1}"
}
And call it as:
myfunc 346346367
Output:
test
Your code is actually already pretty close to working, it just needs to be modified slightly:
TESTVAR="data`echo $DATANUMBER`node"
echo ${!TESTVAR}
If $DATANUMBER has the value 12345 and $data12345node has the value test then the above snippet will output test.
Source: http://wiki.bash-hackers.org/syntax/pe#indirection

Resources