
Pipeline Guardrails: Automating Security Scans in AWS SAM Deployments
Learn how to enforce Shift-Left security in your CI/CD pipelines. This beginner-friendly guide demonstrates how to integrate SonarCloud and OWASP Dependency-Check into GitHub Actions, blocking vulnerabilities before deploying serverless apps to AWS.
Series: The Shift-Left Serverless Pipeline. (5 articles)
- 2Pipeline Guardrails: Automating Security Scans in AWS SAM Deployments This article
Securing your deployment mechanism is a fantastic first step. If you followed the first part of this series, we eliminated static AWS access keys by configuring OpenID Connect (OIDC) between GitHub Actions and AWS IAM. Your pipeline is now securely authenticated.
But what happens if the application code being deployed contains a critical vulnerability?
A secure pipeline delivering insecure code is a risk we must mitigate. In modern software engineering, we apply DevSecOps principles, specifically, the concept of "Shift-Left" security. Shifting left means catching security flaws, bugs, and dependency vulnerabilities as early in the development lifecycle as possible, ideally the moment a developer opens a Pull Request.
In this article, we will build upon our OIDC foundation. We will introduce automated security gates into our GitHub Actions workflow using industry-standard tools: SonarCloud for Static Application Security Testing (SAST) and OWASP Dependency-Check for Software Composition Analysis (SCA). These tools act as automated code reviewers, ensuring that vulnerabilities are blocked before they ever reach your AWS environment.
To do this securely, we will upgrade our pipeline to a Two-Role OIDC Architecture and learn how to manage third-party secrets cost-effectively using AWS Systems Manager (SSM) Parameter Store.
The DevSecOps Mindset: Why Shift Left?
Before diving into the code, it is essential to understand the "why."
Traditionally, security was treated as a final gateway, a separate team running tests right before a production release. This approach creates stressful bottlenecks and makes fixing vulnerabilities expensive and time-consuming for everyone involved.
By integrating security checks directly into the Continuous Integration (CI) pipeline, we empower developers. If a commit introduces a vulnerability, the pipeline fails immediately. The developer gets instant, clear feedback, fixes the issue, and pushes an update. This iterative process builds a culture where security is designed from the ground up, rather than bolted on at the very end.
Our Security Arsenal
To build our pipeline guardrails, we will rely on two primary types of analysis. For this example, we will look at a Java 21 Spring Boot application built with Gradle, but the concepts apply to any stack:
1. Static Application Security Testing (SAST) with SonarCloud
SAST tools analyze your source code for vulnerabilities, code smells, and bugs without actually executing the program.
We will use SonarCloud (the cloud-hosted version of SonarQube). It continuously inspects code quality and detects complex issues, such as hardcoded passwords, SQL injection vulnerabilities, and deviations from clean code principles.
To run SonarCloud, our pipeline needs a static
SONAR_TOKEN . While we could store this in GitHub Secrets, true DevSecOps relies on centralized secret management.2. Software Composition Analysis (SCA) with OWASP Dependency-Check
Modern applications rely heavily on third-party open-source libraries. Even if your custom code is perfectly secure, a vulnerability in a dependency can compromise your entire application.
SCA tools scan your project's dependencies and compare them against known vulnerability databases. We will use the OWASP Dependency-Check Gradle plugin (version 12.2.1) to flag dependencies with known Common Vulnerabilities and Exposures (CVEs).
Step 1: Store the Token in SSM Parameter Store
First, let's store your SonarCloud token securely in AWS.
- Go to the AWS Management Console and navigate to Systems Manager > Parameter Store.
- Click Create parameter.
- For Name, enter
/my-project/sonar-token.
- For Type, select SecureString.
- For KMS key source, keep the default
alias/aws/ssm(this is managed by AWS and free of storage charges).
- In the Value box, paste your
SONAR_TOKENgenerated from SonarCloud.
- Click Create parameter.
Step 2: Create the Secret-Reader IAM Role
Now we need a role that GitHub Actions can assume just to read this specific parameter. Since we already registered GitHub as an OIDC provider in Part 1, we do not need to do it again! We just create a new role.
- In the IAM Console, navigate to Roles and click Create role.
- Select Web identity.
- Choose the existing
token.actions.githubusercontent.comprovider and selectsts.amazonaws.comas the audience. Fill in your GitHub organization and repository details. Click Next.
- On the Add permissions page, click Create policy (this opens a new tab). Switch to the JSON editor and paste the following least-privilege policy. Replace
YOUR_ACCOUNT_IDandYOUR_REGION:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "ssm:GetParameters", "Resource": "arn:aws:ssm:YOUR_REGION:YOUR_ACCOUNT_ID:parameter/my-project/sonar-token" }, { "Effect": "Allow", "Action": "kms:Decrypt", "Resource": "arn:aws:kms:YOUR_REGION:YOUR_ACCOUNT_ID:key/YOUR_KMS_KEY_ID" } ]}(Note: If you used the default AWS-managed KMS key, you can find its ARN in the KMS console under AWS managed keys > aws/ssm).
- Name the policy
GitHubSonarSecretPolicy, save it, and attach it to your new role.
- Name the role
GitHubSonarSecretReaderRoleand save it.
Step 3: Integrating the Workflow YAML
Here is our updated GitHub Actions workflow. Pay close attention to the order of operations. We authenticate twice, completely separating our read permissions from our deployment permissions.
name: Deploy AWS SAM Application with Security Guardrails
on: push: branches: - main
permissions: id-token: write # Required for requesting the JWT for AWS OIDC contents: read # Required for actions/checkout
jobs: build-scan-and-deploy: runs-on: ubuntu-latest steps: - name: Checkout Code uses: actions/checkout@v6
- name: Setup Java uses: actions/setup-java@v5 with: java-version: '21' distribution: 'temurin'
- name: Cache Packages uses: actions/cache@v5 with: path: | ~/.sonar/cache ~/.gradle/caches key: ${{ runner.os }}-packages
# --- GATE 1: FETCH THE SECRET --- - name: Configure AWS Credentials for Parameter Store uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: arn:aws:iam::YOUR_ACCOUNT_ID:role/GitHubSonarSecretReaderRole aws-region: us-east-1
- name: Get Sonar Token from SSM uses: aws-actions/aws-ssm-getparameters-action@v2 with: parameter-name-list: '/my-project/sonar-token' with-decryption: 'true'
# --- GATE 2: EXECUTE SECURITY SCANS --- # The SSM action automatically exports the parameter as an uppercase environment variable. # /my-project/sonar-token becomes MY_PROJECT_SONAR_TOKEN - name: Code Quality & Dependency Scan env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} SONAR_TOKEN: ${{ env.MY_PROJECT_SONAR_TOKEN }} run: ./gradlew build sonarqube dependencyCheckAnalyze --no-daemon
- name: Upload OWASP Report uses: actions/upload-artifact@v6 if: always() with: name: dependency-check-report path: build/reports/dependency-check-report.html
# --- GATE 3: THE DEPLOYMENT --- # This step ONLY executes if the previous Gradle scan passes. # It overwrites our AWS credentials with our deployment role. - name: Configure AWS Credentials for Deployment uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: arn:aws:iam::YOUR_ACCOUNT_ID:role/GitHubActionsSAMDeployRole aws-region: us-east-1
- name: Setup AWS SAM CLI uses: aws-actions/setup-sam@v2
- name: SAM Build run: sam build
- name: SAM Deploy run: | sam deploy --no-confirm-changeset \ --no-fail-on-empty-changeset \ --stack-name my-serverless-app \ --capabilities CAPABILITY_IAM
Breaking Down the Workflow
The Scanning Step
The command
./gradlew build sonarqube dependencyCheckAnalyze is the heart of our security gate.-
build: Compiles the code and runs our unit and integration tests.
-
sonarqube: Triggers the SonarCloud analyzer. It checks the code against rules for bugs and vulnerabilities, sending the results to your SonarCloud dashboard.
-
dependencyCheckAnalyze: Triggers the OWASP plugin. It downloads the latest vulnerability definitions and cross-references them against your dependencies.
Artifact Archiving
When a pipeline fails due to a security issue, developers need context to fix it quickly. The
upload-artifact step takes the HTML report generated by the OWASP plugin and attaches it to the GitHub Actions run. You can download this report, see exactly which dependency is vulnerable, and update it easily.Breaking Down the "Gates"
The magic of CI/CD is that the runner executes steps sequentially.
- Gate 1: We assume the highly restricted reader role. It grabs the token from AWS Parameter Store and injects it into the environment securely, never exposing it in logs.
- Gate 2: The command
./gradlew build sonarqube dependencyCheckAnalyzeruns. If SonarCloud detects a critical vulnerability in your code, or OWASP detects a CVSS score threshold breach in a dependency, the runner exits with an error and stops immediately.
- Gate 3: The deployment credentials are never requested unless the code is proven clean. If the workflow fails at Gate 2, your AWS infrastructure remains untouched.
Conclusion
By introducing a Two-Role OIDC architecture and utilizing AWS SSM Parameter Store, we centralized our secret management while maintaining strict Shift-Left security. We are detecting vulnerabilities during the build phase, providing immediate feedback to developers, and ensuring that only clean, verified code makes its way to AWS.
In the next installment of this series, we will focus on the runtime environment itself, exploring how to implement strict Least-Privilege IAM roles for AWS Lambda functions to minimize the blast radius if an application is ever compromised. Keep building securely!
Series: The Shift-Left Serverless Pipeline. (5 articles)
- 2Pipeline Guardrails: Automating Security Scans in AWS SAM Deployments This article
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article