DevSecOps Lab Infrastructure Setup Guide on AWS
Estimated Cost: ~$1-2/day
This guide provides detailed instructions for setting up the necessary infrastructure on AWS to support the CI/CD labs in our curriculum. By following this guide, educators and learners will be able to prepare a robust environment for hands-on practice with continuous integration and continuous delivery.
Prerequisites
Before setting up the infrastructure for the CI/CD labs on AWS, ensure that you have the following tools installed and configured on your machine. These tools are necessary for interacting with AWS services and for deploying and managing the infrastructure.
Software Requirements
Name | Purpose | Installation Guide |
---|---|---|
AWS CLI | To interact with Amazon Web Services. | Guide Link |
Terraform | To provision AWS Infrastructure consistently and programmatically. | Guide Link |
Git | To clone the infrastructure scripts. | Guide Link |
WireGuard Client | To access internal services. | Guide Link |
Other Requirements
Active AWS Account: You need an active AWS account to provision AWS services. You will be billed accordingly for the AWS resources utilized during the labs.
Configuring AWS CLI
Obtain Access Credentials
- Log into your AWS Management Console.
- Navigate to
IAM > Users
. - Click
Create user
. - Enter the desired user name, click next, and attach the
AdministratorAccess
policy. - After creation, click on the new user and navigate to the
Security credentials
tab, and clickCreate access key
. - Generate a new access key for the Command Line Interface user case. Make sure to save these credentials securely.
Configure the AWS CLI
- Open your terminal.
- Run the following command:
aws configure
- Enter the Access Key ID and Secret Access Key when prompted.
- Specify the default region (e.g.,
us-west-1
). This should be the region where you will deploy the resources.
Setting Up The Infrastructure With Terraform
With the AWS CLI configured, the next step is to set up the actual lab infrastructure using Terraform. Terraform will allow you to automate the deployment of all required AWS resources.
Clone The Infrastructure Setup Scripts
- Open your terminal.
- Run the following command to clone the lab infrastructure setup repository. This repository contains all the necessary Terraform scripts for various lab topics.
git clone https://github.com/open-devsecops/lab-infra-setup.git
- Change into the directory containing the Terraform scripts for Topic 3 DevOps lab:
cd lab-infra-setup/topic-3-devsecops/aws
Initialize Terraform
- Within the Topic 3 - DevSecOps directory, initialize Terraform to install necessary providers and set up your environment.
- In the terminal, enter the following:
terraform init
- Before applying any changes, review what Terraform intends to do. This command will show you a list of resources that Terraform plans to create.
terraform plan
- Apply the configuration to begin provisioning the AWS resources.
terraform apply
-
When prompted to
Enter a value:
, enteryes
. - After Terraform successfully applies the configuration, it will output important information such as public IPs, or other commands and other data needed to access your resources.
Output Name | Description | Usage |
---|---|---|
SSH | SSH command to access the EC2 instance. | Use this command to SSH into the EC2 instance for administrative tasks or troubleshooting. |
ec2_public_ip | The public IP address of the EC2 instance. | Needed to access various web interfaces for the lab, such as downloading VPN configurations, accessing Jenkins, etc. |
Please allow sufficient time for tools to install. After Terraform successfully provisions the AWS resources, it typically takes about 5 minutes for all software tools to be fully installed and operational on the provisioned resources. You can verify completion by entering in the terminal [ssh command] -f "grep 'Lab Infrastructure Provisioning Complete' /var/log/cloud-init-output.log"
.
Example Output
aws_subnet.lab_public_subnet: Creation complete after 1s
aws_route_table.lab_public_route_table: Creation complete after 1s
aws_route_table_association.lab_pub_sub_rt: Creating...
aws_route_table_association.lab_pub_sub_rt: Creation complete after 1s
aws_security_group.lab: Creation complete after 2s
aws_instance.topic-2-lab: Creating...
aws_instance.topic-2-lab: Still creating... [10s elapsed]
aws_instance.topic-2-lab: Creation complete after 13s
Apply complete! Resources: 16 added, 0 changed, 0 destroyed.
Outputs:
SSH = "ssh -i topic-2-cicd-lab-key.pem ubuntu@54.176.55.245"
ec2_public_ip = "54.176.55.245"
Accessing Internal Services
Use The VPN Config Generator
- Navigate to the VPN Config Generator at
https://{ec2_public_ip}
. Replace{ec2_public_ip}
with the actual public IP address output by Terraform. - Download the VPN Configuration file
- Import the VPN Configuration file into your WireGuard client.
- Activate the VPN connection using WireGuard to securely connect to the internal network.
- Access internal services such as
http://dashboard.internal
orhttp://jenkins.internal
.
Configuring Jenkins
Once your infrastructure is ready and you have connected to the internal network via VPN, you can proceed to set up Jenkins for the DevOps labs.
- Navigate to
http://jenkins.internal
in your web browser. - To unlock Jenkins and begin setup, you need the initial admin password. Replace
{ec2_public_ip}
in the command below with the public ip in the terraform output.
ssh -i topic-3-devsecops-lab-key.pem ubuntu@{ec2_public_ip} -f "sudo docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword"
Make sure you are in the topic-3-devsecops/aws
directory where the SSH key is located before you enter the command.
-
Enter the modified command to retrieve the pasword.
-
Back in your web browser on the Jenkins unlock page, enter the initial admin password you retrieved to unlock
-
Select the option to Install suggested plugins.
- Once the plugin installation is complete, proceed to the Create First Admin User step.
- Fill out the form with the admin username, password.
- On the Instance Configuration page, ensure the Jenkins URL is set to http://jenkins.internal/. This should be populated automatically.
- Click Save and Finish.
Setting Up Jenkins
Creating Student Account
Finally, let’s set up a student account that has the necessary permissions to create and manage pipelines but does not possess full administrative rights.
- Click on Manage Jenkins from the main menu on the left.
- Access
Security > Users
- Click on Create User to set up a new account.
- Return to Manage Jenkins and select
Security > Security
. - Scroll to the Authorization section.
- Select “Matrix-based security” from the list of Authorization strategies.
- Click Add user.
- Enter the username of the student account you created.
- Configure the permissions for the student account as follows and click on “Save” to apply the changes.
(Optional) Installing BlueOcean Plugin
BlueOcean improves the user experience of Jenkins, providing a more visual and intuitive approach to pipeline creation and management.
- Go back to the Manage Jenkins page and select Manage Plugins.
- Switch to the Available tab and use the search bar to find
Blue Ocean
. - Check the box next to Blue Ocean
- Click on Install to begin installing the selected plugins.
Security Tools Overview
Here are the security tools installed and configured for the lab:
Tool | Purpose | Configuration Required |
---|---|---|
SonarQube | Static Application Security Testing (SAST) | Manual configuration in SonarQube and Jenkins. |
Trivy | Container image scanning | Pre-installed, no manual configuration required. |
Ensure that you are connected to the internal network via VPN using the VPN configuration file from the previous steps.
Configuring SonarQube
SonarQube comes pre-installed, but some manual configuration is needed to integrate the SAST tool into the Jenkins pipeline that students will use for Topic 3 - DevSecOps labs.
Generating Access Token
- Navigate to
http://sonarqube.internal
- Use the default credentials to log in.
- Username:
admin
- Password:
admin
- Username:
- After logging in for the first time, you will be prompted to change the admin password.
- Click on the profile icon (top-right corner) and select
My Account
. - Navigate to the
Security
tab. - Enter a name for the token, select
User Token
as the type, and click Generate. - Copy the generated token. This token will be used to configure SonarQube in Jenkins.
Creating Student Account
To allow students to read the project reports in SonarQube, we need to create a new user account with the appropriate permissions.
- In SonarQube, go to the
Administration
tab. - Click on
Security
in the navigation bar, then selectUsers
. - Click on the
Create User
button. - Enter in the details and click
Create
.- Login: student
- Name: Student
- Password: student1!
Creating a New Quality Gate
A quality gate is a set of conditions that is used to ensure that the code meets certain standards before it is allowed to proceed through the CI/CD pipeline. The default Sonar way
quality gate focuses only on new code, which means even if new issues and security hotspots are found, the project will pass, and the pipeline will continue to the next step.
The purpose of this lab is for the students to utilize these tools to fix these issues until all issues and hotspots have been addressed.
We need to create a new quality gate that includes conditions on Overall Code
, ensuring that the project fails if there are any issues or if all security hotspots have not been reviewed.
- In SonarQube, go to the
Quality Gates
tab. - Click on
Create
to create a new quality gate. - Click
Unlock Editing
. - Add the following conditions for Overall Code:
- Issues: is greater than 0
- Security hotspots reviewed: is less than 100%
- To make this new quality gate the default, click on the vertical ellipsis icon beside the quality gate name and select
Set as Default
.
SonarQube and Jenkins Integration
- In
http://jenkins.internal
, go toManage Jenkins > Manage Plugins
. - Switch to the Available tab and search for
SonarQube Scanner
. - After installing the plugin, go to
Manage Jenkins > System
. - Scroll down to the SonarQube servers section and click
Add SonarQube
. - Enter the following details:
- Name:
sonarqube
(This specific name is important because it is used in the Jenkinsfile within the reference application that the students use to go through the lab exercise) - Server URL:
http://sonarqube.internal
- Server authentication token: Use the token you generated from SonarQube.
- Click
Add > Jenkins
(If the Jenkins button does not appear when you click Add for the authentication token, try saving the configuration first, and then revisiting the SonarQube servers section). - Change kind to
Secret text
. - Paste in the access token from the previous step into
Secret
- Enter in the id.
- Click
Add
. - Select the Jenkins credentials you just generated in
Server authentication token
- Click
- Name:
- Click
Save
to save the configuration.