This project defines an AWS web server infrastructure setup using Terraform. The configuration includes an EC2 instance running Ubuntu, with a security group allowing HTTP, HTTPS, and SSH access. Additionally, the instance is configured to install and run Nginx automatically.
- Terraform: Ensure you have Terraform version
1.10.3
or later installed. - AWS CLI: Install and configure the AWS CLI with appropriate credentials.
- SSH Key Pair:
- A public key located at
~/.ssh/id_rsa.pub
. - A private key located at
~/.ssh/id_rsa_tf
.
- A public key located at
- Graphviz (optional): For generating dependency graphs with
terraform graph
.
- AMI: Fetches the latest Ubuntu AMI based on provided filters.
- EC2 Instance:
- Instance type:
t2.micro
- Ubuntu 24.04
- Automatically installs and configures Nginx.
- Instance type:
- Key Pair: Adds an existing SSH public key to AWS for secure access or generate the new one.
- Security Group: Configures inbound rules for HTTP (port 80), HTTPS (port 443), and SSH (port 22).
- Ingress and Egress Rules:
- Ingress rules allow specific traffic as defined in
security_group_rules
. - Egress rule allows all outbound traffic.
- Ingress rules allow specific traffic as defined in
Variable | Type | Description |
---|---|---|
instance_detail |
Map | Details about the instance configuration. |
instance_kp |
Map | Details about the key pair. |
instance_security_group |
Map | Details about the security group. |
security_group_rules |
Map | Ingress rules for the security group. |
public_key_path |
String | Path to the SSH public key. |
private_key_path |
String | Path to the SSH private key. |
-
Initialize Terraform:
terraform init
-
Validate Configuration Syntax:
terraform validate
-
Plan the Infrastructure:
terraform plan
-
Apply the Configuration:
terraform apply --auto-approve
-
Check Logs: Logs for the instance creation are written to
instance.log
. -
Access the EC2 Instance: Use the private key to SSH into the instance:
ssh -i ~/.ssh/id_rsa ubuntu@<instance_public_ip>
-
Generate Dependency Graph:
terraform graph | dot -Tpdf > graph.pdf
Requires Graphviz to be installed.
-
Destroy Infrastructure: To delete all created resources:
terraform destroy --auto-approve
- Ensure your AWS credentials are properly configured using the AWS CLI or environment variables.
- Adjust
security_group_rules
as needed to limit access based on your requirements. - Modify
instance_detail
to select the desired AMI, instance type, and region.