Terraform Cheat Sheet
Common Terraform Commands
terraform init // Initialize a new or existing Terraform working directory terraform plan // Create an execution plan for changes to be applied terraform apply // Apply changes to infrastructure terraform destroy // Destroy Terraform-managed infrastructure terraform validate // Check if the configuration is valid terraform fmt // Format the configuration file
AWS-Specific Commands
Provider Configuration
provider "aws" { region = "us-west-2" access_key = "YOUR_ACCESS_KEY" secret_key = "YOUR_SECRET_KEY" }
Resources
EC2 Instance
resource "aws_instance" "example" { ami = "ami-0c94855ba95c71c99" instance_type = "t2.micro" tags = { Name = "example-instance" } }
S3 Bucket
resource "aws_s3_bucket" "example" { bucket = "example-bucket" acl = "private" }
Azure-Specific Commands
Provider Configuration
provider "azurerm" { features {} subscription_id = "YOUR_SUBSCRIPTION_ID" client_id = "YOUR_CLIENT_ID" client_secret = "YOUR_CLIENT_SECRET" tenant_id = "YOUR_TENANT_ID" }
Resources
Virtual Machine
resource "azurerm_virtual_machine" "example" { name = "example-vm" location = "westus" resource_group_name = "example-resource-group" vm_size = "Standard_DS1_v2" storage_image_reference { publisher = "Canonical" offer = "UbuntuServer" sku = "16.04-LTS" version = "latest" } storage_os_disk { name = "example-os-disk" caching = "ReadWrite" create_option = "FromImage" managed_disk_type = "Premium_LRS" } os_profile { computer_name = "example-vm" admin_username = "adminuser" admin_password = "P@ssw0rd1234!" } os_profile_linux_config { disable_password_authentication = false } tags = { environment = "production" } }
Storage Account
resource "azurerm_storage_account" "example" { name = "examplestorageaccount" resource_group_name = "example-resource-group" location = "westus" account_tier = "Standard" account_replication_type = "LRS" }
Ansible Cheatsheet: AWS and Azure Resource Deployment
AWS:
1. Install AWS SDK for Python (Boto):
pip install boto boto3
2. Configure AWS Credentials:
[default] aws_access_key_id = YOUR_ACCESS_KEY aws_secret_access_key = YOUR_SECRET_KEY
3. Create an EC2 Instance:
- name: Create an EC2 instance ec2_instance: image: ami-0c94855ba95c71c99 instance_type: t2.micro key_name: my_keypair region: us-west-2 count: 1 tags: Name: my_instance register: ec2
4. Create an S3 Bucket:
- name: Create an S3 bucket s3: bucket: my_bucket region: us-west-2 mode: create
5. Create an RDS Instance:
- name: Create an RDS instance rds: region: us-west-2 instance_name: my_instance db_instance_identifier: my_db allocated_storage: 20 engine: postgres instance_class: db.t2.micro master_username: admin master_password: Password123 vpc_security_group_ids: - sg-0123456789abcdef0
Azure:
1. Install Azure SDK for Python:
pip install azure
2. Configure Azure Credentials:
export AZURE_CLIENT_ID="YOUR_CLIENT_ID" export AZURE_CLIENT_SECRET="YOUR_CLIENT_SECRET" export AZURE_TENANT_ID="YOUR_TENANT_ID"
3. Create a Resource Group:
- name: Create a resource group azure_rm_resourcegroup: name: my_resource_group location: westus
4. Create a Virtual Machine:
- name: Create a virtual machine azure_rm_virtualmachine: resource_group: my_resource_group name: my_vm vm_size: Standard_DS1_v2 admin_username: azureuser admin_password: Password123! image: offer: UbuntuServer publisher: Canonical sku:
This cheatsheet provides a basic overview of Ansible tasks to deploy resources in AWS and Azure. Remember to adjust the parameters and values according to your specific requirements. For more advanced use cases and additional modules, consult the Ansible documentation and the respective cloud provider’s documentation. Please note that the commands and examples provided assume that you have Ansible installed and configured on your system.
Kubernetes Cheatsheet
Category | Command | Description |
---|---|---|
Cluster Management | kubectl cluster-info | Display information about the Kubernetes cluster. |
kubectl get nodes | List all the nodes in the cluster. | |
kubectl describe node | Get detailed information about a specific node. | |
Pods | kubectl get pods | List all pods in the current namespace. |
kubectl get pods –all-namespaces | List all pods in all namespaces. | |
kubectl describe pod | Get detailed information about a specific pod. | |
kubectl logs | Retrieve the logs of a specific pod. | |
Deployments | kubectl get deployments | List all deployments in the current namespace. |
kubectl describe deployment | Get detailed information about a specific deployment. | |
kubectl scale deployment –replicas= | Scale the number of replicas for a deployment. | |
kubectl rollout status deployment | Check the status of a deployment rollout. | |
Services | kubectl get services | List all services in the current namespace. |
kubectl describe service | Get detailed information about a specific service. | |
kubectl port-forward : | Forward a local port to a port on a specific pod. | |
kubectl expose deployment –type=LoadBalancer –port= | Expose a deployment as a LoadBalancer service. | |
Namespaces | kubectl get namespaces | List all namespaces in the cluster. |
kubectl create namespace | Create a new namespace. | |
kubectl delete namespace | Delete a namespace and all its resources. | |
Configmaps and Secrets | kubectl get configmaps | List all configmaps in the current namespace. |
kubectl get secrets | List all secrets in the current namespace. | |
kubectl describe configmap | Get detailed information about a specific configmap. | |
kubectl describe secret | Get detailed information about a specific secret. | |
Persistent Volumes and Persistent Volume Claims | kubectl get pv | List all persistent volumes in the cluster. |
kubectl get pvc | List all persistent volume claims in the current namespace. | |
kubectl describe pv | Get detailed information about a specific persistent volume. | |
kubectl describe pvc | Get detailed information about a specific persistent volume claim. | |
Other Useful Commands | kubectl apply -f | Create or update resources in a Kubernetes cluster using a YAML or JSON file. |
kubectl delete | Delete a specific resource. | |
kubectl explain | Get information about the fields in a specific resource type. | |
kubectl create -f | Create resources in a Kubernetes cluster using a YAML or JSON file. |
Docker Cheatsheet
Docker Basics:
1. Images:
- docker images: List all available Docker images.
- docker pull : Download an image from a registry.
- docker build -t : Build an image from a Dockerfile.
- docker rmi : Remove a Docker image.
- docker run : Run a Docker image.
2. Containers:
- docker ps: List all running containers.
- docker ps -a: List all containers (including stopped ones).
- docker start : Start a stopped container.
- docker stop : Stop a running container.
- docker restart : Restart a container.
- docker rm : Remove a container.
- docker exec -it : Run a command in a running container.
3. Registry and Repository:
- docker login: Log in to a Docker registry.
- docker logout: Log out from a Docker registry.
- docker push : Push an image to a registry.
- docker tag : Tag an image with a new name.
4. Volumes:
- docker volume create : Create a named volume.
- docker volume ls: List all Docker volumes.
- docker volume rm : Remove a Docker volume.
- docker run -v : : Mount a host directory as a volume in the container.
5. Networking:
- docker network create : Create a Docker network.
- docker network ls: List all Docker networks.
- docker network inspect : Inspect a Docker network.
- docker network connect : Connect a container to a network.
- docker network disconnect : Disconnect a container from a network.
6. Docker Compose:
- docker-compose up: Start containers defined in a Docker Compose file.
- docker-compose down: Stop and remove containers defined in a Docker Compose file.
- docker-compose build: Build or rebuild services defined in a Docker Compose file.
- docker-compose logs: View output logs of containers defined in a Docker Compose file.
7. Docker Swarm:
- docker swarm init: Initialize a Docker swarm.
- docker swarm join --token : Join a Docker swarm as a worker node.
- docker swarm join-token manager: Get the token to join a swarm as a manager.
- docker node ls: List all nodes in the swarm.
- docker service create --name : Create a service in the swarm.
- docker service ls: List all services in the swarm.
Dockerfile Directives:
- FROM : Set the base image for subsequent instructions.
- RUN : Execute a command in the image during build.
- COPY : Copy files or directories from the host to the image.
- ADD : Copy files or directories from the host to the image (supports URL and extraction).
- WORKDIR : Set the working directory for subsequent instructions.
- ENV =: Set an environment variable in the image.
- EXPOSE : Expose a port for the container at runtime.
- CMD : Set the default command for the container.
- ENTRYPOINT : Configure a container to run as an executable.
Database Cheatsheet
MySQL Cheatsheet:
- Connect to MySQL Server:
mysql -u username -p
- Create a Database:
CREATE DATABASE database_name;
- Use a Database:
USE database_name;
- Create a Table:
CREATE TABLE table_name ( column1 datatype, column2 datatype, ... );
- Insert Data into a Table:
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
- Select Data from a Table:
SELECT column1, column2, ... FROM table_name WHERE condition;
- Update Data in a Table:
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
- Delete Data from a Table:
DELETE FROM table_name WHERE condition;
- Join Tables:
SELECT columns FROM table1 JOIN table2 ON condition;
- Create an Index:
CREATE INDEX index_name ON table_name (column);
- Backup and Restore a Database:
mysqldump -u username -p database_name > backup.sql mysql -u username -p database_name < backup.sql
PostgreSQL Cheatsheet:
- Connect to PostgreSQL Server:
psql -U username -d database_name
- Create a Database:
CREATE DATABASE database_name;
- Use a Database:
\c database_name;
- Create a Table:
CREATE TABLE table_name ( column1 datatype, column2 datatype, ... );
- Insert Data into a Table:
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
- Select Data from a Table:
SELECT column1, column2, ... FROM table_name WHERE condition;
- Update Data in a Table:
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
- Delete Data from a Table:
DELETE FROM table_name WHERE condition;
- Join Tables:
SELECT columns FROM table1 JOIN table2 ON condition;
- Create an Index:
CREATE INDEX index_name ON table_name (column);
- Backup and Restore a Database:
pg_dump -U username -d database_name > backup.sql psql -U username -d database_name < backup.sql
MongoDB Cheatsheet:
- Connect to MongoDB Server:
mongo --host hostname --port port_number --username username --password password
- Create a Database:
use database_name
- Create a Collection:
db.createCollection("collection_name")
- Insert a Document into a Collection:
db.collection_name.insertOne({ key: value, ... })
- Insert Multiple Documents into a Collection:
db.collection_name.insertMany([{ key: value, ... }, { key: value, ... }])
- Find Documents in a Collection:
db.collection_name.find({ key: value, ... })
- Update a Document in a Collection:
db.collection_name.updateOne({ key: value, ... }, { $set: { key: value, ... } })
- Update Multiple Documents in a Collection:
db.collection_name.updateMany({ key: value, ... }, { $set: { key: value, ... } })
- Delete a Document from a Collection:
db.collection_name.deleteOne({ key: value, ... })
- Delete Multiple Documents from a Collection:
db.collection_name.deleteMany({ key: value, ... })
- Aggregate Documents in a Collection:
db.collection_name.aggregate([{ $group: { _id: "$key", count: { $sum: 1 } } }])
- Create an Index:
db.collection_name.createIndex({ key: 1 })
Microsoft SQL Cheatsheet:
- Connect to SQL Server:
sqlcmd -S server_name -U username -P password
- Create a Database:
CREATE DATABASE database_name;
- Use a Database:
USE database_name;
- Create a Table:
CREATE TABLE table_name ( column1 datatype, column2 datatype, ... );
- Insert Data into a Table:
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
- Select Data from a Table:
SELECT column1, column2, ... FROM table_name WHERE condition;
- Update Data in a Table:
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
- Delete Data from a Table:
DELETE FROM table_name WHERE condition;
- Join Tables:
SELECT columns FROM table1 JOIN table2 ON condition;
- Create an Index:
CREATE INDEX index_name ON table_name (column);
- Backup and Restore a Database:
BACKUP DATABASE database_name TO disk='backup_file_path'; RESTORE DATABASE database_name FROM disk='backup_file_path';
Bash Scripting Cheatsheet
1. Shebang (Interpreter Directive)
#!/bin/bash
2. Variables
variable_name=value
3. Comments
# This is a comment
4. Command Execution
command_name arg1 arg2
5. I/O Redirection
command > output_file command >> output_file command < input_file command 2> error_file command1 | command2
6. Arithmetic Operations
result=$((num1 + num2)) result=$(expr $num1 + $num2) let result=num1+num2
7. Conditional Statements
if condition then # code to execute if condition is true else # code to execute if condition is false fi
8. Loops
For Loop:
for variable in list do # code to execute done
While Loop:
while condition do # code to execute done
Until Loop:
until condition do # code to execute done
9. Functions
function_name() { # code to execute }
10. Command-Line Arguments
$0, $1, $2, ..., $9: Represents script name and command-line arguments. $#: Number of command-line arguments passed. $@: All command-line arguments as separate strings. $*: All command-line arguments as a single string.
11. Conditional Expressions
Comparison Operators:
-eq: Equal to -ne: Not equal to -gt: Greater than -lt: Less than -ge: Greater than or equal to -le: Less than or equal to
String Operators:
=: Equal to !=: Not equal to -z: Empty string -n: Non-empty string
12. String Manipulation
Concatenation:
result=$string1$string2 result=${string1}${string2}
Substring Extraction:
substring=${string:start:length}
String Length:
length=${#string}
String Replacement:
new_string=${string/substring/replacement} new_string=${string//substring/replacement}
13. File Operations
File Existence:
if [ -e filename ]; then # code to execute if the file exists fi
File Permissions:
chmod permission filename
14. Exit Status
$?: Represents the exit status of the last command executed.