Sobes.tech
Middle

Where in the pipeline should new infrastructure be connected, and should it be prepared in advance?

sobes.tech AI

Answer from AI

The new infrastructure should be connected at the deployment stage of the pipeline. It needs to be prepared in advance.

Main reasons for early infrastructure preparation:

  • Stability: Testing the application on the planned infrastructure early (testing, staging) reveals incompatibilities or configuration issues before production.
  • Speed: The process of creating infrastructure often takes an unpredictable amount of time. Having the infrastructure ready before deployment speeds up the release.
  • Idempotency: Using Infrastructure as Code (IaC) allows creating or updating infrastructure deterministically, minimizing manual errors.
  • Security: Pre-prepared infrastructure undergoes necessary security checks and audits.

Pipeline stages and the role of infrastructure:

  1. Build: Compilation of code, creation of artifacts (Docker images, binaries). The build infrastructure (CI/CD runner) should already be ready.
  2. Test: Running Unit, Integration, End-to-End tests. For integration/E2E tests, part or all of the target infrastructure may be needed (test databases, dependency services). This can be a temporary or permanent test environment.
  3. Deploy (Staging): Deploying artifacts to an intermediate (staging) environment, as close as possible to production. At this stage, the target infrastructure (staging) should be fully ready.
  4. Deploy (Production): Deploying to the production environment. The target infrastructure (production) should be fully ready.

Example of using Terraform (IaC) to prepare infrastructure before deployment:

// main.tf
resource "aws_instance" "app_server" {
  ami           = "ami-0c55b159cbfafe1f0" // Example AMI
  instance_type = "t2.micro"
  subnet_id     = aws_subnet.app_subnet.id

  tags = {
    Name = "AppServer-${var.environment}"
  }
}

resource "aws_subnet" "app_subnet" {
  vpc_id     = aws_vpc.app_vpc.id
  cidr_block = "10.0.1.0/24"

  tags = {
    Name = "AppSubnet-${var.environment}"
  }
}

resource "aws_vpc" "app_vpc" {
  cidr_block = "10.0.0.0/16"

  tags = {
    Name = "AppVPC-${var.environment}"
  }
}

variable "environment" {
  description = "Deployment environment (e.g., staging, production)"
  type        = string
}
# pipeline_script.sh
#!/bin/bash

# Apply infrastructure for staging
terraform init
terraform apply -var="environment=staging" -auto-approve

# Check infrastructure status (optional)

# Deploy application to staging
# deploy_app_to_staging.sh

# Run tests on staging
# run_staging_tests.sh

# If tests are successful, apply infrastructure for production
terraform init
terraform apply -var="environment=production" -auto-approve

# Deploy application to production
# deploy_app_to_production.sh

In summary, infrastructure should be prepared in advance. The ideal moment for creating/updating it via IaC is a separate step before the actual deployment of the application to a new or updated environment.