Saturday, October 31, 2020

Terraform: For and If

Reference: https://www.concurrency.com/blog/july-2019/conditionals-and-for-in-terraform

In below article, if purpose of statement or <condition>?<true>:<false> is to filter, rather than flow control

For

In the newer versions of Terraform >= 0.12, Terraform now supports for expressions. This is mostly used for parsing preexisting lists and maps rather than generating ones. For example, we are able to convert all elements in a list of strings to upper case using this expression.

[for el in var.list : upper(el)]

The For iterates over each element of the list and returns the value of upper(el) for each element in form of a list. We can also use this expression to generate maps.

{for el in var.list : el => upper(el)}

In this case, the original element from list now correspond to their uppercase version.

Lastly, we can include an if statement as a filter in for expressions. Unfortunately, we are not able to use if in logical operations like the ternary operators we used before. The following state will try to return a list of all non-empty elements in their uppercase state.

[for el in var.list : upper(el) if el != ""]

All in all, the use of for expressions are much more limited compared to imperative programming languages. The two expressions introduced in this section, for and if are exclusively used for lists rather than flow control of the program. That is to be expected since Terraform follows the declarative programming paradigm by describing the logic of the computation rather than the control flow- describing what the program must accomplish rather than how that is accomplished.

Friday, October 30, 2020

Install Terraform on MacBook

 $ asdf plugin-add terraform https://github.com/Banno/asdf-hashicorp.git


List versions of terraform

asdf list-all terraform


You can install the different version now, or when it is needed.


$ asdf install terraform 0.13.4

$ asdf install terraform 0.12.29


$ asdf global terraform 0.13.4

$ cat .tool-versions

kubectl 1.16.10

helm 3.1.1

terraform 0.13.4

$ terraform --version

Terraform v0.13.4

To integrate terraform with python, please install terraform_external_data

pip3 install terraform_external_data

Thursday, October 29, 2020

Terraform

 

External Data Source

https://registry.terraform.io/providers/hashicorp/external/latest/docs/data-sources/data_source

https://www.terraform.io/docs/configuration/syntax-json.html

https://stackoverflow.com/questions/63296646/iterate-through-list-of-dictionaries-to-create-terraform-resources-with-differen

variable query_dict {
    default = [
        {
            name = "query1"
            workgroup = "bar"
            query = "SELECT * FROM foo"   
        },
        {
            name = "query2"
            workgroup = "bar"
            query = "SELECT * FROM baz"   
        }
    ]
}
resource "aws_athena_named_query" "olap" {

  for_each = {for idx, query in var.query_dict: idx => query}
  
  name = each.value.name
  query = each.value.query
  database = "test"
  workgroup = each.value.workgroup
} 

In the above you create map with idx as a key:

{
  "0" = {
    "name" = "query1"
    "query" = "SELECT * FROM foo"
    "workgroup" = "bar"
  }
  "1" = {
    "name" = "query2"
    "query" = "SELECT * FROM baz"
    "workgroup" = "bar"
  }
}
https://github.com/hashicorp/terraform-provider-external/issues?q=is%3Aissue+is%3Aopen+list

https://technology.amis.nl/2020/02/24/set-terraform-resource-properties-from-an-element-in-a-list-retrieved-by-a-data-source-using-a-local-value/

https://stackoverflow.com/questions/51946982/dependency-on-local-file-creation

https://registry.terraform.io/providers/hashicorp/external/latest/docs/data-sources/data_source

https://paulbrice.com/terraform/python/2017/12/18/external-provider-terraform.html
Loop and if
https://blog.gruntwork.io/terraform-tips-tricks-loops-if-statements-and-gotchas-f739bbae55f9


JSON Path Finder

 JSON Path

https://support.smartbear.com/alertsite/docs/monitors/api/endpoint/jsonpath.html


Monday, October 12, 2020

Rancher: Node disk is running full within 24 hours

 

Problem: Node disk is running full within 24 hours

Cause: predict_linear(node_filesystem_free_bytes{mountpoint!~"^/etc/(?:resolv.conf|hosts|hostname)$"}[6h], 3600 * 24) < 0


Solution:

https://forums.rancher.com/t/rancher-monitoring-is-falsely-alerting-on-var-lib-lxcfs-running-out-of-disk-space/14513

predict_linear(node_filesystem_files_free{mountpoint!~"^/(?:etc/resolv.conf|etc/hosts|etc/hostname|var/lib/lxcfs)$"}[6h], 3600 * 24)

Sunday, October 4, 2020

Kubernetes Pod Readiness Probe

Readiness Probe: 

1. httpGet 

readinessProbe:
  httpGet:
    path: /api/ready
    port: 8080

2. tcpSocket:

readinessProbe:
  topSocket:
    port:3306

3. command

readinessProbe:
  exec:
    command:
       - cat
      -/app/is_ready


Liveness Probe:

 1. httpGet 

livenessProbe:
  httpGet:
    path: /api/ready
    port: 8080 
  initialDelaySeconds: 10 
  periodSeconds: 5 
  failureThreshold: 8 

 
 

2. tcpSocket:

livenessProbe:
  topSocket:
    port:3306


3. command

livenessProbe:
  exec:
    command:
       - cat
      -/app/is_ready


 

Thursday, October 1, 2020

Helm to Deploy RabbitMQ Cluster

Reference: https://github.com/bitnami/charts/tree/master/bitnami/rabbitmq

$ helm install rabbitmq-fei-01  --namespace rabbitmq \

  --set auth.username=admin,auth.password=think4me,auth.erlangCookie=mysecretcookie,persistence.enabled=false \

    bitnami/rabbitmq



$ RABBITMQ_PASSWORD="$(kubectl get secret rabbitmq-fei-01-rabbitmq -o jsonpath='{.data.rabbitmq-$ password}' | base64 --decode)"

$ RABBITMQ_ERLANG_COOKIE="$(kubectl get secret rabbitmq-fei-01-rabbitmq -o jsonpath='{.data.rabbitmq-erlang-cookie}' | base64 --decode)"


$ helm upgrade rabbitmq-fei-01 bitnami/rabbitmq \

  --set replicaCount=4 \

  --set persistence.enabled=false \

  --set auth.password="$RABBITMQ_PASSWORD" \

  --set auth.erlangCookie="$RABBITMQ_ERLANG_COOKIE" \

  --debug


$ kubectl port-forward --namespace rabbitmq svc/rabbitmq-cluster 15672:15672