<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Yuncheng Yang</title>
    <description>The latest articles on Forem by Yuncheng Yang (@fabioyyc).</description>
    <link>https://forem.com/fabioyyc</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F455817%2Fb4fb8e27-f0ab-43de-ac37-e5e21d9eb5ca.jpeg</url>
      <title>Forem: Yuncheng Yang</title>
      <link>https://forem.com/fabioyyc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/fabioyyc"/>
    <language>en</language>
    <item>
      <title>Testing your Infrastructure in CI/CD Pipeline</title>
      <dc:creator>Yuncheng Yang</dc:creator>
      <pubDate>Wed, 08 Mar 2023 06:39:25 +0000</pubDate>
      <link>https://forem.com/stackql/testing-your-infrastructure-in-cicd-pipeline-7ig</link>
      <guid>https://forem.com/stackql/testing-your-infrastructure-in-cicd-pipeline-7ig</guid>
      <description>&lt;h2&gt;
  
  
  Testing Infrastructure ad Code
&lt;/h2&gt;

&lt;p&gt;These days, everyone's using Infrastructure as Code (IaC) to manage their infrastructure.&lt;/p&gt;

&lt;p&gt;🛠️ Some popular Infrastructure as Code (IaC) tools include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Terraform&lt;/strong&gt;: Open-source IaC tool for managing infrastructure across multiple cloud platforms and providers. Flexible and reliable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;AWS CloudFormation&lt;/strong&gt;: IaC tool to manage and automate AWS resources. Allows previewing changes before deployment and rolling back infrastructure if needed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Azure Resource Manager&lt;/strong&gt;: Robust IaC tool for managing infrastructure using ARM templates. Supports RBAC and provides excellent organization tools.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Google Cloud Deployment Manager&lt;/strong&gt;: Native infrastructure deployment service for Google Cloud Platform. Uses declarative language and offers built-in console and UI support.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pulumi&lt;/strong&gt;: Flexible IaC tool that supports various programming languages. Allows for automation options for deployment delivery, quality assurance, easy auditing, and comprehensive identity control.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Ansible&lt;/strong&gt;: Red Hat's orchestration and configuration tool that uses YAML-based playbooks to configure infrastructure. Easy to use with robust default configuration and allows for custom modules.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It's great because it's &lt;strong&gt;scalable, automated, and ensures consistency across environments&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But when it comes to testing that IaC, a lot of us fall back on the old "deploy and see" method: deploying your infrastructure and then checking to see if it's actually doing what it's supposed to be doing.&lt;br&gt;
It's a time-consuming and error-prone process, and it's not enough to catch all the potential issues. Especially when you're dealing with complex infrastructures with multiple environments.&lt;/p&gt;

&lt;p&gt;To make the process more efficient and reliable,** you can create tests for infrastructure that can be run with a CI/CD pipeline.**&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Standardize the deployment environment&lt;/li&gt;
&lt;li&gt;Automate many of the manual tasks involved in testing and deployment&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Use 🌟SQL🌟 with Cloud
&lt;/h3&gt;

&lt;p&gt;If you prefer not to learn and use a domain-specific language to manage resources on the cloud, there are several tools available.&lt;/p&gt;

&lt;p&gt;Using SQL also enables the use of relational algebra concepts and operations, such as joins, selections, and projections.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Steampipe&lt;/strong&gt;: a Postgres-based open-source tool that uses foreign data wrappers and SDK-based provider plugins to query your cloud infrastructure using SQL. Steampipe supports multiple cloud providers, including AWS, Azure, and Google Cloud Platform.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CloudQuery&lt;/strong&gt;: an open-source tool that collects inventory and configuration data from cloud providers using the provider SDKs. CloudQuery allows you to query your cloud infrastructure using SQL. CloudQuery supports multiple cloud providers, including AWS, Azure, Google Cloud Platform, and Kubernetes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;IaSQL&lt;/strong&gt;: a Postgres-based tool that allows you to query and provision resources in AWS.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;StackQL&lt;/strong&gt;: an open-source query engine that allows you to query, provision, and interact with cloud and SaaS resources using SQL. &lt;br&gt;
StackQL cloud provider interfaces are extensions of the provider's OpenAPI specification, exposing all operations available in a provider's API using SQL grammar. StackQL's document-based provider definitions make bringing your providers or APIs, and extending to other cloud or SaaS platforms easy.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Testing tools
&lt;/h2&gt;

&lt;p&gt;There are several IaC testing tools available, including &lt;strong&gt;Terratest&lt;/strong&gt;, &lt;strong&gt;Kitchen-Terraform&lt;/strong&gt;, and &lt;strong&gt;Chef InSpec&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;These tools can help automate the process of testing your infrastructure code and catch issues.&lt;/p&gt;
&lt;h3&gt;
  
  
  Terratest
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://terratest.gruntwork.io/"&gt;Terratest&lt;/a&gt; is a Go library that provides patterns and helpers for testing infrastructure.&lt;/p&gt;

&lt;p&gt;Terratest allows you to execute your real IaC tools (Terraform, Packer, etc.) to deploy real infrastructure in a real environment.&lt;br&gt;
You can then use the tools built into Terratest to validate that the infrastructure works correctly in that environment by making HTTP requests, API calls, SSH connections, etc. Terratest can undeploy everything at the end of the test.&lt;/p&gt;

&lt;p&gt;Here's an example of how to use Terratest to test a Terraform module:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package test

import (
  "fmt"
  "testing"

  "github.com/gruntwork-io/terratest/modules/terraform"
  "github.com/stretchr/testify/assert"
)

func TestTerraformModule(t *testing.T) {
  terraformOptions := &amp;amp;terraform.Options{
    TerraformDir: "../examples/simple",
  }

  defer terraform.Destroy(t, terraformOptions)

  terraform.InitAndApply(t, terraformOptions)

  output := terraform.Output(t, terraformOptions, "output_name")
  assert.Equal(t, "expected_output", output)
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This test will run Terraform to create the infrastructure, then check that the output matches the expected output.&lt;/p&gt;

&lt;h3&gt;
  
  
  Chef InSpec
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.inspec.io/"&gt;Chef InSpec&lt;/a&gt; is an open-source testing and compliance automation framework. It allows you to write tests for your infrastructure code and check that it meets the desired state.&lt;/p&gt;

&lt;p&gt;You can use InSpec to test various infrastructure components, such as operating systems, cloud providers, and databases. InSpec can also test compliance with industry standards like PCI DSS, HIPAA, and CIS benchmarks.&lt;/p&gt;

&lt;p&gt;Here's an example of how to use Chef InSpec to test a Terraform module:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;control "example-control" do
  title "Example Control"
  desc "An example InSpec control"

  describe aws_instance('example-instance') do
    it { should be_running }
    its('instance_type') { should eq 't2.micro' }
  end

  describe aws_security_group('example-security-group') do
    it { should allow_in(port: 22, ipv4_range: '0.0.0.0/0') }
  end
end

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This test will check that an AWS EC2 instance named &lt;code&gt;example-instance&lt;/code&gt; is running and is of the correct instance type, and that the security group &lt;code&gt;example-security-group&lt;/code&gt; allows incoming traffic on port 22 from any IP address.&lt;/p&gt;

&lt;h3&gt;
  
  
  Kitchen-Terraform
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/newcontext-oss/kitchen-terraform"&gt;Kitchen-Terraform&lt;/a&gt; is a Ruby-based test harness for Terraform modules.&lt;/p&gt;

&lt;p&gt;Kitchen-Terraform allows automated testing of Terraform code, including syntax and linting checks.&lt;br&gt;
It can also create and destroy infrastructure in a testing environment and run tests against it. It can be integrated with Chef InSpec to perform additional testing of infrastructure code for a comprehensive testing process.&lt;/p&gt;

&lt;p&gt;Here's an example of how to use Kitchen-Terraform to test a Terraform module:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
driver:
  name: terraform

provisioner:
  name: terraform

verifier:
  name: terraform

platforms:
  - name: terraform

suites:
  - name: default
    verifier:
      rakefile: test/integration/default/default_test.rb

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This test will create a Terraform instance and apply the Terraform configuration in the &lt;code&gt;default&lt;/code&gt; suite. After that, it will run the tests in &lt;code&gt;default_test.rb&lt;/code&gt;, which can include any number of tests.&lt;/p&gt;

&lt;h3&gt;
  
  
  GitHub Action: stackql-assert
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://github.com/marketplace/actions/stackql-studios-stackql-assert"&gt;stackql-assert&lt;/a&gt; is a GitHub action that allows you to run a StackQL query with minimal setup.&lt;br&gt;
It is used to test assertions against the results of a StackQL query, which can validate the state of a resource after an IaC or lifecycle operation has been performed, or validate the system (e.g., CSPM or compliance queries).&lt;/p&gt;

&lt;p&gt;For example, you can use the stackql-assert action in a Terraform deployment pipeline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- name: Terraform Apply
        env:
          TF_VAR_google_credentials: ${{ secrets.GOOGLE_CREDS }}
        id: apply
        run: cd terraform; terraform apply -no-color -var-file=stackql-demo.tfvars -auto-approve

- name: check terraform deployment using stackql-assert - should fail
  uses: stackql/stackql-assert@v1.0.2
  with:
    auth_obj_path: './stackql/auth.json'
    test_query_file_path: './stackql/scripts/check-terraform-instances.iql'
    expected_results_str: '[{"name":"terraform-test-1","name":"terraform-test-2"}]'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example run a StackQL &lt;code&gt;SELECT&lt;/code&gt; query and compare the actual result with an expected result after a Terraform deployment.&lt;br&gt;
This can test specific configuration properties of the resource (for compliance or policy enforcement) or just the existence of the resource.&lt;/p&gt;

&lt;h2&gt;
  
  
  Read more
&lt;/h2&gt;

&lt;p&gt;Read more about &lt;a href="https://stackql.io/docs"&gt;StackQL&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Reference
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://bluelight.co/blog/best-infrastructure-as-code-tools#what-are-the-best-infrastructure-as-code-tools-for-2022"&gt;https://bluelight.co/blog/best-infrastructure-as-code-tools#what-are-the-best-infrastructure-as-code-tools-for-2022&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://qxf2.com/blog/testing-infrastructure-as-code-beginner/"&gt;https://qxf2.com/blog/testing-infrastructure-as-code-beginner/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>terraform</category>
      <category>aws</category>
      <category>stackql</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
