Skip to content

Commit d06c08b

Browse files
committed
Readme added for the current project
1 parent 46ab1c4 commit d06c08b

File tree

2 files changed

+62
-127
lines changed

2 files changed

+62
-127
lines changed

.github/workflows/deploy.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,14 @@ jobs:
193193
194194
echo "✅ Printed last 20 lines of logs from verifier EC2"
195195
196+
# # Verify Logs in S3 using AWS CLI
197+
# - name: Verify Logs in S3
198+
# run: |
199+
# echo "📦 Checking for logs in S3 bucket: $S3_BUCKET"
200+
# aws s3 ls s3://$S3_BUCKET/${STAGE}/system/cloud-init.log || { echo "❌ system logs missing"; exit 1; }
201+
# aws s3 ls s3://$S3_BUCKET/${STAGE}/app/my-app.log || { echo "❌ app logs missing"; exit 1; }
202+
# echo "✅ Logs found in S3 bucket"
203+
196204

197205
# Destroy Infrastructure
198206
- name: Destroy Infrastructure

README.md

Lines changed: 54 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# AWS EC2 Auto Deployment with Terraform & Shell Scripts
1+
# AWS EC2 Auto Deployment with Terraform & GitHub Actions
22

3-
This project automates the provisioning of an EC2 instance and the deployment of your application on AWS using Terraform and shell scripts. It supports different environments (Dev, Prod) through configuration files and deployment scripts.
3+
This project automates the provisioning of EC2 instances and the deployment of your application on AWS using **Terraform** and **GitHub Actions**. It supports different environments (Dev, Prod) via configuration files.
44

55
---
66

@@ -9,14 +9,18 @@ This project automates the provisioning of an EC2 instance and the deployment of
99
```
1010
tech_eazy_devops_git-user-9/
1111
├── README.md # Project documentation
12+
├── .gitignore # Lists files to exclude from version control
13+
├── .github/
14+
│ └── workflows/
15+
│ └── deploy.yml # GitHub Actions workflow for deployment
1216
├── terraform/ # Terraform configurations
1317
│ ├── main.tf # Main Terraform configuration file
1418
│ ├── outputs.tf # Defines Terraform outputs (e.g., EC2 public IP)
1519
│ ├── variables.tf # Public variables file for EC2 and other details
1620
│ ├── dev_config.tfvars # Variable values for 'Dev' environment
1721
│ ├── prod_config.tfvars # Variable values for 'Prod' environment
18-
├── scripts/ # Shell scripts for provisioning and deployments
19-
│ ├── deploy.sh # Automates provisioning with Terraform
22+
├── scripts/ # Shell scripts for configuration and log validation
23+
│ ├── deploy.sh # [OBSOLETE] Legacy deployment script (replaced by deploy.yml)
2024
│ ├── dev_script.sh # Dev-specific configuration script for EC2
2125
│ ├── prod_script.sh # Production-specific script for EC2
2226
│ ├── verify_logs.sh # Validates and uploads logs
@@ -25,162 +29,85 @@ tech_eazy_devops_git-user-9/
2529
│ │ └── my-app.log # Main application log
2630
│ └── system/ # Tracks provisioning/system logs
2731
│ └── cloud-init.log # Logs of initialization processes
28-
└── .gitignore # Lists files to exclude from version control
2932
```
3033

3134
---
3235

3336
## ⚙️ **Prerequisites**
3437

35-
Ensure the following tools and resources are configured before deploying:
38+
* **Fork this repository** – You must fork it to your own GitHub account so you can add secrets (you cannot add secrets to a repo you don’t own).
39+
* **AWS Account** with IAM permissions to provision EC2, S3, etc.
40+
* **GitHub Secrets**
3641

37-
- **AWS Account** with IAM permissions for creating EC2, S3, and other resources.
38-
- **IAM User** with access keys for programmatic access.
39-
- **AWS CLI** installed and configured on your machine.
40-
- **Terraform** (version >= 1.0 recommended).
41-
- **Git** installed for version control.
42-
- An **EC2 Key Pair** set up in AWS Console for securely accessing instances (see [Key Pair Section](#ec2-key-pair-requirement)).
42+
* `AWS_ACCESS_KEY_ID` – IAM user access key
43+
* `AWS_SECRET_ACCESS_KEY` – IAM user secret key
44+
* `SSH_PRIVATE_KEY` – Private key for SSH access to EC2 instances
45+
* Terraform installed (for local testing if required)
46+
* EC2 Key Pair configured in AWS and referenced in Terraform configs
4347

4448
---
4549

46-
## 🔐 **AWS Credentials Setup**
50+
## 🔐 **How to Get SSH Private Key from .pem File**
4751

48-
Terraform authenticates with AWS using your configured credentials.
52+
When you create an AWS EC2 Key Pair, AWS provides a `.pem` file. To use this in GitHub Actions, you must convert it to a format that can be stored as a secret.
4953

50-
### Option 1: AWS CLI (Recommended)
54+
### Steps:
5155

52-
```bash
53-
aws configure
54-
```
55-
Provide the following inputs:
56-
- AWS Access Key ID
57-
- AWS Secret Access Key
58-
- Default AWS region (e.g., `ap-south-1`)
59-
- Default output format (e.g., `json`)
56+
1. **Generate the Key Pair in AWS Console** (download the `.pem` file)
57+
2. Open the `.pem` file in a text editor and copy its contents.
58+
3. Add it as a GitHub secret named `SSH_PRIVATE_KEY` in your forked repository.
6059

61-
### Option 2: Environment Variables
60+
Example:
6261

63-
Set environment variables explicitly:
6462
```bash
65-
export AWS_ACCESS_KEY_ID=your_access_key
66-
export AWS_SECRET_ACCESS_KEY=your_secret_key
67-
export AWS_DEFAULT_REGION=ap-south-1
68-
```
69-
70-
---
71-
72-
## EC2 Key Pair Requirement
73-
74-
Ensure you have an EC2 Key Pair set up in the AWS Console. Update the key pair's name in these files:
75-
76-
**`terraform/variables.tf`**
77-
```hcl
78-
variable "key_name" {
79-
default = "your-key-name-here"
80-
}
63+
cat path/to/your-key.pem
8164
```
8265

83-
**`terraform/dev_config.tfvars`**
84-
```hcl
85-
key_name = "your-key-name-here"
86-
```
87-
88-
**`terraform/prod_config.tfvars`**
89-
```hcl
90-
key_name = "your-key-name-here"
91-
```
92-
93-
The Key Pair ensures secure SSH access to the instances.
66+
Copy the entire output (including `-----BEGIN RSA PRIVATE KEY-----` and `-----END RSA PRIVATE KEY-----`) into the GitHub secret.
9467

95-
---
96-
97-
## 🚀 **How to Deploy**
68+
⚠️ Ensure your `.pem` file permissions are secure:
9869

99-
### 1️⃣ Clone the Repository
10070
```bash
101-
git clone https://github.com/git-user-9/tech_eazy_devops_git-user-9.git
102-
cd tech_eazy_devops_git-user-9
71+
chmod 400 path/to/your-key.pem
10372
```
10473

105-
### 2️⃣ Run the Deployment Script
106-
```bash
107-
./scripts/deploy.sh dev # For Development Environment
108-
./scripts/deploy.sh prod # For Production Environment
109-
```
110-
This will:
111-
- Apply Terraform configurations for selected environment
112-
- Output the public IP of the created EC2 instance
113-
- Upload logs to S3 automatically
114-
- Terminate the instance after 10-15 minutes if configured
115-
116-
### 3️⃣ Access the Application
117-
Navigate to:
118-
```
119-
http://<ec2-public-ip>:80
120-
```
121-
122-
---
123-
124-
## 🛠️ **Details of Automation**
125-
126-
### Terraform Provisions:
127-
- **EC2 Instances** within the default VPC.
128-
- **Security Groups** with HTTP (80) and SSH (22) access.
129-
- **IAM Roles** for instances to access S3.
130-
131-
### Shell Scripts:
132-
- Update operating system packages.
133-
- Install required tools such as Java, Git, Maven, AWS CLI, etc.
134-
- Clone, build, and run the application on Port 80.
135-
- Upload logs to the S3 bucket.
136-
13774
---
13875

139-
## Note on Pulling Logs from EC2 to Local
76+
## 🚀 **Deployment Workflow**
14077

141-
To enable **log pulling from EC2 to your local machine,** follow these steps:
78+
The deployment is managed via GitHub Actions.
14279

143-
1. **Uncomment Lines in the Script:**
144-
*This step is only if you want logs to be fetched from s3 to your local directory*
145-
Locate the following lines in your deployment script between **lines 52–59** and uncomment them:
80+
### ✅ Trigger Methods
14681

147-
```bash
148-
# Wait a while for logs to upload
149-
sleep 100
150-
cd .. # Save logs at the root level
151-
PRIVATE_KEY_PATH="/Users/default/CS/DevOps/AWS/ssh-key-ec2.pem" # Change this to your SSH private key path and ensure `chmod 400` on your key
152-
echo "Trying to SCP logs to local"
153-
scp -r -i "$PRIVATE_KEY_PATH" ubuntu@$VERIFIER_IP:/mylogs/ . # Pull logs from EC2 to /mylogs/ in your local directory
154-
cd $TERRAFORM_DIR # Return to Terraform directory for destroy commands
155-
```
82+
* **Push to Branch**: `devops/a3`
83+
* **Git Tags**: `deploy-dev` (for Dev), `deploy-prod` (for Prod)
84+
* **Manual Trigger**: Run from GitHub Actions → Select Stage (dev/prod)
15685

157-
2. **Specify Your Private Key Path:**
158-
- Replace the placeholder `"/Users/default/CS/DevOps/AWS/ssh-key-ec2.pem"` under the variable `PRIVATE_KEY_PATH` with the actual path to your EC2 key's private key file.
159-
- Before using, ensure the private key has the appropriate permissions by running:
160-
```bash
161-
chmod 400 /path/to/your/private-key.pem
162-
```
86+
### 📖 Overview of Workflow
16387

164-
3. **Save the logs locally:**
165-
After successfully setting this up, the script will pull logs from `/mylogs/` on your EC2 instance to a local `/mylogs/` directory at the repository's root level.
88+
The workflow performs the following steps:
16689

167-
This addition ensures your logs are saved to your local environment automatically.
90+
1. **Checkout Repository** – Fetches the code from the repository.
91+
2. **Configure AWS Credentials** – Uses GitHub Secrets to authenticate with AWS.
92+
3. **Setup Terraform** – Installs Terraform and initializes configuration.
93+
4. **Determine Stage** – Sets the target environment (dev or prod) based on trigger type.
94+
5. **Provision App EC2 Instance (Write Access)**
16895

96+
* Deploys the first EC2 instance with **write access to S3**.
97+
* Installs required software (Java, Maven, Git, etc.).
98+
* Pulls source code from the repository and builds the Maven application.
99+
* Runs the application and pushes logs (system and app logs) to the S3 bucket.
100+
6. **Provision Verifier EC2 Instance (Read Access)**
169101

102+
* Deploys a second EC2 instance with **read-only access to S3**.
103+
* Uses AWS CLI to pull logs from the S3 bucket to the instance.
104+
7. **Log Validation via SSH**
170105

171-
---
172-
173-
## 💬 **FAQs**
174-
175-
**Q: How can I deploy in a different region?**
176-
Modify the `aws_region` variable in the `terraform/variables.tf` file and update it in the `.tfvars` files.
177-
178-
**Q: What happens if deployment fails?**
179-
Terraform maintains a state file. Retry by running the deployment script again.
180-
181-
**Q: Where can I find the logs?**
182-
Logs are stored in the `mylogs/` directory or uploaded to the configured S3 bucket.
183-
184-
---
106+
* SSH into the Verifier EC2 instance.
107+
* Validates that required logs exist in S3.
108+
* Prints the last 20 lines of each log for inspection.
109+
8. **App Health Check** – Ensures the application is healthy (HTTP 200 response).
110+
9. **Destroy Infrastructure** – After validation, destroys all provisioned resources and cleans up Terraform workspaces.
185111

112+
This workflow fully automates the lifecycle: provisioning, deployment, validation, and cleanup, ensuring no manual intervention is needed during the process.
186113

0 commit comments

Comments
 (0)