in Education by
I'm creating a list of IAM policy, which is stored in .json format. I have only 1 resource block, by using count = length(count) I want to create a multiple IAM policy. The policies are stored in .json format. I am referring to them in Terraform by using file(). If I create a multiple resources block, the program works. main.tf: resource "aws_iam_role_policy" "cloudcheckr" { count = "${length(var.file_name)}" role = "${aws_iam_role.cloudcheckr.id}" // An IAM role is created in another resource block name = "${var.file_name[count.index]}" policy = "${file("${var.file_name[count.index]}.json")}" variables.tf: variable "file_name" { type = "list" default = [ "xxxxxx", "xxxxxx", "xxxxxx", "xxxxxx", ] } Expected results: Multiple IAM policies are created. Actual results: aws_iam_role_policy.cloudcheckr: 3 error(s) occurred: * aws_iam_role_policy.cloudcheckr[3]: file: open iam_policy_cloudcheckr_security.json: no such file or directory in: ${file("${var.file_name[count.index]}.json")} * aws_iam_role_policy.cloudcheckr[0]: file: open iam_policy_cloudcheckr_cloudwatchflowlogs.json: no such file or directory in: ${file("${var.file_name[count.index]}.json")} * aws_iam_role_policy.cloudcheckr[2]: file: open iam_policy_cloudcheckr_inventory.json: no such file or directory in: ${file("${var.file_name[count.index]}.json")} JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
There is no problem from first view. But there are still several ways to work out the file path issue. ${path.module} is useful when using file() from inside a module, you generally want to make the path relative to the module base, like this: file("${path.module}/file"). So your code can be changed to resource "aws_iam_role_policy" "cloudcheckr" { count = "${length(var.file_name)}" role = "${aws_iam_role.cloudcheckr.id}" // An IAM role is created in another resource block name = "${var.file_name[count.index]}" policy = "${file("${path.module}/${var.file_name[count.index]}.json")}" } If this doesn't work, try with format() policy = "${file(format("%s/%s.json", "${path.module}, ${var.file_name[count.index]}"))}"

Related questions

0 votes
    I'm confused as to how I should use terraform to connect Athena to my Glue Catalog database. I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I am going to create a new distribution at CloudFront. Already I have uploaded my SSL certificate at AWS IAM ... for this distribution? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I am creating a below ABC -- ecs.tf (gives me cluster id) Content of ecs.tf: resource " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    How can you integrate IAM with data centers security? Select the option from below list : a) None ... Connect AWS Interview Questions and Answers Agile Interview Questions Answers...
asked Jul 21, 2021 in Technology by Editorial Staff
0 votes
    Consider a hash table of size seven, with starting index zero, and a hash function (3x + 4)mod7. ... Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    Which is the correct way to create an array in JavaScript? I) var myProg = []; II) var myArray = ["C","Java","C++","Python"]; III) ... 1. I, III 2. II, III 3. I, II 4. I, II & III...
asked Feb 24, 2021 in Technology by JackTerrance
0 votes
    I am trying out a simple example suggested by AWS documentation to create a role using a policy json file http:// ... botocore/1.3.9 Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    Which function calculates the count of each category of a categorical variable? (a) Table (b) Intact (c) ... of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    I have a function that returns False and True based on conditions defined in the function below: def find_trend(data ... False 29 True Select the correct answer from above options...
asked Jan 17, 2022 in Education by JackTerrance
0 votes
    Subtotal value is the data base are calculated with count function are true ya false Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
    Subtotal value is the data base are calculated with count function are true ya false Select the correct answer from above options...
asked Nov 29, 2021 in Education by JackTerrance
0 votes
    Which of the following function is used to find the column count of the particular resultset? (a) ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    I want to calculate a percentage, for each id, of True values from all rows of the id. Here an example ... df.num_true/df.num_col1_id Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    If, like me, you sometimes count using your fingers, odds are you can count up to 5 things on one hand ( ... finger represents a 0. Select the correct answer from above options...
asked Dec 21, 2021 in Education by JackTerrance
0 votes
    If you had enabled a Conditional Access Policy for External Users using multi-factor authentication, and then also ... users, would they trigger both when accessing content?...
asked Mar 10, 2021 in Technology by JackTerrance
...