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
# Proof of Concept: Node.js to AWS ECR with Terraform & GitHub Actions
1
+
# POC: Node.js to AWS ECR with Terraform & GitHub Actions
2
2
3
3
This project serves as a Proof of Concept (PoC) demonstrating the deployment of a Node.js (Express) backend application, containerized with Docker, to Amazon Elastic Container Registry (ECR) using Terraform for infrastructure provisioning and GitHub Actions for CI/CD automation.
4
4
5
5
## Overview
6
6
7
7
The primary goal is to showcase an automated workflow where:
8
+
8
9
1. Terraform defines and manages the AWS ECR repository and related resources.
9
10
2. A local setup script (`setup_tf_backend.sh`) configures the Terraform backend using AWS S3 and DynamoDB.
10
11
3. GitHub Actions, upon a push to the `main` branch, authenticates to AWS using an IAM Role (OIDC), builds the Docker image, and pushes it to the ECR repository provisioned by Terraform.
11
12
12
13
## Features
13
14
14
-
-Infrastructure as Code (IaC) using Terraform for AWS ECR.
15
-
-Automated CI/CD pipeline with GitHub Actions.
16
-
-Secure authentication to AWS from GitHub Actions using IAM Roles for Service Accounts (OIDC).
17
-
-Docker containerization of a Node.js Express application.
18
-
-Automated Terraform backend configuration (S3 bucket and DynamoDB table for state locking).
15
+
- Infrastructure as Code (IaC) using Terraform for AWS ECR.
16
+
- Automated CI/CD pipeline with GitHub Actions.
17
+
- Secure authentication to AWS from GitHub Actions using IAM Roles for Service Accounts (OIDC).
18
+
- Docker containerization of a Node.js Express application.
19
+
- Automated Terraform backend configuration (S3 bucket and DynamoDB table for state locking).
19
20
20
21
## Tech Stack
21
22
22
-
-**Cloud Provider:** AWS
23
-
- ECR (Elastic Container Registry)
24
-
- S3 (for Terraform backend state)
25
-
- DynamoDB (for Terraform state locking)
26
-
- IAM (Identity and Access Management - OIDC for GitHub Actions)
27
-
-**IaC:** Terraform
28
-
-**CI/CD:** GitHub Actions
29
-
-**Containerization:** Docker
30
-
-**Application:** Node.js (Express.js)
23
+
-**Cloud Provider:** AWS
24
+
- ECR (Elastic Container Registry)
25
+
- S3 (for Terraform backend state)
26
+
- DynamoDB (for Terraform state locking)
27
+
- IAM (Identity and Access Management - OIDC for GitHub Actions)
28
+
-**IaC:** Terraform
29
+
-**CI/CD:** GitHub Actions
30
+
-**Containerization:** Docker
31
+
-**Application:** Node.js (Express.js)
31
32
32
33
## Prerequisites
33
34
34
35
Before you begin, ensure you have the following installed and configured:
Create an IAM Role in your AWS account that GitHub Actions can assume. This role needs permissions to manage ECR, S3 (for Terraform backend), and DynamoDB (for Terraform state lock table).
47
48
48
-
- Follow the AWS documentation for [Configuring OpenID Connect in Amazon Web Services](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_create_oidc.html).
49
-
- The trust relationship for the IAM role should be configured for GitHub Actions. Example policy snippet:
- Attach necessary permission policies to this role (e.g., `AmazonEC2ContainerRegistryFullAccess`, plus custom policies for S3 backend bucket creation/access and DynamoDB table creation/access if the setup script handles this).
49
+
- Follow the AWS documentation for [Configuring OpenID Connect in Amazon Web Services](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_create_oidc.html).
50
+
- The trust relationship for the IAM role should be configured for GitHub Actions. Example policy snippet:
- Attach necessary permission policies to this role (e.g., `AmazonEC2ContainerRegistryFullAccess`, plus custom policies for S3 backend bucket creation/access and DynamoDB table creation/access if the setup script handles this).
71
73
72
74
### 2. Configure GitHub Secrets
73
75
74
76
In your GitHub repository, navigate to `Settings > Secrets and variables > Actions` and add the following secrets:
75
77
76
-
- `AWS_REGION`: Your AWS region (e.g., `us-east-1`).
77
-
- `AWS_IAM_ROLE_ARN`: The ARN of the IAM role created in the previous step (e.g., `arn:aws:iam::YOUR_AWS_ACCOUNT_ID:role/YOUR_IAM_ROLE_NAME`).
78
+
-`AWS_REGION`: Your AWS region (e.g., `us-east-1`).
79
+
-`AWS_IAM_ROLE_ARN`: The ARN of the IAM role created in the previous step (e.g., `arn:aws:iam::YOUR_AWS_ACCOUNT_ID:role/YOUR_IAM_ROLE_NAME`).
78
80
79
81
### 3. Terraform Backend Setup Script
80
82
81
83
The `setup_tf_backend.sh` script automates the creation of the S3 bucket and DynamoDB table for the Terraform backend and generates the necessary Terraform configuration files.
82
84
83
-
- The script is expected to create:
84
-
- `terraform/backend.tf`: Configures the S3 backend for Terraform state.
85
-
- `terraform/variables.tf`: May define variables used by the backend setup or main configuration (e.g., `TF_STATE_KEY`, `TF_BACKEND_REGION` if these are dynamically set or user-provided).
85
+
- The script is expected to create:
86
+
-`terraform/backend.tf`: Configures the S3 backend for Terraform state.
87
+
-`terraform/variables.tf`: May define variables used by the backend setup or main configuration (e.g., `TF_STATE_KEY`, `TF_BACKEND_REGION` if these are dynamically set or user-provided).
86
88
87
89
**Note:** Ensure your Terraform configuration in the `terraform/` directory (especially `main.tf` or a dedicated backend setup file) defines the resources for the S3 bucket and DynamoDB table that the `setup_tf_backend.sh` script will use or create. The script utilizes outputs like `s3_backend_bucket_name` and `dynamodb_lock_table_name` from a preliminary Terraform apply for the backend resources.
This script will typically run `terraform init` and `terraform apply` against a configuration designed to provision the S3 bucket and DynamoDB table for the state backend. It then uses the outputs to generate `terraform/backend.tf` (and potentially `terraform/variables.tf`).
108
+
104
109
```bash
105
110
./setup_tf_backend.sh
106
111
```
112
+
107
113
Follow any prompts from the script.
108
114
109
115
4. **Commit and Push Changes:**
110
116
After the script successfully generates `terraform/backend.tf` and `terraform/variables.tf`, commit these files and any other changes to your repository:
@@ -116,10 +123,10 @@ The `setup_tf_backend.sh` script automates the creation of the S3 bucket and Dyn
116
123
117
124
5. **GitHub Actions Workflow:**
118
125
Pushing to the `main` branch will trigger the GitHub Actions workflow defined in`.github/workflows/deploy-to-ecr.yml`. This workflow will:
119
-
- Configure AWS credentials using the OIDC role.
120
-
- Run `terraform init` and `terraform apply` to create/update the ECR repository defined in your main Terraform configuration (e.g., `terraform/main.tf`).
121
-
- Build the Docker image using the `Dockerfile`.
122
-
- Push the Docker image to the ECR repository.
126
+
- Configure AWS credentials using the OIDC role.
127
+
- Run `terraform init` and `terraform apply` to create/update the ECR repository defined in your main Terraform configuration (e.g., `terraform/main.tf`).
128
+
- Build the Docker image using the `Dockerfile`.
129
+
- Push the Docker image to the ECR repository.
123
130
124
131
## Project Structure
125
132
@@ -138,3 +145,4 @@ The `setup_tf_backend.sh` script automates the creation of the S3 bucket and Dyn
138
145
├── Dockerfile # Dockerfile to build the Node.js application image
139
146
├── setup_tf_backend.sh # Script to setup Terraform backend and generate config
0 commit comments