This repository is dedicated to learning Terraform and exploring its capabilities in managing AWS resources. The project focuses on provisioning and configuring AWS services such as S3, VPC, security groups, EC2 instances, and utilizing Terraform function expressions to optimize and manage infrastructure as code.
- Prerequisites
- Getting Started
- AWS Resources
- Terraform Function Expressions
- Usage
- Contributing
- License
- Acknowledgments
Before you begin, ensure you have the following installed:
- Terraform (version 1.x or higher)
- AWS CLI configured with your AWS credentials
- Access to an AWS account
To get started with this project:
-
Clone the repository:
git clone https://github.com/yourusername/terraform-aws-learning.git cd terraform-aws-learning
-
Initialize Terraform:
terraform init
-
Validate the configuration files:
terraform validate
-
Plan the changes to be made:
terraform plan
-
Apply the changes to provision the resources:
terraform apply
This project includes an S3 bucket for storing files and data.
resource "aws_s3_bucket" "my_bucket" {
bucket = "my-unique-bucket-name"
acl = "private"
}
A Virtual Private Cloud (VPC) is created to isolate the resources.
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "my_vpc"
}
}
Security groups are defined to control inbound and outbound traffic for EC2 instances.
resource "aws_security_group" "my_security_group" {
name = "my_security_group"
vpc_id = aws_vpc.my_vpc.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
EC2 instances are launched within the defined VPC and security group.
resource "aws_instance" "my_instance" {
ami = "ami-0c55b159cbfafe01e" # Replace with a valid AMI ID
instance_type = "t2.micro"
subnet_id = aws_subnet.my_subnet.id
vpc_security_group_ids = [aws_security_group.my_security_group.id]
tags = {
Name = "my_instance"
}
}
Terraform function expressions are used to manipulate and transform data within configurations. Here are a few examples:
-
Joining a List:
variable "tags" { type = list(string) default = ["Environment=Dev", "Project=Learning"] } output "tag_string" { value = join(", ", var.tags) }
-
Conditional Expressions:
resource "aws_instance" "my_instance" { ami = var.instance_type == "t2.micro" ? "ami-0c55b159cbfafe01e" : "ami-abc123" instance_type = var.instance_type }
To modify or add resources:
- Update the respective
.tf
files in the repository. - Re-run
terraform plan
to see the changes. - Apply the changes using
terraform apply
.
Contributions are welcome! To contribute to this project:
- Fork the repository.
- Create a new branch:
git checkout -b feature/your-feature
- Make your changes and commit them:
git commit -m "Add some feature"
- Push to the branch:
git push origin feature/your-feature
- Open a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.
- Special thanks to the Terraform community and AWS documentation for their resources and support.
- Acknowledgment to contributors and libraries that aided in the development of this project.
Feel free to customize this template according to your project's specifics and expand on sections as you continue your learning journey with Terraform and AWS!