Skip to content

Instantly share code, notes, and snippets.

@greenido
Created November 6, 2024 22:09
Show Gist options
  • Save greenido/15c6479710fefc27c5379c6c17e4400b to your computer and use it in GitHub Desktop.
Save greenido/15c6479710fefc27c5379c6c17e4400b to your computer and use it in GitHub Desktop.
Setting Up Test Environment for Docker Images on AWS

Setting Up Test Environment for Docker Images on AWS

1. Infrastructure Setup using AWS CDK or CloudFormation

# test-environment.yaml
AWSTemplateFormatVersion: '2010-09-01'
Description: 'Test environment for Docker applications'

Parameters:
  VpcId:
    Type: AWS::EC2::VPC::Id
    Description: VPC where resources will be created

Resources:
  TestECSCluster:
    Type: AWS::ECS::Cluster
    Properties:
      ClusterName: test-cluster
      CapacityProviders:
        - FARGATE
      DefaultCapacityProviderStrategy:
        - CapacityProvider: FARGATE
          Weight: 1

  TestTaskExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service: ecs-tasks.amazonaws.com
            Action: sts:AssumeRole
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AmagedAmazonECSTaskExecutionRolePolicy

  TestSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Security group for test environment
      VpcId: !Ref VpcId
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 3000
          ToPort: 3000
          CidrIp: 0.0.0.0/0
        - IpProtocol: tcp
          FromPort: 5433
          ToPort: 5433
          CidrIp: 0.0.0.0/0

2. Test Environment Docker Compose

Create a new file docker-compose.test.yml:

version: '3.8'

services:
  app:
    image: ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/node-app:test
    container_name: node_app_test
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=test
      - DATABASE_HOST=postgres_test
      - DATABASE_PORT=5433
    depends_on:
      - postgres_test

  postgres_test:
    image: ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/postgres-db:test
    container_name: postgres_db_test
    ports:
      - "5433:5433"
    environment:
      - POSTGRES_DB=testdb
      - POSTGRES_USER=testuser
      - POSTGRES_PASSWORD=testpass
    volumes:
      - postgres_test_data:/var/lib/postgresql/data

volumes:
  postgres_test_data:

3. Test Environment Setup Scripts

3.1 Create Test ECR Repositories

#!/bin/bash
# create-test-repos.sh

# Set variables
AWS_REGION="your-region"
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)

# Create ECR repositories for test environment
aws ecr create-repository \
    --repository-name node-app \
    --image-tag-mutability MUTABLE \
    --image-scanning-configuration scanOnPush=true \
    --tags Key=Environment,Value=test

aws ecr create-repository \
    --repository-name postgres-db \
    --image-tag-mutability MUTABLE \
    --image-scanning-configuration scanOnPush=true \
    --tags Key=Environment,Value=test

3.2 Build and Push Test Images

#!/bin/bash
# push-test-images.sh

# Set variables
AWS_REGION="your-region"
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)

# Login to ECR
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com

# Build and push images with test tag
docker-compose -f docker-compose.test.yml build
docker-compose -f docker-compose.test.yml push

4. Test Environment Task Definition

Create a new task definition for the test environment:

{
    "family": "test-app",
    "networkMode": "awsvpc",
    "containerDefinitions": [
        {
            "name": "app",
            "image": "${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/node-app:test",
            "essential": true,
            "portMappings": [
                {
                    "containerPort": 3000,
                    "protocol": "tcp"
                }
            ],
            "environment": [
                {
                    "name": "NODE_ENV",
                    "value": "test"
                }
            ],
            "logConfiguration": {
                "logDriver": "awslogs",
                "options": {
                    "awslogs-group": "/ecs/test/app",
                    "awslogs-region": "${AWS_REGION}",
                    "awslogs-stream-prefix": "test"
                }
            }
        }
    ],
    "requiresCompatibilities": [
        "FARGATE"
    ],
    "cpu": "256",
    "memory": "512"
}

5. Deployment Steps

  1. Deploy the infrastructure:
aws cloudformation create-stack \
    --stack-name test-environment \
    --template-body file://test-environment.yaml \
    --parameters ParameterKey=VpcId,ParameterValue=your-vpc-id
  1. Create repositories and push images:
./create-test-repos.sh
./push-test-images.sh
  1. Register task definition:
aws ecs register-task-definition --cli-input-json file://task-definition.json
  1. Create test service:
aws ecs create-service \
    --cluster test-cluster \
    --service-name test-service \
    --task-definition test-app:1 \
    --desired-count 1 \
    --launch-type FARGATE \
    --network-configuration "awsvpcConfiguration={subnets=[subnet-1,subnet-2],securityGroups=[sg-xxx],assignPublicIp=ENABLED}"

6. Testing Workflow

  1. Update test images:
# Build and tag new version
docker-compose -f docker-compose.test.yml build
docker-compose -f docker-compose.test.yml push

# Update ECS service
aws ecs update-service --cluster test-cluster --service test-service --force-new-deployment
  1. View logs:
aws logs get-log-events \
    --log-group-name /ecs/test/app \
    --log-stream-name test/app/[container-id]

7. Cleanup

# Delete service
aws ecs delete-service --cluster test-cluster --service test-service --force

# Delete stack
aws cloudformation delete-stack --stack-name test-environment

# Delete ECR repositories
aws ecr delete-repository --repository-name node-app --force
aws ecr delete-repository --repository-name postgres-db --force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment