You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# AWS EC2 Auto Deployment with Terraform & Shell Scripts
1
+
# AWS EC2 Auto Deployment with Terraform & GitHub Actions
2
2
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.
4
4
5
5
---
6
6
@@ -9,14 +9,18 @@ This project automates the provisioning of an EC2 instance and the deployment of
9
9
```
10
10
tech_eazy_devops_git-user-9/
11
11
├── 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
│ ├── dev_script.sh # Dev-specific configuration script for EC2
21
25
│ ├── prod_script.sh # Production-specific script for EC2
22
26
│ ├── verify_logs.sh # Validates and uploads logs
@@ -25,162 +29,85 @@ tech_eazy_devops_git-user-9/
25
29
│ │ └── my-app.log # Main application log
26
30
│ └── system/ # Tracks provisioning/system logs
27
31
│ └── cloud-init.log # Logs of initialization processes
28
-
└── .gitignore # Lists files to exclude from version control
29
32
```
30
33
31
34
---
32
35
33
36
## ⚙️ **Prerequisites**
34
37
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**
36
41
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
43
47
44
48
---
45
49
46
-
## 🔐 **AWS Credentials Setup**
50
+
## 🔐 **How to Get SSH Private Key from .pem File**
47
51
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.
49
53
50
-
### Option 1: AWS CLI (Recommended)
54
+
### Steps:
51
55
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.
60
59
61
-
### Option 2: Environment Variables
60
+
Example:
62
61
63
-
Set environment variables explicitly:
64
62
```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
81
64
```
82
65
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.
94
67
95
-
---
96
-
97
-
## 🚀 **How to Deploy**
68
+
⚠️ Ensure your `.pem` file permissions are secure:
***Manual Trigger**: Run from GitHub Actions → Select Stage (dev/prod)
156
85
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
163
87
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:
166
89
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)** –
168
95
96
+
* Deploys the first EC2 instance with **write access to S3**.
* 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** –
170
105
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.
185
111
112
+
This workflow fully automates the lifecycle: provisioning, deployment, validation, and cleanup, ensuring no manual intervention is needed during the process.
0 commit comments