Skip to content

Commit b09671f

Browse files
committed
logs sent to s3 bucket
Signed-off-by: Akhil <2100030084@kluniversity.in>
1 parent 8782237 commit b09671f

File tree

13 files changed

+290
-84
lines changed

13 files changed

+290
-84
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ override.tf.json
3535
# Ignore CLI configuration files
3636
.terraformrc
3737
terraform.rc
38+
39+
.terraform.lock.hcl

README.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ This project automates the provisioning of an EC2 instance and the deployment of
88

99
```
1010
tech_eazy_devops_git-user-9/
11-
├── README.md
12-
├── terraform/
13-
│ ├── main.tf
14-
│ ├── outputs.tf
15-
│ ├── variables.tf
16-
│ ├── dev_config.tfvars
17-
│ └── prod_config.tfvars
18-
└── scripts/
19-
├── deploy.sh
20-
├── dev_script.sh
21-
└── prod_script.sh
11+
├── README.md # Project documentation
12+
├── terraform/ # Contains Terraform configurations
13+
│ ├── main.tf # Main Terraform configuration file
14+
│ ├── 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
2222
```
2323

2424
---
@@ -66,8 +66,8 @@ export AWS_DEFAULT_REGION=ap-south-1
6666
### 1️⃣ Clone the Repository
6767

6868
```bash
69-
git clone <your-repo-url>
70-
cd tech_eazy_devops_git-user-9
69+
git clone https://github.com/git-user-9/tech_eazy_devops_git-user-9.git app
70+
cd app
7171
```
7272

7373
### 2️⃣ Run the Deployment Script
@@ -81,6 +81,8 @@ The script will:
8181
- Load the corresponding Terraform variable file
8282
- Initialize and apply the Terraform configuration
8383
- Output the public IP of the created EC2 instance
84+
- Upload logs to S3 bucket
85+
- Shutdown the instance after 10-15 minutes (configurable)
8486

8587
### 3️⃣ Access the Application
8688

@@ -100,9 +102,11 @@ http://<your-ec2-ip>:80
100102

101103
### User Data (inside shell script):
102104
- Updates the system
103-
- Installs Java 21, Git, and Maven
105+
- Installs AWS CLI, curl, unzip, Java 21, Git, and Maven
104106
- Clones and builds your application
105107
- Launches the application on port 80
108+
- Uploads logs to S3 bucket
109+
- Shuts down the instance after 10-15 minutes (configurable)
106110

107111
---
108112

@@ -117,7 +121,7 @@ http://<your-ec2-ip>:80
117121
## 💬 **FAQ**
118122

119123
**Q: Can I deploy to a different AWS region?**
120-
Yes. Modify the `region` value in `terraform/main.tf` or export `AWS_DEFAULT_REGION`.
124+
Yes. Modify the `aws_region` value in `terraform/variables.tf`, `dev_config.tfvars` and `prod_config.tfvars`
121125

122126

123127

scripts/deploy.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@ curl "http://$RAW_INSTANCE_IP:80"
3737

3838
echo "[+] Instance Public IP: $RAW_INSTANCE_IP"
3939

40-
sleep 650
41-
terraform destroy -var-file="$CONFIG_FILE" -auto-approve
40+
41+
# sleep 650
42+
43+
# terraform destroy -var-file="$CONFIG_FILE" -auto-approve

scripts/dev_script.sh

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
11
#!/bin/bash
22
set -e
33

4-
# Update and install dependencies
4+
# Update system and install dependencies
55
apt-get update -y
6-
apt-get install -y openjdk-21-jdk maven git
6+
apt-get install -y unzip curl git openjdk-21-jdk maven
7+
8+
# Install AWS CLI v2
9+
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
10+
unzip awscliv2.zip
11+
sudo ./aws/install
12+
713

814
# Set JAVA_HOME
915
export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))
1016
echo "export JAVA_HOME=$JAVA_HOME" >> /etc/profile
1117
export PATH=$JAVA_HOME/bin:$PATH
1218

13-
# Clone the repo
1419
cd /home/ubuntu
15-
git clone https://github.com/techeazy-consulting/techeazy-devops app
20+
git clone ${repo_url} app
21+
#git checkout HEAD~1 # Latest commit in repo has bug two @GetMapping("/")
22+
1623
cd app
17-
# git checkout HEAD~1 # Latest commit in repo has bug two @GetMapping("/")
24+
mvn clean package
25+
26+
# Run the Java app
27+
nohup java -jar target/*.jar --server.port=80 > /var/log/my-app.log 2>&1 &
28+
29+
# Wait for the app to start
30+
sleep 30
1831

19-
# Build the application
20-
mvn package
32+
# Upload Logs to S3
33+
aws s3 cp /var/log/cloud-init.log s3://${s3_bucket_name}/system/
34+
aws s3 cp /var/log/my-app.log s3://${s3_bucket_name}/app/
2135

22-
# Run the app in background and redirect output
23-
nohup java -jar target/techeazy-devops-0.0.1-SNAPSHOT.jar > /home/ubuntu/app.log 2>&1 &
36+
# Shutdown after timeout
37+
sudo shutdown -h +${shutdown_minutes}
2438

25-
shutdown -h +10

scripts/prod_script.sh

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
11
#!/bin/bash
22
set -e
33

4-
# Update and install dependencies
4+
# Update system and install dependencies
55
apt-get update -y
6-
apt-get install -y openjdk-21-jdk maven git
6+
apt-get install -y unzip curl git openjdk-21-jdk maven
7+
8+
# Install AWS CLI v2
9+
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
10+
unzip awscliv2.zip
11+
sudo ./aws/install
12+
713

814
# Set JAVA_HOME
915
export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))
1016
echo "export JAVA_HOME=$JAVA_HOME" >> /etc/profile
1117
export PATH=$JAVA_HOME/bin:$PATH
1218

13-
# Clone the repo
1419
cd /home/ubuntu
15-
git clone https://github.com/techeazy-consulting/techeazy-devops app
20+
git clone ${repo_url} app
21+
#git checkout HEAD~1 # Latest commit in repo has bug two @GetMapping("/")
22+
1623
cd app
17-
# git checkout HEAD~1 # Latest commit in repo has bug two @GetMapping("/")
24+
mvn clean package
25+
26+
# Run the Java app
27+
nohup java -jar target/*.jar --server.port=80 > /var/log/my-app.log 2>&1 &
28+
29+
# Wait for the app to start
30+
sleep 30
1831

19-
# Build the application
20-
mvn package
32+
# Upload Logs to S3
33+
aws s3 cp /var/log/cloud-init.log s3://${s3_bucket_name}/system/
34+
aws s3 cp /var/log/my-app.log s3://${s3_bucket_name}/app/
2135

22-
# Run the app in background and redirect output
23-
nohup java -jar target/techeazy-devops-0.0.1-SNAPSHOT.jar > /home/ubuntu/app.log 2>&1 &
36+
# Shutdown after timeout
37+
sudo shutdown -h +${shutdown_minutes}
2438

25-
shutdown -h +10

terraform/dev_config.tfvars

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ instance_type = "t2.micro"
22
key_name = "ssh-key-ec2"
33
stage = "Dev"
44
shutdown_minutes = 10
5+
s3_bucket_name = "techeazy-logs-dev-unique123ss" # Change this!
6+
aws_region = "ap-south-1"
7+
repo_url = "https://github.com/techeazy-consulting/techeazy-devops"

terraform/ec2.tf

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Security Group
2+
resource "aws_security_group" "web_sg" {
3+
name = "web-sg-${var.stage}"
4+
description = "Allow HTTP and SSH"
5+
6+
ingress {
7+
from_port = 80
8+
to_port = 80
9+
protocol = "tcp"
10+
cidr_blocks = ["0.0.0.0/0"]
11+
}
12+
13+
ingress {
14+
from_port = 22
15+
to_port = 22
16+
protocol = "tcp"
17+
cidr_blocks = ["0.0.0.0/0"]
18+
}
19+
20+
egress {
21+
from_port = 0
22+
to_port = 0
23+
protocol = "-1"
24+
cidr_blocks = ["0.0.0.0/0"]
25+
}
26+
27+
tags = {
28+
Name = "WebSG-${var.stage}"
29+
Stage = var.stage
30+
}
31+
}
32+
33+
# EC2 Instance
34+
resource "aws_instance" "app" {
35+
ami = var.ami_id
36+
instance_type = var.instance_type
37+
key_name = var.key_name
38+
vpc_security_group_ids = [aws_security_group.web_sg.id]
39+
iam_instance_profile = aws_iam_instance_profile.ec2_instance_profile_b.name
40+
41+
42+
user_data = templatefile("${path.module}/../scripts/${lower(var.stage)}_script.sh", {
43+
s3_bucket_name = var.s3_bucket_name
44+
aws_region = var.aws_region
45+
repo_url = var.repo_url
46+
shutdown_minutes = var.shutdown_minutes
47+
})
48+
49+
tags = {
50+
Name = "TecheazyApp-${var.stage}"
51+
Stage = var.stage
52+
}
53+
}

terraform/iam.tf

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Role A: Read-Only Access to S3
2+
resource "aws_iam_role" "role_a_readonly" {
3+
name = "readonly_s3_role"
4+
5+
assume_role_policy = jsonencode({
6+
Version = "2012-10-17",
7+
Statement = [
8+
{
9+
Effect = "Allow",
10+
Principal = {
11+
AWS = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/uploadonly_s3_role"
12+
},
13+
Action = "sts:AssumeRole"
14+
}
15+
]
16+
})
17+
}
18+
19+
resource "aws_iam_policy" "readonly_policy" {
20+
name = "readonly_s3_policy"
21+
description = "Allows read-only access to S3"
22+
23+
policy = jsonencode({
24+
Version = "2012-10-17",
25+
Statement = [
26+
{
27+
Action = ["s3:ListBucket", "s3:GetObject"],
28+
Effect = "Allow",
29+
Resource = ["arn:aws:s3:::${var.s3_bucket_name}", "arn:aws:s3:::${var.s3_bucket_name}/*"]
30+
}
31+
]
32+
})
33+
}
34+
35+
resource "aws_iam_role_policy_attachment" "readonly_attach" {
36+
role = aws_iam_role.role_a_readonly.name
37+
policy_arn = aws_iam_policy.readonly_policy.arn
38+
}
39+
40+
# Role B: Write-Only Access to S3
41+
resource "aws_iam_role" "role_b_uploader" {
42+
name = "uploadonly_s3_role"
43+
44+
assume_role_policy = jsonencode({
45+
Version = "2012-10-17",
46+
Statement = [
47+
{
48+
Effect = "Allow",
49+
Principal = { Service = "ec2.amazonaws.com" },
50+
Action = "sts:AssumeRole"
51+
}
52+
]
53+
})
54+
}
55+
56+
resource "aws_iam_policy" "uploadonly_policy" {
57+
name = "uploadonly_s3_policy"
58+
description = "Allows write-only access to S3 and the ability to assume Role A"
59+
60+
policy = jsonencode({
61+
Version = "2012-10-17",
62+
Statement = [
63+
{
64+
Effect = "Allow",
65+
Action = ["s3:PutObject", "s3:CreateBucket"],
66+
Resource = ["arn:aws:s3:::${var.s3_bucket_name}", "arn:aws:s3:::${var.s3_bucket_name}/*"]
67+
},
68+
{
69+
Effect = "Deny",
70+
Action = ["s3:GetBucket"],
71+
Resource = ["arn:aws:s3:::*"]
72+
},
73+
{
74+
Effect = "Allow",
75+
Action = ["sts:AssumeRole"],
76+
Resource = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/readonly_s3_role"
77+
}
78+
]
79+
})
80+
}
81+
82+
resource "aws_iam_role_policy_attachment" "uploadonly_attach" {
83+
role = aws_iam_role.role_b_uploader.name
84+
policy_arn = aws_iam_policy.uploadonly_policy.arn
85+
}
86+
87+
# Instance profiles for EC2
88+
resource "aws_iam_instance_profile" "ec2_instance_profile_a" {
89+
name = "${var.stage}_readonly_instance_profile"
90+
role = aws_iam_role.role_a_readonly.name
91+
}
92+
93+
resource "aws_iam_instance_profile" "ec2_instance_profile_b" {
94+
name = "${var.stage}_writeonly_instance_profile"
95+
role = aws_iam_role.role_b_uploader.name
96+
}

terraform/main.tf

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,5 @@ provider "aws" {
22
region = var.aws_region
33
}
44

5-
resource "aws_security_group" "web_sg" {
6-
name = "web-sg-${var.stage}"
7-
description = "Allow HTTP and SSH"
8-
9-
ingress {
10-
from_port = 80
11-
to_port = 80
12-
protocol = "tcp"
13-
cidr_blocks = ["0.0.0.0/0"]
14-
}
15-
ingress {
16-
from_port = 22
17-
to_port = 22
18-
protocol = "tcp"
19-
cidr_blocks = ["0.0.0.0/0"]
20-
}
21-
22-
egress {
23-
from_port = 0
24-
to_port = 0
25-
protocol = "-1"
26-
cidr_blocks = ["0.0.0.0/0"]
27-
}
28-
}
29-
30-
resource "aws_instance" "app" {
31-
ami = var.ami_id
32-
instance_type = var.instance_type
33-
key_name = var.key_name
34-
vpc_security_group_ids = [aws_security_group.web_sg.id]
35-
36-
user_data = file("${path.module}/../scripts/${lower(var.stage)}_script.sh")
37-
38-
tags = {
39-
Name = "TecheazyApp-${var.stage}"
40-
Stage = var.stage
41-
}
42-
}
5+
# Data source for current AWS account
6+
data "aws_caller_identity" "current" {}

0 commit comments

Comments
 (0)