Top 10 Terraform & AWS Commands
Terraform is a powerful Infrastructure as Code (IaC) tool that allows you to define, provision, and manage infrastructure resources using declarative configuration files. It supports multiple cloud providers like AWS, Azure, and Google Cloud, making it a go-to solution for consistent, repeatable, and scalable infrastructure management.
AWS (Amazon Web Services), the leading cloud platform, offers a broad range of services, including compute power, storage, and databases. Terraform and AWS together provide an efficient way to automate cloud infrastructure, ensuring that deployments are reliable, consistent, and traceable.
In this guide, we’ll explore beginner-friendly Terraform commands and AWS CLI commands to help you get started with cloud infrastructure automation and management
Terraform Commands for Beginners
1. Initialize a Terraform Project
- Prepares your working directory by downloading the required provider plugins and setting up Terraform configuration files.
terraform initInitializing provider plugins... Terraform has been successfully initialized!
2. Format Terraform Configuration Files
- Formats
.tf
files to ensure consistent style and readability.
terraform fmt
3. Validate a Terraform Configuration
- Checks if your Terraform configuration is syntactically and logically valid.
terraform validateSuccess! The configuration is valid.
4. Plan Infrastructure Changes
- Generates an execution plan showing what Terraform will do when you apply the configuration.
terraform plan
aws_instance.my-instance ami: "ami-12345678" instance_type: "t2.micro"
5. Apply Changes to Create/Update Resources
- Executes the planned changes to provision or update the infrastructure.
terraform apply
6. Destroy Resources
- Deletes all the resources managed by your Terraform configuration.
terraform destroy
7. Show Current State
- Displays the current state of the infrastructure as managed by Terraform.
terraform show
8. Get Outputs
- Displays the output values defined in the configuration after applying it.
terraform output
9. View State
- Lists all the resources in the current Terraform state.
terraform state list
10. Import Existing Resources
- Brings an existing resource under Terraform management.
terraform import <resource_type>.<resource_name> <resource_id>
AWS CLI Commands for Beginners
1. Configure AWS CLI
- Sets up AWS CLI with your Access Key, Secret Key, region, and output format.
aws configure
2. List All S3 Buckets
- Displays all S3 buckets in your AWS account.
aws s3 ls
3. Create an S3 Bucket
- Creates a new S3 bucket named
my-new-bucket
.
aws s3 mb s3://my-new-bucket
4. Upload a File to S3
- Uploads a local file (
file.txt
) to the specified S3 bucket.
aws s3 cp file.txt s3://my-new-bucket/
5. Launch an EC2 Instance
- Launches an EC2 instance with the specified image and instance type.
aws ec2 run-instances --image-id ami-12345678 --count 1 --instance-type t2.micro
6. List Running EC2 Instances
- Displays a table of running EC2 instances and their states.
aws ec2 describe-instances --query "Reservations[*].Instances[*].[InstanceId,State.Name]" --output table
7. Stop an EC2 Instance
- Stops the specified EC2 instance.
aws ec2 stop-instances --instance-ids i-1234567890abcdef0
8. Check Account Billing
- Retrieves a cost and usage report for a specified time period.
aws ce get-cost-and-usage --time-period Start=2024-01-01,End=2024-01-31 --granularity MONTHLY --metrics BLENDED_COST UNBLENDED_COST USAGE_QUANTITY --output table
9. Create a Key Pair
- Creates an SSH key pair for EC2 access and saves the private key.
aws ec2 create-key-pair --key-name MyKeyPair
10. Terminate an EC2 Instance
- Permanently deletes the specified EC2 instance.
aws ec2 terminate-instances --instance-ids i-1234567890abcdef0
Conclusion
Terraform and AWS provide an exceptional combination for automating and managing cloud infrastructure. Terraform’s declarative configurations and AWS CLI’s direct access to resources empower users to create, modify, and maintain infrastructure with ease. The commands highlighted above are perfect for beginners to get started, helping you navigate infrastructure tasks confidently. As you gain experience, you can delve into more advanced features like Terraform modules, AWS IAM policies, and multi-cloud deployments to unlock the full potential of these tools.