In this section, you will set up a complete CI/CD pipeline using AWS CodePipeline, CodeBuild, and GitLab CI/CD to automate the build and deployment process for both backend and frontend applications.
GitLab → S3 (Artifacts) → CodePipeline → CodeBuild (Backend) → EC2
↓
CodeBuild (Frontend) → S3/CloudFront
In your GitLab project, go to Settings → CI/CD → Variables and add:
| Variable | Value | Protected | Masked |
|---|---|---|---|
AWS_ACCESS_KEY_ID | Your AWS Access Key | ✅ | ✅ |
AWS_SECRET_ACCESS_KEY | Your AWS Secret Key | ✅ | ✅ |
The .gitlab-ci.yml file in your project root:
stages:
- deploy
deploy-to-aws:
stage: deploy
image: amazon/aws-cli:latest
before_script:
- |
aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
aws configure set region ap-southeast-1
script:
- echo "📦 Creating source.zip..."
- |
apk add zip
zip -r source.zip . \
-x "*.git*" \
-x "node_modules/*" \
-x ".idea/*" \
-x "target/*" \
-x "*.zip"
- echo "📤 Uploading to S3..."
- |
aws s3 cp source.zip \
s3://workshop-aws-dev-artifacts-502310717700-ap-southeast-1/source.zip
- echo "✅ Upload completed! CodePipeline will trigger automatically."
only:
- main
when: on_success
What it does:
source.zipThe cicd-pipeline.yaml CloudFormation template creates:
aws cloudformation create-stack \
--stack-name workshop-aws-dev-cicd \
--template-body file://aws/cicd-pipeline.yaml \
--parameters \
ParameterKey=ProjectName,ParameterValue=workshop-aws \
ParameterKey=Environment,ParameterValue=dev \
ParameterKey=SourceProvider,ParameterValue=S3 \
ParameterKey=ArtifactBucketName,ParameterValue=workshop-aws-dev-artifacts-502310717700-ap-southeast-1 \
--capabilities CAPABILITY_NAMED_IAM \
--region ap-southeast-1
aws cloudformation wait stack-create-complete \
--stack-name workshop-aws-dev-cicd \
--region ap-southeast-1
Expected time: 3-5 minutes
CodeBuild uses buildspec-backend.yml:
version: 0.2
phases:
pre_build:
commands:
- echo "Installing Maven..."
- yum install -y maven
build:
commands:
- echo "Building Backend JAR..."
- cd BE/workshop_BE
- mvn clean package -DskipTests
post_build:
commands:
- echo "Uploading JAR to S3..."
- aws s3 cp target/workshop-0.0.1-SNAPSHOT.jar \
s3://workshop-aws-dev-backend-502310717700-ap-southeast-1/jars/
- echo "Deploying to EC2..."
- aws ssm send-command \
--instance-ids i-09fdbf7739ee37b32 \
--document-name "AWS-RunShellScript" \
--parameters commands=[
"cd /opt/workshop",
"aws s3 cp s3://workshop-aws-dev-backend-502310717700-ap-southeast-1/jars/workshop-0.0.1-SNAPSHOT.jar .",
"sudo systemctl restart workshop.service"
]
artifacts:
files:
- '**/*'
CodeBuild uses buildspec-frontend.yml:
version: 0.2
phases:
pre_build:
commands:
- echo "Installing Node.js..."
- curl -sL https://rpm.nodesource.com/setup_18.x | bash -
- yum install -y nodejs
build:
commands:
- echo "Building Frontend..."
- cd FE
- npm install
- npm run build
post_build:
commands:
- echo "Deploying to S3..."
- aws s3 sync dist/ \
s3://workshop-aws-dev-frontend-502310717700-ap-southeast-1/ \
--delete
- echo "Invalidating CloudFront..."
- aws cloudfront create-invalidation \
--distribution-id E3K48K7CPOOLHZ \
--paths "/*"
artifacts:
files:
- 'FE/dist/**/*'
Make a code change and push to main branch:
git add .
git commit -m "Test CI/CD pipeline"
git push origin main
deploy-to-aws jobworkshop-aws-dev-pipelinesource.zip in S3For detailed logs:
curl https://98385v3jef.execute-api.ap-southeast-1.amazonaws.com/dev/dna_service/actuator/health
Expected response:
{"status":"UP"}
Open browser and navigate to:
https://d3gmmg22uirq0t.cloudfront.net
Verify the application loads with latest changes.
Step-by-step flow:
Issue: CodePipeline doesn’t start after GitLab push
Solutions:
source.zipIssue: CodeBuild backend fails with Maven errors
Solutions:
buildspec-backend.yml syntaxpom.xmlIssue: CodeBuild frontend fails with npm errors
Solutions:
buildspec-frontend.yml syntaxpackage.json dependenciesIssue: Build succeeds but deployment fails
Solutions:
.m2, npm node_modules)You have successfully set up a complete CI/CD pipeline that:
✅ Automatically builds and packages code from GitLab
✅ Uploads artifacts to S3
✅ Triggers AWS CodePipeline on changes
✅ Builds backend JAR with Maven
✅ Builds frontend with Vite
✅ Deploys backend to EC2
✅ Deploys frontend to S3/CloudFront
✅ Provides monitoring and logging
Next: Clean Up Resources