Posts

Showing posts with the label terraform

Terraform Module with Count Can't Have Duplicate Elements

Image
Clash Royale CLAN TAG #URR8PPP Terraform Module with Count Can't Have Duplicate Elements Tricky one to explain in a title, or even form a question around it, so I'll start with some code (simplified for, err, simplicity): resource "digitalocean_domain" "this_domain" { name = "${var.domain}" ip_address = "${var.main_ip}" } resource "digitalocean_record" "this_a_record" { count = "${length(var.a_records)}" domain = "${var.domain}" type = "A" name = "${element(keys(var.a_records), count.index)}" value = "${lookup(var.a_records, element(keys(var.a_records), count.index))}" } Given the above being part of a module called dns , I can call it like this: dns module "example_com_dns" { source = "./modules/dns" domain = "example.com" main_ip = "1.2.3.4" a_records = { "@" = "5.6.7.8...

Terraform - having timing issues launching EC2 instance with instance profile

Image
Clash Royale CLAN TAG #URR8PPP Terraform - having timing issues launching EC2 instance with instance profile I'm using Terraform to create my AWS infrastructure. I've a module that creates an "aws_iam_role", an "aws_iam_role_policy", and an "aws_iam_instance_profile" and then launches an EC2 Instance with that aws_iam_instance_profile. "terraform plan" works as expected, but with "terraform apply" I consistently get this error: * aws_instance.this: Error launching source instance: InvalidParameterValue: IAM Instance Profile "arn:aws:iam::<deleted>:instance-profile/<deleted>" has no associated IAM Roles If I immediately rerun "terraform apply", it launches the EC2 instance with no problem. If I run a "terraform graph", it does show that the instance is dependent on the profile. Since the second "apply" is successful, that implies that the instance_policy and all that it entai...

Cannot delete an instance of module in Terraform which contains a provider

Image
Clash Royale CLAN TAG #URR8PPP Cannot delete an instance of module in Terraform which contains a provider I have a module which contains resources for: In one of my env directories I can have 0-N .tf files which is an instance of that module and each specify database name etc. So if I add another .tf file with a new name then a new database server with a database will be provisioned. All this works fine. However, if I now delete an existing database module (one of the .tf files in my env directory) I run into issues. Terraform will now try to get the state of all the previously existing resources and since that specific provider (for that postgres server) now is gone terraform cannot get the state of the created postgres role, with the output a provider configuration block is required for all operations . I understand why this happens but I cannot figure out how to solve this. I want to "dynamically" create (and remove) postgres servers with a database on them but this requir...

Terraform: how to read list of maps?

Image
Clash Royale CLAN TAG #URR8PPP Terraform: how to read list of maps? See the example below: data "aws_kms_secrets" "api_key" { count = "${length(keys(var.keys))}" secret { name = "secret_name" payload = "${element(values(var.keys), count.index)}" } } resource "aws_api_gateway_api_key" "access_key" { count = "${length(keys(var.keys))}" name = "${var.environment}-${element(keys(var.keys), count.index)}" value = "${lookup(element(data.aws_kms_secrets.api_key.*.plaintext, count.index), "secret_name")}" } It appears to be impossible to look up the plaintext values from the data resource. value = "${lookup(element(data.aws_kms_secrets.api_key.*.plaintext, count.index), "secret_name")}" value = "${lookup(element(data.aws_kms_secrets.api_key.*.plaintext, count.index), "secret_name")}" Results in lookup: argument 1 should be...