# 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
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:
#!/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
#!/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
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"
}
- Deploy the infrastructure:
aws cloudformation create-stack \
--stack-name test-environment \
--template-body file://test-environment.yaml \
--parameters ParameterKey=VpcId,ParameterValue=your-vpc-id
- Create repositories and push images:
./create-test-repos.sh
./push-test-images.sh
- Register task definition:
aws ecs register-task-definition --cli-input-json file://task-definition.json
- 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}"
- 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
- View logs:
aws logs get-log-events \
--log-group-name /ecs/test/app \
--log-stream-name test/app/[container-id]
# 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