Skip to content

Instantly share code, notes, and snippets.

View mhmdio's full-sized avatar
🇵🇸
Everything as a code

Mohammed Almusaddar mhmdio

🇵🇸
Everything as a code
View GitHub Profile
@mhmdio
mhmdio / tfmv.sh
Created November 7, 2021 06:45
Terraform State File
#!/usr/bin/env bash
state_pull() {
echo ">> state pull"
cd "$1" || return 1
terraform init
terraform state pull >terraform.tfstate
cd ..
return 0
}
@mhmdio
mhmdio / why-multi-stacks.md
Last active October 22, 2021 12:22
terraform best practices

Why?

| Mitchell Hashimoto

Multiple workspaces are my recommended approach to working with Terraform. Small, focused workspaces make Terraform runs fast, limit the blast radius, and enable easier work separation by teams. The terraform_remote_state data source can be used to pass outputs from one workspace to another workspace. This enables a clean separation of responsibilities. This is also officially recommended by Terraform.

I also use multiple workspaces as a way to model environments: dev, staging, production, etc. An environment to me is a collection of many workspaces working together to create a working environment. For example, one project of mine has the following workspaces that depend on each other to create a full environment: k8s-physical, k8s-core, dns, metrics, etc.

The problem statement is that I do not have a good way to create my workspaces, create them all at once in the right order, and then destroy them if I'm done with the environment. Without this provider, I have to manually cl

@mhmdio
mhmdio / variables.tf
Last active October 21, 2021 05:31
terrraform VALIDATING INPUT WITH CUSTOM RULES
ami = "ami-0d1bf5b68307103ca"
environment_cost_allocation = "development-test"
#--->
variable "environment_cost_allocation" {
description = "Environment of the deployment."
default = "dev-test"
validation {
condition = can(regex("^(prod|dev-.*)$", var.environment_cost_allocation))
@mhmdio
mhmdio / terraform_remote_state.tf
Created October 21, 2021 05:26
terraform remote state
data "terraform_remote_state" "network_layer" {
backend = "remote"
config = {
organization = "widgetco"
workspaces = {
name = "network_layer"
}
}
}
@mhmdio
mhmdio / network.tf
Last active October 3, 2021 18:49
Terraform AWS Networking
variable "network_cidr" {
type = list(string)
default = {
dev = "10.0.0.0/16"
qa = "10.1.0.0/16"
production = "10.2.0.0/16"
}
}
variable "private_subnet_cidrs" {
@mhmdio
mhmdio / outputs.tf
Created September 30, 2021 08:30
Terraform Outputs
output "ENV" {
description = "ENV Information"
value = <<EOF
> aws_environment = ${var.environment}
> aws_account_id = ${data.aws_caller_identity.current.account_id}
> aws_region = ${data.aws_region.current.name}
> aws_partition = ${data.aws_partition.current.partition}
EOF
}
@mhmdio
mhmdio / function.sh
Last active October 6, 2021 06:27
bash-commons
#!/usr/bin/env bash
my_function () {
### ALWAYS CHECK THE RETURN CODE
# one argument required. "" evaluates to false(1)
[[ "$1" ]] || return 1
# work with the argument. exit on failure
do_something_with "$1" || return 1
do_something_else || return 1
# Success! no failures detected, or we wouldn't be here
@mhmdio
mhmdio / splunk-forwarder.md
Last active September 10, 2021 08:09
SplunkForwarder

Splunk Forwarder

cat > user-seed.conf <<EOF
[user_info]
USERNAME = csiem
PASSWORD = xxx
EOF

echo $PASS | sudo -S cp user-seed.conf $SPLUNK_HOME/etc/system/local/user-seed.conf
@mhmdio
mhmdio / tf-tools.md
Last active August 23, 2021 13:59
Terraform Tools
@mhmdio
mhmdio / tf-doc.md
Last active August 10, 2021 20:22
terraform-docs