{"id":28485320,"url":"https://github.com/gurpreet2828/terraform-docker-aws","last_synced_at":"2026-04-05T23:31:27.803Z","repository":{"id":288668787,"uuid":"968737276","full_name":"gurpreet2828/terraform-docker-aws","owner":"gurpreet2828","description":"This repository showcases how to automate infrastructure provisioning and Docker container deployment using Terraform. It demonstrates how to create virtual machines (e.g., AWS EC2) and use Terraform to install and run Docker containers, enabling a fully automated, scalable infrastructure setup.","archived":false,"fork":false,"pushed_at":"2025-05-25T01:07:55.000Z","size":2431,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-08T00:09:47.702Z","etag":null,"topics":["automation","aws","aws-cli","aws-ec2","devops","docker","docker-compose","docker-container","dockerfile","iac","infrastructure-as-code","terraform","ubuntu","vscode","yml"],"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/gurpreet2828.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":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-04-18T16:30:33.000Z","updated_at":"2025-05-25T01:07:59.000Z","dependencies_parsed_at":"2025-05-25T01:43:43.387Z","dependency_job_id":"61fbac4d-f23a-479e-a9a4-054e69f2475c","html_url":"https://github.com/gurpreet2828/terraform-docker-aws","commit_stats":null,"previous_names":["gurpreet2828/terraform-docker","gurpreet2828/terraform-docker-aws"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/gurpreet2828/terraform-docker-aws","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gurpreet2828%2Fterraform-docker-aws","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gurpreet2828%2Fterraform-docker-aws/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gurpreet2828%2Fterraform-docker-aws/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gurpreet2828%2Fterraform-docker-aws/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gurpreet2828","download_url":"https://codeload.github.com/gurpreet2828/terraform-docker-aws/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gurpreet2828%2Fterraform-docker-aws/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263074456,"owners_count":23409767,"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":["automation","aws","aws-cli","aws-ec2","devops","docker","docker-compose","docker-container","dockerfile","iac","infrastructure-as-code","terraform","ubuntu","vscode","yml"],"created_at":"2025-06-08T00:09:46.351Z","updated_at":"2025-12-30T22:44:28.632Z","avatar_url":"https://github.com/gurpreet2828.png","language":"HCL","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Deploy Ghost Blog and MySQL with Docker and Terraform on AWS infrastructure\n\n## Summary\n\nThis project involves deploying a Ghost Blog with a MySQL database using Docker, Docker Compose and Terraform on an AWS EC2 instance running Ubuntu.\n\nThe infrastructure is provisioned through Terraform, which automates the setup of the EC2 instance and SSH key configuration. Docker and Docker Compose are used to containerize and orchestrate the Ghost and MySQL services, enabling efficient application management. The AWS CLI is utilized for secure interaction with AWS services, while tools like VS Code and SCP assist in code transfer and editing.\n\nDuring implementation, several issues were encountered and resolved, including AWS CLI configuration problems, SSH permission errors, Git initialization issues, and Docker service failures. Troubleshooting involved checking file permissions, correcting SSH key paths, installing necessary packages, and validating Docker Compose syntax. Successful deployment was verified through Terraform commands and by accessing the blog via the EC2 public IP. Key Docker commands were used to manage containers, ensuring the blog and database were running as expected.\n\n## Setup Overview\n\nThe document outlines the steps to deploy a Ghost Blog and MySQL using Docker, Docker Compose and Terraform on an AWS EC2 instance. Key tools include:\n\n**- Terraform:** For infrastructure provisioning.\n\n**- Docker/Docker Compose:** For application containerization.\n\n**- AWS CLI:** For interaction with AWS services.\n\n**- vsCode:** Code Editor\n\n**- Ubuntu Linux environment:**\n\n**- AWS account with access keys:**\n\n## Troubleshooting and errors which I faced during this project implementation\n\n**1.AWS CLI Configuration Problems:** If aws configure fails, verify that your AWS Access Key and Secret Key are correct.\n\n- Run aws configure and input your credentials\n\n- Confirm setup with aws sts get-caller-identity\n\n**2.Permission Denied During SSH:** Ensure you're using the correct private key and the correct username (ec2-user).And ensure you're using the correct private key with the -i option.\n\n**Example:**\nssh -i /path/to/your-key.pem ec2-user@98.80.171.157\n\n```bash\nssh -i /root/.ssh/docker ec2-user@98.80.171.157\n```\n\n### **3. Permissions Issues during git init**\n\nIssue: Git init shows .git: Permission denied\n\nFix Permission\n\n- sudo chown -R administrator:administrator/home/administrator/Terraform-Docker\n\n- sudo chmod u+rwx /home/administrator/Terraform-Docker\n\n**4.Terraform Plan Errors:** Check if the paths to private/public keys are correctly defined in the variables.tf file (under modules -compute - variables.tf)\n\nIssue: Invalid SSH key path error like `\"C:\\\\Users\\\\wessa\\\\.ssh\\\\id_rsa.pub\"`\n\nFix: Update variables.tf to point to a valid Unix-style path, e.g. `/root/.ssh/docker.pub`\n\n### **5.Docker not running**\n\n- sudo systemctl status docker\n\n- sudo systemctl start docker\n\n- sudo systemctl enable docker\n\n- docker run docker.io/hello-world: Run the hello-world container image to verify installation\n\n### **6.Ghost Blog Not Accessible via Public IP**\n\nIssue: Blog not loading in browser\n\nFix:\n\n**Check running containers:** `docker ps`\n\n**Check not running containers:** `docker ps -a`\n\n**Restart container:** `docker start ghost-blog`\n\n**View logs for errors:** `docker logs ghost-blog`\n\n**Restart services:** `docker-compose up -d`\n\n### **7. Issue: Errors in docker-compose.yml syntax**\n\nFix:\n\nRun docker compose config to validate\n\nCheck for YAML syntax issues like incorrect indentation, colons, or missing values\n\n**8.Access denied to files**\nAfter transferring the files from windows to linux you need to give following permission so that we can save and edit files\n\n```bash\nsudo chown -R administrator:administrator /home/administrator/Terraform-Docker\n\nsudo chmod -R u+rwX /home/administrator/Terraform-Docker\n```\n\n## Step1: Transfer Terraform and yml files\n\nUse scp to transfer your Terraform and docker files from your local machine to your Ubuntu instance:\n\nNote: Transfer the terraform and docker code from local machine to Linux by running following command on cmd\n\n```bash\nscp -r -v \"C:\\Users\\Gurpreet\\OneDrive\\Desktop\\York Univ\\Assignments\\Assignment 4 - Docker\\Terraform-Docker\" administrator@10.0.0.83:/home/administrator\n```\n\nEnter your password of ubuntu user\n\n![Image1](https://github.com/gurpreet2828/Terraform-Docker/blob/fe668ab95c2b25c233b90da636e62a15a3cf9b05/Images/Image1.png)\n\nIt shows the following screen after transfer\n\n![Image2](https://github.com/gurpreet2828/Terraform-Docker/blob/f68bff62e5646f98e8d174bfec18c9d1eac09359/Images/Image2.png)\n\n**Make git repo:**\n\nadministrator@Cloudinfotech-Server:~/Terraform-Docker$ `git init`\n\n/home/administrator/Terraform-Docker/. git: Permission denied\n\n![Image3](https://github.com/gurpreet2828/Terraform-Docker/blob/f68bff62e5646f98e8d174bfec18c9d1eac09359/Images/Image3.png)\n\nFix Permission\n\n```bash\nsudo chown -R administrator:administrator /home/administrator/Terraform-Docker\n\nsudo chmod u+rwx /home/administrator/Terraform-Docker\n```\n\n## Step2: Install Terraform on Ubuntu\n\n### **1. Update and install dependencies**\n\n```bash\nsudo apt update \u0026\u0026 sudo apt install -y gnupg software-properties-common curl\n```\n\n### **2.Add the HashiCorp GPG key**\n\n```bash\ncurl -fsSL https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg\n```\n\n### **3.Add the HashiCorp repo**\n\n```bash\necho \"deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main\" | \\\n\nsudo tee /etc/apt/sources.list.d/hashicorp.list\n```\n\n### **4.Update and install Terraform**\n\n```bash\nsudo apt update\n\nsudo apt install terraform -y\n```\n\n### **5.Verify installation**\n\n```bash\nterraform -v\n```\n\n![Image5](https://github.com/gurpreet2828/Terraform-Docker/blob/f68bff62e5646f98e8d174bfec18c9d1eac09359/Images/Image5.png)\n\n## Provisioning Infrastructure with Terraform\n\n### Step1: `Terraform init`\n\n![Image6](https://github.com/gurpreet2828/Terraform-Docker/blob/f68bff62e5646f98e8d174bfec18c9d1eac09359/Images/Image6.png)\n\n### Step 2: `Terraform fmt`\n\n### Step 3: `Terraform validate`\n\n![Image7](https://github.com/gurpreet2828/Terraform-Docker/blob/f68bff62e5646f98e8d174bfec18c9d1eac09359/Images/Image7.png)\n\n### Step 4: `Terraform plan`\n\nAfter running the terraform plan if you see the above error than you need to configure aws account as it found no aws credentials\n\n![Image8](https://github.com/gurpreet2828/Terraform-Docker/blob/f68bff62e5646f98e8d174bfec18c9d1eac09359/Images/Image8.png\n)\n\n### AWSCLI Install\n\nTo install the AWS CLI, run the following command\n\n```bash\ncurl \"https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip\" -o \"awscliv2.zip\"\n\nunzip awscliv2.zip\n\nsudo ./aws/install\n```\n\nRun the following command to check if AWS CLI is installed correctly:\n\n```bash\naws –version\n```\n\nYou see the following output\n\n![Image9](https://github.com/gurpreet2828/Terraform-Docker/blob/f68bff62e5646f98e8d174bfec18c9d1eac09359/Images/Image9.png)\n\n## Step 3: Create AWS account\n\nAfter Creating\n\nClick on account name - Select Security Credentials\n\n![Image27](https://github.com/gurpreet2828/Terraform-Docker/blob/fe668ab95c2b25c233b90da636e62a15a3cf9b05/Images/Image27.png)\n\nClick Create access key.\n\n![Image28](https://github.com/gurpreet2828/Terraform-Docker/blob/fe668ab95c2b25c233b90da636e62a15a3cf9b05/Images/Image28.png)\n\nNote: Download the key file or copy the Access Key ID \u0026 Secret Access Key (Secret Key is shown only once!).\n\nAfter install and creating AWS account configure the AWS\n\n**Configure AWS CLI with the New Access Key:**\n\n```shell\naws configure\n```\n\nIt will prompt you for:\n\n**1.AWS Access Key ID:** Your access key from AWS IAM.\n\n**2.AWS Secret Access Key:** Your secret key from AWS IAM.\n\n**3.Default region name:** (e.g., us-east-1, us-west-2).\n\n**4.Default output format:** (json, table, text — default is json).\n\nEnter access key and secret key which you will get from aws account\n\nCheck credentials added to aws configure correctly\n\n```bash\naws sts get-caller-identity\n```\n\nIf your AWS CLI is properly configured, you'll see a response like this:\n\n![Image10](https://github.com/gurpreet2828/Terraform-Docker/blob/f68bff62e5646f98e8d174bfec18c9d1eac09359/Images/Image10.png)\n\n**Run `Terraform plan`**\n\nIf it shows the following error\n\n![Image11](https://github.com/gurpreet2828/Terraform-Docker/blob/f68bff62e5646f98e8d174bfec18c9d1eac09359/Images/Image11.png)\n\n```bash\nError: Invalid function argument\n\n│   on modules/compute/main.tf line 17, in resource \"aws_key_pair\" \"aws-key\":\n│   17:   public_key = file(var.ssh_key_public)\n│     ├────────────────\n│     │ while calling file(path)\n│     │ var.ssh_key_public is \"C:\\\\Users\\\\wessa\\\\.ssh\\\\id_rsa.pub\"\n```\n\nThen in this case you must update the location of public and private keys under modules -compute - variables.tf\n\nAs shown bellow in image\n\n![Image12](https://github.com/gurpreet2828/Terraform-Docker/blob/f68bff62e5646f98e8d174bfec18c9d1eac09359/Images/Image12.png)\n\n### 5:`Terraform apply`\n\nProvision terraform managed infrastructure. You must confirm by trying yes if you would like to continue and perform the actions described to provision your infrastructure resources\n\nIf everything works fine at end you will see the public Ip\n\n![Image13](https://github.com/gurpreet2828/Terraform-Docker/blob/f68bff62e5646f98e8d174bfec18c9d1eac09359/Images/Image13.png)\n\n## Step 4: Connect to EC2 instance\n\n```shell\nssh ec2-user@ 98.80.171.157\n```\n\n![Image14](https://github.com/gurpreet2828/Terraform-Docker/blob/f68bff62e5646f98e8d174bfec18c9d1eac09359/Images/Image14.png\n)\nEnsure you're using the correct private key and the correct username (ec2-user) and ensure you're using the correct private key with the -i option.\n\nExample\n\n`ssh -i /path/to/your-key.pem ec2-user@98.80.171.157`\n\n```bash\nssh -i /root/.ssh/docker ec2-user@98.80.171.157\n```\n\nyou will see ec2 instance connected remotely with your ubuntu instance\n\n![Image15](https://github.com/gurpreet2828/Terraform-Docker/blob/f68bff62e5646f98e8d174bfec18c9d1eac09359/Images/Image15.png)\n\n### Docker Install on EC2-instance\n\n```bash\nyum install docker -y\n```\n\n```bash\nsystemctl status docker\n```\n\n```bash\nsystemctl start docker\n```\n\n```bash\nsystemctl enable docker\n```\n\n```bash\ndocker run docker.io/hello-world\n```\n\nRun the hello-world container image to verify installation:\n\n![Image18](https://github.com/gurpreet2828/Terraform-Docker/blob/f68bff62e5646f98e8d174bfec18c9d1eac09359/Images/Image18.png)\n\n## Step 5: Docker Compose\n\n```bash\nInstall docker compose\n```\n\n```bash\nsudo yum install -y py-pip, python3-dev, libffi-dev, openssl-dev, gcc, libc-dev, rust, cargo, make\n```\n\n```bash\nsudo curl -L \"https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)\" -o /usr/local/bin/docker-compose\n\nsudo chmod +x /usr/local/bin/docker-compose\n```\n\n```bash\nCheck docker compose install\n```\n\n**docker compose version:**\n\n![Image19](https://github.com/gurpreet2828/Terraform-Docker/blob/f68bff62e5646f98e8d174bfec18c9d1eac09359/Images/Image19.png)\n\n**Create Docker Compose File:**\n\n```bash\nvi docker-compose.yml\n```\n\ncopy and paste yml file\n\nfor check it save properly\n\nrun\n\n```bash\ncat docker-compose.yml\n```\n\n![Image20](https://github.com/gurpreet2828/Terraform-Docker/blob/f68bff62e5646f98e8d174bfec18c9d1eac09359/Images/Image20.png)\n\nIf you're working with Docker Compose, you can validate your docker-compose.yml file by running:\n\n```bash\ndocker compose config\n```\n\nif it shows some error as follows than there is syntax error in your yml file\n\n![Image21](https://github.com/gurpreet2828/Terraform-Docker/blob/f68bff62e5646f98e8d174bfec18c9d1eac09359/Images/Image21.png)\n\nCorrect the syntax and correct error of your yml file again run\n\n```bash\nDocker compose config\n```\n\nIf it shows the full code than your yml file works fine\n\nStart up the Docker Compose service:\n\nRun\n\n```bash\ndocker compose up -d\n```\n\nExecute a compose up and use the detached flag to run the container in the background:\n\n![Image22](https://github.com/gurpreet2828/Terraform-Docker/blob/f68bff62e5646f98e8d174bfec18c9d1eac09359/Images/Image22.png)\n\nVerify that your app is up and running using the public IP address of your EC2 instance\n\n![Image23](https://github.com/gurpreet2828/Terraform-Docker/blob/f68bff62e5646f98e8d174bfec18c9d1eac09359/Images/Image23.png)\n\n**Some important command of docker:**\n\n```bash\ndocker info\n```\n\n```bash\n**docker ps:**  shows only running containers\n```\n\n```bash\n**Docker ps -a**\n```\n\nIt shows all the containers\n\n![Image25](https://github.com/gurpreet2828/Terraform-Docker/blob/f68bff62e5646f98e8d174bfec18c9d1eac09359/Images/Image25.png)\n**Docker stop \u003ccontainer_name\u003e:**\n\nTo stop the container\n\n```bash\n**Docker rm \u003ccontainer_name\u003e:**\n```\n\nTo delete the container\n\n**Docker start \u003ccontainer_name\u003e:**\n\n to start the container\n\n```bash\n**Docker search \u003cimage_name\u003e**\n```\n\nEx: docker search ubuntu: it search all the images regarding ubuntu present on ubuntu hub\n\n![Image26](https://github.com/gurpreet2828/Terraform-Docker/blob/fe668ab95c2b25c233b90da636e62a15a3cf9b05/Images/Image26.png)\n\n```bash\n**Docker exec -it \u003ccontainer_name\u003e**\n```\n\nTo go inside the running docker\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgurpreet2828%2Fterraform-docker-aws","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgurpreet2828%2Fterraform-docker-aws","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgurpreet2828%2Fterraform-docker-aws/lists"}