Terraform setproduct with inner loop - terraform-provider-aws

I'm trying to create a CloudWatch alarm that will cycle through instances defined in data.tf and for each on of these to cycle through the volume id's
data.tf
data "aws_instances" "instance_cloudwatch" {
instance_tags = {
Type = var.type
}
}
data "aws_ebs_volumes" "cw_volumes" {
tags = {
Name = var.name
}
}
data "aws_ebs_volume" "cw_volume" {
for_each = toset(data.aws_ebs_volumes.cw_volumes.ids)
filter {
name = "volume-id"
values = [each.value]
}
}
In the resource I created
locals {
vol_map = {
for pair in setproduct(data.aws_instances.instance_cloudwatch.ids,data.aws_ebs_volume.cw_volume.*.id) : "${pair[0]}-${pair[1]}" => {
id = pair[0]
vol = pair[1]
}
}
}
And then I try to use these pairs in the alarm dimensions
resource "aws_cloudwatch_metric_alarm" "some_alarm" {
for_each = local.vol_map
...
dimensions = {
InstanceId = each.value.id
VolumeId = each.value.vol
}
When I run terraform apply I get this error
Error: Unsupported attribute
for pair in setproduct(data.aws_instances.instance_cloudwatch.ids,data.aws_ebs_volume.cw_volume..id) : "${pair[0]}-${pair[1]}" => {*
This object does not have an attribute named "id" I tried volume_id and got the same error

The issue is that you can't use the .*. syntax (in data.aws_instances.instance_cloudwatch.ids,data.aws_ebs_volume.cw_volume.*.id) on a resource that you created with for_each. The .*. syntax only works when you use count. This is because it only works with arrays/lists, and for_each creates a map.
Try values(data.aws_instances.instance_cloudwatch.ids,data.aws_ebs_volume.cw_volume).*.id. This will get the data.aws_ebs_volume.cw_volume values as a list instead of a map, so you can then use .*. on them.

Related

rewrite data null_data_source to locals - rewrite count to for_each

data "aws_iam_role" "extra_iam_role" {
count = length(var.s3_permission_extra_roles)
name = var.s3_permission_extra_roles[count.index]
}
data "null_data_source" "extra_iam" {
count = length(var.s3_permission_extra_roles)
inputs = {
role_id = "${data.aws_iam_role.extra_iam_role[count.index].unique_id}:*"
}
}
I want to rewrite that code like this:
data "aws_iam_role" "extra_iam_role" {
for_each = var.s3_permission_extra_roles
name = each.value
}
# is that correct ?
data "null_data_source" "extra_iam" {
for_each = var.s3_permission_extra_roles
inputs = {
role_id = data.aws_iam_role.extra_iam_role????????
#what code should be in previous line
}
}
i am clueless here....please help
i want to get rid of that count.index because that cant be used by locals...
i am not sure how to rewrite that count.index into for_each similarity
#kovokilla I believe 'var.s3_permission_extra_roles' is of type set and in that case, in your first code you should do something similar to below
data "aws_iam_role" "extra_iam_role" {
for_each = toset(var.s3_permission_extra_roles)
name = each.value
}
and for second block, you can write something similar to below
data "null_data_source" "extra_iam" {
for_each = toset(var.s3_permission_extra_roles)
inputs = {
role_id = data.aws_iam_role.extra_iam_role[each.key].unique_id
}
}
Remember to define your 's3_permission_extra_roles' as type set in your variables.tf file
variable "s3_permission_extra_roles" {
description = "s3 permission roles"
default = ["role1","role2"]
type = set(string)
}

How to transform a map to map in Terraform

I have a map in Terraform, values of which I want to transform by prepending them with a string, resulting in another map.
variable "my_map" {
type = map(string)
}
locals {
my_new_map = [for key, value in var.my_map: { key = "prefix/${value}"}]
}
But local.my_new_map is a tuple instead of a map. What am I missing for the result to be a map?
You must use the map syntax with { and } :
variable "my_map" {
type = map(string)
}
locals {
my_new_map = {for key, value in var.my_map: key => "prefix/${value}"}
}

Is there any way to access individual values of a map of <key, value> pairs in karate if we do not know the value of key?

Assume that i have the following structure
Content : {
"Key1" : {
JSON_OBJECT1
}
"Key2" : {
JSON_OBJECT#
}
}
I need a way to evaluate the schema for all the JSON objects in the content structure and i do not know the value of key at runtime here, is there any way in karate to achieve the same?
Came up with a generic function to transform my Map to object of values and that fixed my issue.
"""
* def getObjects =
"""
function(array) {
var objects = []
for (var propName in array) {
var a = array[propName];
objects.push(a);
}
return objects;
}
"""

list of objects (blocks for network)

In openstack_compute_instance_v2, Terraform can attach the existing networks, while I have 1 or n network to attach, in module:
...
variable "vm_network" {
type = "list"
}
resource "openstack_compute_instance_v2" "singlevm" {
name = "${var.vm_name}"
image_id = "${var.vm_image}"
key_pair = "${var.vm_keypair}"
security_groups = "${var.vm_sg}"
flavor_name = "${var.vm_size}"
network = "${var.vm_network}"
}
in my .tf file:
module "singlevm" {
...
vm_network = {"name"="NETWORK1"}
vm_network = {"name"="NETWORK2"}
}
Terraform returns expected object, got invalid error.
What am I doing wrong here?
That's not how you specify a list in your .tf file that sources the module.
Instead you should have something more like:
variable "vm_network" { default = [ "NETWORK1", "NETWORK2" ] }
module "singlevm" {
...
vm_network = "${var.vm_network}"
}

Exjts4 grid remote summary (no grouping)

I've created Ext.grid.Panel and now I need to make a summary with data calculated on server. And I have no grouping in that grid.
In feature ftype: 'summary' there is no such property like 'remoteRoot'.
Is there any opportunity to create this summary withou grouping?
You have to extend / override class. This is an example based on AbstractSummary.js and should be optimised.
// usage in grid:
{
ftype : 'summary',
remoteRoot : 'summary'
}
//response from server
{
data : [] // our standard data
summary : {
summaryField : 123123
}
}
// our class
Ext.define('w3desApp.grid.feature.Summary', {
override : 'Ext.grid.feature.Summary',
getSummary: function(store, type, field, group) {
var reader = store.proxy.reader;
if (this.remoteRoot && reader.rawData) {
// reset reader root and rebuild extractors to extract summaries data
root = reader.root;
reader.root = this.remoteRoot;
reader.buildExtractors(true);
summaryRow = reader.getRoot(reader.rawData);
// restore initial reader configuration
reader.root = root;
reader.buildExtractors(true);
if (typeof summaryRow[field] != 'undefined') {
return summaryRow[field];
}
return '';
}
return this.callParent(arguments);
}
});

Resources