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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / terraform-upgrade.md
Created November 7, 2021 06:48
Terraform v1.0 Upgrade Tips
@mhmdio
mhmdio / README.md
Last active May 6, 2022 13:11
git-config

git-config

Local

git config user.name "Mohammed Yahya"
git config user.email my.almusaddar@gmail.com
@mhmdio
mhmdio / main.tf
Created December 17, 2021 12:46
terraform target
terraform plan \
-target="aws_launch_configuration.tfer--sensformer-002D-weatherapi20210301191443117900000001" \
-target="aws_autoscaling_group.tfer--tf-002D-asg-002D-20201118172430104500000036"