|
| 1 | +--- |
| 2 | +description: Use Trivy for static analysis of a Nitric project deployed with Terraform |
| 3 | +tags: |
| 4 | + - Terraform |
| 5 | + - Testing |
| 6 | +published_at: 2025-01-09 |
| 7 | +--- |
| 8 | + |
| 9 | +# Static analysis of Terraform with Trivy |
| 10 | + |
| 11 | +This guide will walk you through generating a report with [Trivy](https://aquasecurity.github.io/trivy/) from a Nitric project. |
| 12 | + |
| 13 | +## How Trivy works |
| 14 | + |
| 15 | +[Trivy](https://aquasecurity.github.io/trivy/) is a comprehensive security scanner that supports scanning file systems, Git repositories, and container images. It also includes a “config” scanning feature that checks IaC (Infrastructure as Code) files like Terraform for misconfigurations, security issues, and vulnerabilities. |
| 16 | + |
| 17 | +This guide assumes that you have already [installed Trivy](https://aquasecurity.github.io/trivy/v0.35/getting-started/installation/) by following their installation guide. |
| 18 | + |
| 19 | +## What we'll be doing |
| 20 | + |
| 21 | +1. Create and set up your application. |
| 22 | +2. Deploying to AWS with a Terraform provider. |
| 23 | +3. Run Trivy. |
| 24 | + |
| 25 | +## Create and set up your application |
| 26 | + |
| 27 | +Trivy can be used with any Nitric project that you intend to deploy with Terraform. We'll be using a basic starter template in this guide; however, you can use your own Nitric project or an [example project](https://github.com/nitrictech/examples). |
| 28 | + |
| 29 | +Let's start by creating a new project from a Nitric template. This will provide a base for building the API: |
| 30 | + |
| 31 | +```bash |
| 32 | +nitric new my-profile-api ts-starter |
| 33 | +``` |
| 34 | + |
| 35 | +Next, open the project in your editor of choice and make sure all dependencies are resolved: |
| 36 | + |
| 37 | +Using NPM: |
| 38 | + |
| 39 | +```bash |
| 40 | +npm install |
| 41 | +``` |
| 42 | + |
| 43 | +You can test the project to verify everything is working as expected: |
| 44 | + |
| 45 | +```bash |
| 46 | +nitric start |
| 47 | +``` |
| 48 | + |
| 49 | +## Deploying to AWS with a Terraform provider |
| 50 | + |
| 51 | +To deploy your application with Terraform, you'll need to use Nitric’s Terraform providers. You can learn more about using Nitric with Terraform [here](/providers/terraform). |
| 52 | + |
| 53 | +```bash |
| 54 | +nitric stack new dev aws-tf |
| 55 | +``` |
| 56 | + |
| 57 | +Update this newly created stack file to include your target region: |
| 58 | + |
| 59 | +```yaml title:nitric.dev.yaml |
| 60 | +# The nitric provider to use |
| 61 | +provider: nitric/[email protected] |
| 62 | + |
| 63 | +# The target AWS region to deploy to |
| 64 | +region: us-east-2 |
| 65 | +``` |
| 66 | +
|
| 67 | +The Nitric Terraform providers are currently in preview. To enable them, you'll need to enable beta-providers in your Nitric project. You can do this by adding the following to your project's `nitric.yaml` file: |
| 68 | + |
| 69 | +```yaml title:nitric.yaml |
| 70 | +preview: |
| 71 | + - beta-providers |
| 72 | +``` |
| 73 | + |
| 74 | +Once you've created your stack file, you can generate the Terraform code by running the following command: |
| 75 | + |
| 76 | +```bash |
| 77 | +nitric up |
| 78 | +``` |
| 79 | + |
| 80 | +This will generate Terraform code that can deploy your application. The output will be in a folder named `cdktf.out` by default. |
| 81 | + |
| 82 | +## Run Trivy |
| 83 | + |
| 84 | +Trivy’s config scanning feature can analyze your Terraform files for possible misconfiguration. |
| 85 | + |
| 86 | +```bash |
| 87 | +cd cdktf.out/stacks/my-profile-api-dev |
| 88 | +trivy config . |
| 89 | +``` |
| 90 | + |
| 91 | +## Analyzing the results |
| 92 | + |
| 93 | +Trivy comes with a variety of checks for common security and misconfiguration issues. Some findings might not always be relevant to your deployment scenario. For example, if Trivy flags a missing KMS key reference for encryption, it might be because your ECR repository is already encrypted by default with SSE-S3, and you haven’t explicitly configured a KMS key. In such cases, it’s worth reviewing the findings to see if they apply to your use case. |
| 94 | + |
| 95 | +If you have any concerns, please don't hesitate to [reach out](https://nitric.io/chat). |
0 commit comments