During App Modernization journey, a code visualization system is required for code quality management because many changes are needed. SonarQube, the most famous and used solution, is an open-source platform developed by SonarSource for continuous inspection of code quality. This project provides a solution including IaC code and configurations to run SonarQube on a container with minimum cost.
- Build a continuous inspection of code quality on Fargate with SonarQube and CDK in 10 minutes
- Running SonarQube with minimum cost using Fargate Spot and Aurora Serverless.
- Help developers keep high-quality code using SonarQube
I hope you build and learn about code inspection/visualization systems using SonarQube in 1 day only.
- Deploy VPC stack
- Deploy ECS Fargate cluster stack
- Deploy IAM Role stack
- Deploy ECR and CodeCommit repository stack
- Deploy ECS Fargate Service stack
- Create a Sonarqube Token
- Run SonarQube Scanner
SonarQube version: 8.9.9-community
npm install -g aws-cdk@2.32.1
npm install -g cdk-ecr-deployment@2.5.5
# install packages in the root folder
npm install
cdk bootstrap
Use the cdk
command-line toolkit to interact with your project:
cdk deploy
: deploys your app into an AWS accountcdk synth
: synthesizes an AWS CloudFormation template for your appcdk diff
: compares your app with the deployed stackcdk watch
: deployment every time a file change is detected
Stack | Time To Complete | |
---|---|---|
1 | VPC | 3m 30s (optional) |
2 | ECS Fargate cluster | 50s |
3 | IAM roles | 1m |
4 | ECR and CodeCommit repository | 1m |
5 | ECS Fargate Service and ALB | 4m |
Total | 7m (10m 30s with a new VPC) |
Use the deploy-all.sh file if you want to deploy all stacks without prompt at a time.
Deploy a new VPC:
cd vpc
cdk deploy
The VPC ID will be saved into the SSM Parameter Store(/sonarqube-fargate-cdk/vpc-id
) to refer from other stacks.
To use the existing VPC, use the -c vpcId
context parameter or create SSM Parameter:
aws ssm put-parameter --name "/sonarqube-fargate-cdk/vpc-id" --value "{existing-vpc-id}" --type String
cd ../ecs-sonarqube-cluster
cdk deploy
# or define your VPC id with context parameter
cdk deploy -c vpcId=<vpc-id>
SSM parameter:
- /sonarqube-fargate-cdk/vpc-id
Cluster Name: config.ts
ecs-sonarqube-cluster/lib/sonarqube-cluster-stack.ts
Create the ECS Task Execution role and default Task Role.
- AmazonECSFargateTaskExecutionRole
- ECSFargateDefaultTaskRole including a policy for ECS Exec
cd ../ecs-iam-role
cdk deploy
ecs-iam-role/lib/ecs-iam-role-stack.ts
cd ../ecr-codecommit
cdk deploy --outputs-file ./cdk-outputs.json
cat ./cdk-outputs.json | jq .
Crearte a Fargate Service, Auto Scaling, ALB, and Log Group.
cd ../ecs-sonarqube-service
cdk deploy --outputs-file ./cdk-outputs.json
cat ./cdk-outputs.json | jq .
e.g.,
{
"ecs-sonarqube-fargate-dev": {
"TaskDefinition": "sonarqube-task",
"LogGroup": "sonarqube",
"ALB": "alb-sonarqube-123456789.ap-northeast-2.elb.amazonaws.com",
"Service": "arn:aws:ecs:ap-northeast-2:123456789:service/sonarqube-fargate-dev/sonarqube"
}
}
SSM parameters:
- /sonarqube-fargate-cdk/vpc-id
- /sonarqube-fargate-cdk/cluster-securitygroup-id
- /sonarqube-fargate-cdk/task-execution-role-arn
- /sonarqube-fargate-cdk/default-task-role-arn
ecs-sonarqube-service/lib/sonarqube-fargate-stack.ts
IMPORTANT
If the ECS cluster was re-created, you HAVE to deploy after cdk.context.json files deletion with the below:
find . -name "cdk.context.json" -exec rm -f {} \;
To connect to Sonarqube URL, refer to the ecs-sonarqube-service/cdk-outputs.json
file.
- Sign in with admin/admin and change the password.
- Create a Token on Administration > Security > Users menu.
Run SonarQube Scanner using gradle wrapper:
./gradlew sonar -Dsonar.login="{your-sonarqube-token}" -Dsonar.host.url="{alb-endpoint}"
# e.g.,
# gradle sonar -Dsonar.login="bd853394582391ee93c5efff61c2b53e356b1eb7d" -Dsonar.host.url="http://alb-sonarqube-123456789012.ap-northeast-2.elb.amazonaws.com"
If you want to integrate with Jenkins using Webhooks, refer to the https://github.com/DevSecOpsSamples/sonarqube-jenkins repository.
├── build.gradle
├── deploy-all.sh
├── cleanup.sh
├── config.ts
├── package.json
├── tsconfig.json
├── app
│ ├── Dockerfile
│ └── build.sh
├── ecr-codecommit
│ ├── bin
│ │ └── index.ts
│ ├── cdk.json
│ └── lib
│ └── ecr-codecommit-stack.ts
├── ecs-iam-role
│ ├── bin
│ │ └── index.ts
│ ├── cdk.json
│ └── lib
│ └── ecs-iam-role-stack.ts
├── ecs-sonarqube-cluster
│ ├── bin
│ │ └── index.ts
│ ├── cdk.json
│ ├── jest.config.js
│ └── lib
│ └── sonarqube-cluster-stack.ts
├── ecs-sonarqube-service
│ ├── bin
│ │ └── index.ts
│ ├── cdk.json
│ └── lib
│ └── sonarqube-fargate-stack.ts
└── vpc
├── bin
│ └── index.ts
├── cdk.json
└── lib
└── vpc-stack.ts