{"id":25436721,"url":"https://github.com/shivamchavan01/terraform-nginx-ec2-deployment","last_synced_at":"2026-05-05T13:42:22.308Z","repository":{"id":277390572,"uuid":"932274828","full_name":"ShivamChavan01/Terraform-Nginx-EC2-Deployment","owner":"ShivamChavan01","description":"This Terraform project provisions an AWS infrastructure using LocalStack for local AWS service emulation. It sets up a Virtual Private Cloud (VPC) with both public and private subnets, configures networking components like an Internet Gateway and Route Table, and deploys an EC2 instance with Nginx installed.","archived":false,"fork":false,"pushed_at":"2025-03-05T19:45:04.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-05T20:51:16.856Z","etag":null,"topics":["aws","aws-ec2","nginx-server","terraform"],"latest_commit_sha":null,"homepage":"","language":"HCL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ShivamChavan01.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"securitygroup.tf","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-02-13T16:47:10.000Z","updated_at":"2025-03-05T19:45:08.000Z","dependencies_parsed_at":"2025-03-21T02:31:32.362Z","dependency_job_id":null,"html_url":"https://github.com/ShivamChavan01/Terraform-Nginx-EC2-Deployment","commit_stats":null,"previous_names":["shivamchavan01/terraform-nginx-ec2-deployment"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShivamChavan01%2FTerraform-Nginx-EC2-Deployment","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShivamChavan01%2FTerraform-Nginx-EC2-Deployment/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShivamChavan01%2FTerraform-Nginx-EC2-Deployment/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShivamChavan01%2FTerraform-Nginx-EC2-Deployment/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ShivamChavan01","download_url":"https://codeload.github.com/ShivamChavan01/Terraform-Nginx-EC2-Deployment/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254264766,"owners_count":22041794,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","aws-ec2","nginx-server","terraform"],"created_at":"2025-02-17T08:22:11.857Z","updated_at":"2026-05-05T13:42:22.240Z","avatar_url":"https://github.com/ShivamChavan01.png","language":"HCL","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# 🚀 Terraform AWS VPC \u0026 EC2 Setup with Nginx  \n\n\n\n## Overview\nThis Terraform configuration sets up an AWS environment using LocalStack and deploys an EC2 instance running Nginx. The setup includes:\n- **VPC** with public and private subnets.\n- **Internet Gateway** and **Route Table** for external access.\n- **Security Group** to allow HTTP traffic.\n- **EC2 Instance** with user data to install and start Nginx.\n- **Terraform Outputs** for instance public IP and Nginx access URL.\n\n\n  ## 🛠️ Features  \n✅ **Automated AWS Infrastructure** – Deploys a VPC, subnets, and EC2 instance using Terraform.  \n\n✅ **Nginx Installation** – Installs and starts Nginx automatically.  \n\n✅ **Security Group Rules** – Allows HTTP (port 80) access.  \n\n✅ **LocalStack Integration** – Simulates AWS services locally.  \n\n\n## Prerequisites\n- Terraform installed ([Install Terraform](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli))\n- LocalStack installed ([Install LocalStack](https://docs.localstack.cloud/getting-started/installation/))\n- AWS CLI installed ([Install AWS CLI](https://aws.amazon.com/cli/))\n\n## Terraform Configuration\n\n### 1. Provider Configuration\nThe AWS provider is set up to use LocalStack, which simulates AWS services locally.\n```hcl\nterraform {\n  required_providers {\n    aws = {\n      source  = \"hashicorp/aws\"\n      version = \"5.54.1\"\n    }\n  }\n}\n\nprovider \"aws\" {\n  access_key                  = \"LKIAQAAAAAAAIOFPSI36\"  # Dummy values for LocalStack\n  secret_key                  = \"p4VoUAkjIKZKyTAQco/KtX5fRIZjDye/DuxJvo+C\"\n  region                      = \"eu-north-1\"\n  s3_use_path_style           = true\n  skip_credentials_validation = true\n  skip_requesting_account_id  = true\n  skip_metadata_api_check     = true\n\n  endpoints {\n    s3    = \"http://localhost:4566\"\n    ec2   = \"http://localhost:4566\"\n    iam   = \"http://localhost:4566\"\n    sts   = \"http://localhost:4566\"\n    rds   = \"http://localhost:4566\"\n    lambda = \"http://localhost:4566\"\n  }\n}\n```\n\n### 2. VPC Configuration\nCreates a VPC with a CIDR block of `10.0.0.0/16`.\n```hcl\nresource \"aws_vpc\" \"my-vpc\" {\n  cidr_block = \"10.0.0.0/16\"\n  tags = { Name = \"my-vpc\" }\n}\n```\n\n### 3. Subnets\nTwo subnets are created within the VPC:\n```hcl\nresource \"aws_subnet\" \"private_subnet\" {\n  vpc_id     = aws_vpc.my-vpc.id\n  cidr_block = \"10.0.1.0/24\"\n  tags = { Name = \"Private_subnet\" }\n}\n\nresource \"aws_subnet\" \"public_subnet\" {\n  vpc_id     = aws_vpc.my-vpc.id\n  cidr_block = \"10.0.2.0/24\"\n  tags = { Name = \"Public_Subnet\" }\n}\n```\n\n### 4. Internet Gateway\nRequired to allow the EC2 instance to access the internet.\n```hcl\nresource \"aws_internet_gateway\" \"my-igw\" {\n  vpc_id = aws_vpc.my-vpc.id\n  tags = { Name = \"my-igw\" }\n}\n```\n\n### 5. Route Table \u0026 Association\nAssociates the route table with the public subnet for internet access.\n```hcl\nresource \"aws_route_table\" \"my-rt\" {\n  vpc_id = aws_vpc.my-vpc.id\n  route {\n    cidr_block = \"0.0.0.0/0\"\n    gateway_id = aws_internet_gateway.my-igw.id\n  }\n  tags = { Name = \"My Routing Table\" }\n}\n\nresource \"aws_route_table_association\" \"public_sub\" {\n  route_table_id = aws_route_table.my-rt.id\n  subnet_id      = aws_subnet.public_subnet.id\n}\n```\n\n### 6. Security Group\nAllows inbound traffic on port **80 (HTTP)** and all outbound traffic.\n```hcl\nresource \"aws_security_group\" \"nginx-sp\" {\n  vpc_id = aws_vpc.my-vpc.id\n  ingress {\n    from_port   = 80\n    to_port     = 80\n    protocol    = \"tcp\"\n    cidr_blocks = [\"0.0.0.0/0\"]\n  }\n  egress {\n    from_port   = 0\n    to_port     = 0\n    protocol    = \"-1\"\n    cidr_blocks = [\"0.0.0.0/0\"]\n  }\n  tags = { Name = \"Nginx-sp\" }\n}\n```\n\n### 7. EC2 Instance with Nginx Installation\nDeploys an EC2 instance and installs Nginx via `user_data`.\n```hcl\nresource \"aws_instance\" \"my-nginx-server\" {\n  ami                    = \"ami-0df4d205d505c6f42\"  # Update with valid AMI ID\n  instance_type          = \"t3.nano\"\n  subnet_id             = aws_subnet.public_subnet.id\n  vpc_security_group_ids = [aws_security_group.nginx-sp.id]\n  associate_public_ip_address = true\n\n  user_data = \u003c\u003c-EOF\n    #!/bin/bash\n    sudo yum install -y nginx\n    sudo systemctl start nginx\n    sudo systemctl enable nginx\n  EOF\n\n  tags = { Name = \"Nginx-Ec2-Server\" }\n}\n```\n\n### 8. Outputs\nAfter deployment, these outputs provide access details.\n```hcl\noutput \"instance_public_ip\" {\n  description = \"Public IP of the EC2 instance\"\n  value       = aws_instance.my-nginx-server.public_ip\n}\n\noutput \"instance_url\" {\n  description = \"Nginx Server URL\"\n  value       = \"http://${aws_instance.my-nginx-server.public_ip}\"\n}\n```\n\n## Deployment Steps\n\n### **1. Start LocalStack**\n```bash\nlocalstack start -d\n```\n\n### **2. Initialize Terraform**\n```bash\nterraform init\n```\n\n### **3. Apply Terraform Configuration**\n```bash\nterraform apply -auto-approve\n```\n\n### **4. Verify Nginx Server**\n- Copy the public IP from the Terraform output.\n- Open the browser and visit: `http://\u003cPUBLIC_IP\u003e`.\n\n### **5. SSH into the Instance (If Needed)**\n```bash\nssh -i your-key.pem ec2-user@\u003cPUBLIC_IP\u003e\n```\n```bash\nsudo systemctl status nginx\n```\n\n## Notes\n- LocalStack **does not fully support EC2**, so this configuration works only on real AWS.\n- Always check for the latest AMI ID before deploying.\n- Restrict security groups in production for better security.\n\n## Conclusion\nThis Terraform configuration automates the deployment of an AWS EC2 instance with Nginx using LocalStack (for supported resources). If you want real EC2 functionality, you must deploy it on actual AWS.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshivamchavan01%2Fterraform-nginx-ec2-deployment","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshivamchavan01%2Fterraform-nginx-ec2-deployment","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshivamchavan01%2Fterraform-nginx-ec2-deployment/lists"}