Skip to content

Commit fed863e

Browse files
committed
Added feature to use 2nd EC2 instance(read only role) to pull logs to local directory
1 parent b09671f commit fed863e

File tree

12 files changed

+1117
-98
lines changed

12 files changed

+1117
-98
lines changed

README.md

Lines changed: 113 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# AWS EC2 Auto Deployment with Terraform & Shell Scripts
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 scripts.
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.
44

55
---
66

@@ -9,50 +9,58 @@ 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-
├── terraform/ # Contains Terraform configurations
12+
├── terraform/ # Terraform configurations
1313
│ ├── main.tf # Main Terraform configuration file
1414
│ ├── outputs.tf # Defines Terraform outputs (e.g., EC2 public IP)
15-
│ ├── variables.tf # Declares input variables for Terraform
16-
│ ├── dev_config.tfvars # Terraform variable values for the 'dev' environment
17-
│ └── prod_config.tfvars # Terraform variable values for the 'prod' environment
18-
└── scripts/ # Shell scripts for deployment and configuration
19-
├── deploy.sh # Automates the Terraform apply process for a given environment
20-
├── dev_script.sh # User-data script for configuring EC2 in the 'dev' environment
21-
└── prod_script.sh # User-data script for configuring EC2 in the 'prod' environment
15+
│ ├── variables.tf # Public variables file for EC2 and other details
16+
│ ├── dev_config.tfvars # Variable values for 'Dev' environment
17+
│ ├── prod_config.tfvars # Variable values for 'Prod' environment
18+
├── scripts/ # Shell scripts for provisioning and deployments
19+
│ ├── deploy.sh # Automates provisioning with Terraform
20+
│ ├── dev_script.sh # Dev-specific configuration script for EC2
21+
│ ├── prod_script.sh # Production-specific script for EC2
22+
│ ├── verify_logs.sh # Validates and uploads logs
23+
├── mylogs/ # Application and system logs
24+
│ ├── app/ # Stores runtime application logs
25+
│ │ └── my-app.log # Main application log
26+
│ └── system/ # Tracks provisioning/system logs
27+
│ └── cloud-init.log # Logs of initialization processes
28+
└── .gitignore # Lists files to exclude from version control
2229
```
2330

2431
---
2532

2633
## ⚙️ **Prerequisites**
2734

28-
Before you begin, ensure you have the following:
35+
Ensure the following tools and resources are configured before deploying:
2936

30-
- **AWS Account** with EC2 access and permissions to create resources
31-
- **IAM User** with programmatic access (access key ID and secret access key)
32-
- **AWS CLI** installed and configured
33-
- **Terraform** installed (version >= 1.0 recommended)
34-
- **Git** (optional, for repository operations)
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)).
3543

3644
---
3745

3846
## 🔐 **AWS Credentials Setup**
3947

40-
You must set your AWS credentials in your local environment so Terraform can authenticate with AWS.
48+
Terraform authenticates with AWS using your configured credentials.
4149

42-
### Option 1: Using AWS CLI (Recommended)
50+
### Option 1: AWS CLI (Recommended)
4351

4452
```bash
4553
aws configure
4654
```
47-
48-
You'll be prompted to input your:
55+
Provide the following inputs:
4956
- AWS Access Key ID
5057
- AWS Secret Access Key
51-
- Default region name (e.g., `ap-south-1`)
58+
- Default AWS region (e.g., `ap-south-1`)
5259
- Default output format (e.g., `json`)
5360

54-
### Option 2: Set environment variables directly
61+
### Option 2: Environment Variables
5562

63+
Set environment variables explicitly:
5664
```bash
5765
export AWS_ACCESS_KEY_ID=your_access_key
5866
export AWS_SECRET_ACCESS_KEY=your_secret_key
@@ -61,67 +69,117 @@ export AWS_DEFAULT_REGION=ap-south-1
6169

6270
---
6371

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+
}
81+
```
82+
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.
94+
95+
---
96+
6497
## 🚀 **How to Deploy**
6598

6699
### 1️⃣ Clone the Repository
67-
68100
```bash
69-
git clone https://github.com/git-user-9/tech_eazy_devops_git-user-9.git app
70-
cd app
101+
git clone https://github.com/git-user-9/tech_eazy_devops_git-user-9.git
102+
cd tech_eazy_devops_git-user-9
71103
```
72104

73105
### 2️⃣ Run the Deployment Script
74-
75106
```bash
76-
./scripts/deploy.sh dev # For Dev environment
77-
./scripts/deploy.sh prod # For Prod environment
107+
./scripts/deploy.sh dev # For Development Environment
108+
./scripts/deploy.sh prod # For Production Environment
78109
```
79-
80-
The script will:
81-
- Load the corresponding Terraform variable file
82-
- Initialize and apply the Terraform configuration
110+
This will:
111+
- Apply Terraform configurations for selected environment
83112
- Output the public IP of the created EC2 instance
84-
- Upload logs to S3 bucket
85-
- Shutdown the instance after 10-15 minutes (configurable)
113+
- Upload logs to S3 automatically
114+
- Terminate the instance after 10-15 minutes if configured
86115

87116
### 3️⃣ Access the Application
88-
89-
Visit the public IP shown in the terminal:
90-
117+
Navigate to:
91118
```
92-
http://<your-ec2-ip>:80
119+
http://<ec2-public-ip>:80
93120
```
94121

95122
---
96123

97-
## 🛠️ **What Happens Behind the Scenes**
124+
## 🛠️ **Details of Automation**
98125

99-
### Terraform provisions:
100-
- EC2 instance in default VPC
101-
- Security group with ports 22 (SSH) and 80 (HTTP) open
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.
102130

103-
### User Data (inside shell script):
104-
- Updates the system
105-
- Installs AWS CLI, curl, unzip, Java 21, Git, and Maven
106-
- Clones and builds your application
107-
- Launches the application on port 80
108-
- Uploads logs to S3 bucket
109-
- Shuts down the instance after 10-15 minutes (configurable)
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.
110136

111137
---
112138

113-
## **Security Notes**
139+
## Note on Pulling Logs from EC2 to Local
140+
141+
To enable **log pulling from EC2 to your local machine,** follow these steps:
142+
143+
1. **Uncomment Lines in the Script:**
144+
Locate the following lines in your deployment script between **lines 52–59** and uncomment them:
145+
146+
```bash
147+
# Wait a while for logs to upload
148+
sleep 100
149+
cd .. # Save logs at the root level
150+
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
151+
echo "Trying to SCP logs to local"
152+
scp -r -i "$PRIVATE_KEY_PATH" ubuntu@$VERIFIER_IP:/mylogs/ . # Pull logs from EC2 to /mylogs/ in your local directory
153+
cd $TERRAFORM_DIR # Return to Terraform directory for destroy commands
154+
```
155+
156+
2. **Specify Your Private Key Path:**
157+
- 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.
158+
- Before using, ensure the private key has the appropriate permissions by running:
159+
```bash
160+
chmod 400 /path/to/your/private-key.pem
161+
```
162+
163+
3. **Save the logs locally:**
164+
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.
165+
166+
This addition ensures your logs are saved to your local environment automatically.
167+
114168
115-
- No sensitive keys are stored in the repo
116-
- Make sure your security group is restricted to specific IPs if used in production
117-
- Rotate AWS credentials regularly
118169
119170
---
120171
121-
## 💬 **FAQ**
172+
## 💬 **FAQs**
122173
123-
**Q: Can I deploy to a different AWS region?**
124-
Yes. Modify the `aws_region` value in `terraform/variables.tf`, `dev_config.tfvars` and `prod_config.tfvars`
174+
**Q: How can I deploy in a different region?**
175+
Modify the `aws_region` variable in the `terraform/variables.tf` file and update it in the `.tfvars` files.
125176
177+
**Q: What happens if deployment fails?**
178+
Terraform maintains a state file. Retry by running the deployment script again.
179+
180+
**Q: Where can I find the logs?**
181+
Logs are stored in the `mylogs/` directory or uploaded to the configured S3 bucket.
182+
183+
---
126184
127185

mylogs/app/my-app.log

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
. ____ _ __ _ _
3+
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
4+
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
5+
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
6+
' |____| .__|_| |_|_| |_\__, | / / / /
7+
=========|_|==============|___/=/_/_/_/
8+
9+
:: Spring Boot :: (v3.4.6)
10+
11+
2025-07-02T17:49:01.816Z INFO 4364 --- [techeazy-devops] [ main] c.t.devops.TecheazyDevopsApplication : Starting TecheazyDevopsApplication v0.0.1-SNAPSHOT using Java 21.0.7 with PID 4364 (/home/ubuntu/app/target/techeazy-devops-0.0.1-SNAPSHOT.jar started by root in /home/ubuntu/app)
12+
2025-07-02T17:49:01.821Z INFO 4364 --- [techeazy-devops] [ main] c.t.devops.TecheazyDevopsApplication : No active profile set, falling back to 1 default profile: "default"
13+
2025-07-02T17:49:04.140Z INFO 4364 --- [techeazy-devops] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 80 (http)
14+
2025-07-02T17:49:04.185Z INFO 4364 --- [techeazy-devops] [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
15+
2025-07-02T17:49:04.186Z INFO 4364 --- [techeazy-devops] [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.41]
16+
2025-07-02T17:49:04.491Z INFO 4364 --- [techeazy-devops] [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
17+
2025-07-02T17:49:04.495Z INFO 4364 --- [techeazy-devops] [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2567 ms

0 commit comments

Comments
 (0)