Skip to content

Commit de83c66

Browse files
authored
Merge pull request #178 from Code-Paragon/devops/terraform-infrastructure
feat(devops): introduce terraform infrastructure as code
2 parents 914528c + 1866887 commit de83c66

File tree

6 files changed

+219
-0
lines changed

6 files changed

+219
-0
lines changed

infra/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# AgenticPay Infrastructure
2+
3+
This directory contains the Infrastructure as Code (IaC) for AgenticPay using Terraform. It provisions AWS resources for the Next.js frontend (AWS Amplify), Express.js backend (AWS App Runner), and underlying networking (VPC).
4+
5+
## Architecture
6+
- **State Management**: Remote state stored securely in AWS S3 with DynamoDB state locking.
7+
- **Frontend**: AWS Amplify (optimizes Next.js SSR and static asset delivery).
8+
- **Backend**: AWS App Runner (serverless container compute pulling from ECR).
9+
- **Networking**: Isolated VPC with public/private subnets and NAT Gateways.
10+
11+
## Supported Environments
12+
We use a workspace/tfvars approach to support multiple environments:
13+
- `dev`: Active development and testing against Stellar Testnet.
14+
- `staging`: Pre-production replica against Stellar Testnet.
15+
- `prod`: Live production environment against Stellar Public network.
16+
17+
## Usage Guide
18+
19+
### Prerequisites
20+
1. Install [Terraform](https://developer.hashicorp.com/terraform/downloads) (>= 1.5.0).
21+
2. Configure your AWS CLI credentials (`aws configure`).
22+
23+
### Deployment Steps
24+
25+
1. **Initialize Terraform**
26+
Downloads the required providers and initializes the S3 backend.
27+
```bash
28+
terraform init
29+
```
30+
2. Select an Environment
31+
Select the workspace corresponding to your environment (create it if it doesn't exist).
32+
```
33+
terraform workspace select dev || terraform workspace new dev
34+
```
35+
3. Plan the Deployment
36+
Review the changes Terraform will make to your infrastructure.
37+
```bash
38+
terraform plan -var-file="environments/dev.tfvars"
39+
```
40+
4. Apply the Changes
41+
Provision the resources.
42+
```bash
43+
terraform apply -var-file="environments/dev.tfvars"
44+
```

infra/environments/dev.tfvars

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
environment = "dev"
2+
stellar_network = "testnet"
3+
vpc_cidr = "10.0.0.0/16"
4+
private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
5+
public_subnets = ["10.0.101.0/24", "10.0.102.0/24"]

infra/environments/prod.tfvars

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
environment = "prod"
2+
stellar_network = "public"
3+
vpc_cidr = "10.2.0.0/16"
4+
private_subnets = ["10.2.1.0/24", "10.2.2.0/24"]
5+
public_subnets = ["10.2.101.0/24", "10.2.102.0/24"]

infra/environments/staging.tfvars

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
environment = "staging"
2+
stellar_network = "testnet"
3+
vpc_cidr = "10.1.0.0/16"
4+
private_subnets = ["10.1.1.0/24", "10.1.2.0/24"]
5+
public_subnets = ["10.1.101.0/24", "10.1.102.0/24"]

infra/main.tf

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
terraform {
2+
required_version = ">= 1.5.0"
3+
4+
# Acceptance Criteria: State management
5+
backend "s3" {
6+
bucket = "agenticpay-terraform-state"
7+
key = "infrastructure/terraform.tfstate"
8+
region = "us-east-1"
9+
dynamodb_table = "agenticpay-terraform-locks"
10+
encrypt = true
11+
}
12+
13+
required_providers {
14+
aws = {
15+
source = "hashicorp/aws"
16+
version = "~> 5.0"
17+
}
18+
}
19+
}
20+
21+
provider "aws" {
22+
region = var.aws_region
23+
24+
default_tags {
25+
tags = {
26+
Project = "AgenticPay"
27+
Environment = var.environment
28+
ManagedBy = "Terraform"
29+
}
30+
}
31+
}
32+
33+
# ------------------------------------------------------------------------------
34+
# FOUNDATIONAL NETWORKING
35+
# ------------------------------------------------------------------------------
36+
module "vpc" {
37+
source = "terraform-aws-modules/vpc/aws"
38+
version = "5.0.0"
39+
40+
name = "agenticpay-${var.environment}-vpc"
41+
cidr = var.vpc_cidr
42+
43+
azs = ["${var.aws_region}a", "${var.aws_region}b"]
44+
private_subnets = var.private_subnets
45+
public_subnets = var.public_subnets
46+
47+
enable_nat_gateway = true
48+
single_nat_gateway = var.environment != "prod" # Cost optimization for non-prod
49+
}
50+
51+
# ------------------------------------------------------------------------------
52+
# BACKEND RESOURCES (Express.js API)
53+
# ------------------------------------------------------------------------------
54+
resource "aws_ecr_repository" "backend" {
55+
name = "agenticpay-backend-${var.environment}"
56+
image_tag_mutability = "MUTABLE"
57+
58+
image_scanning_configuration {
59+
scan_on_push = true
60+
}
61+
}
62+
63+
resource "aws_apprunner_service" "backend" {
64+
service_name = "agenticpay-backend-${var.environment}"
65+
66+
source_configuration {
67+
image_repository {
68+
image_configuration {
69+
port = "3001"
70+
runtime_environment_variables = {
71+
NODE_ENV = var.environment
72+
STELLAR_NETWORK = var.stellar_network
73+
}
74+
}
75+
image_identifier = "${aws_ecr_repository.backend.repository_url}:latest"
76+
image_repository_type = "ECR"
77+
}
78+
auto_deployments_enabled = true
79+
}
80+
81+
network_configuration {
82+
egress_configuration {
83+
egress_type = "VPC"
84+
vpc_connector_arn = aws_apprunner_vpc_connector.connector.arn
85+
}
86+
}
87+
}
88+
89+
resource "aws_apprunner_vpc_connector" "connector" {
90+
vpc_connector_name = "agenticpay-vpc-connector-${var.environment}"
91+
subnets = module.vpc.private_subnets
92+
security_groups = [module.vpc.default_security_group_id]
93+
}
94+
95+
# ------------------------------------------------------------------------------
96+
# FRONTEND RESOURCES (Next.js)
97+
# ------------------------------------------------------------------------------
98+
resource "aws_amplify_app" "frontend" {
99+
name = "agenticpay-frontend-${var.environment}"
100+
repository = "https://github.com/Smartdevs17/agenticpay"
101+
102+
build_spec = <<-EOT
103+
version: 1
104+
frontend:
105+
phases:
106+
preBuild:
107+
commands:
108+
- cd frontend
109+
- npm install
110+
build:
111+
commands:
112+
- npm run build
113+
artifacts:
114+
baseDirectory: frontend/.next
115+
files:
116+
- '**/*'
117+
cache:
118+
paths:
119+
- frontend/node_modules/**/*
120+
EOT
121+
122+
environment_variables = {
123+
NEXT_PUBLIC_API_URL = "https://${aws_apprunner_service.backend.service_url}/api/v1"
124+
NODE_ENV = var.environment
125+
}
126+
}

infra/variables.tf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
variable "aws_region" {
2+
description = "The AWS region to deploy into"
3+
type = string
4+
default = "us-east-1"
5+
}
6+
7+
variable "environment" {
8+
description = "The deployment environment (dev, staging, prod)"
9+
type = string
10+
validation {
11+
condition = contains(["dev", "staging", "prod"], var.environment)
12+
error_message = "Environment must be dev, staging, or prod."
13+
}
14+
}
15+
16+
variable "stellar_network" {
17+
description = "Stellar network to connect to (testnet or public)"
18+
type = string
19+
}
20+
21+
variable "vpc_cidr" {
22+
description = "CIDR block for the VPC"
23+
type = string
24+
}
25+
26+
variable "private_subnets" {
27+
description = "List of private subnet CIDRs"
28+
type = list(string)
29+
}
30+
31+
variable "public_subnets" {
32+
description = "List of public subnet CIDRs"
33+
type = list(string)
34+
}

0 commit comments

Comments
 (0)