SlideShare a Scribd company logo
Compliance as Code
Emre Erkunt
terraform-compliance
> cat who_is_this_person.tf
resource “human_person” “me” {
name = “Emre Erkunt”
interests {
professional = [“DevOps”, “DevSecOps”, “Security”, “Automation”]
personal = [“Astrophotography”, “Aikido”, “Guitar”, “Apnea Diving”, “Gaming”]
}
recent_focus = [
“terraform”, “terraform-compliance”, “aws”, “tons of aws”, “serverless”,
“pipelines”, “cultural change”, “new ways of working”, “agile”, “#nobuzzwords”
]
twitter = “@3rkunt”
linkedin = “only person with this name and surname”
}
Compliance as Code terraform-compliance.com
> prediction
Compliance as Code terraform-compliance.com
Your main problem is not about testing terraform.
Infrastructure as Code :
What is was ;
A codified way of defining tasks that is required to perform CRUD operations in an IT Environment.
What it is now ;
A codified way of defining the latest state on target IT environment. Mostly used in for Cloud Environments.
Critical Requirement for both is IDEMPOTENCY and ideally IMMUTABILITY.
> terraform init
Compliance as Code terraform-compliance.com
“ “
“ “
Brilliant API Client that focus on desired state and has its own configuration language, HCL/HCL2.
Lots of providers actively maintained. Tons of modules that can be used from the registry.
Solid state management
.. and most importantly ;
> terraform init
Compliance as Code terraform-compliance.com
What is Compliance as Code ?
A codified way of defining compliance policies.
> terraform-compliance -h
Compliance as Code terraform-compliance.com
“ “
What is Compliance as Code ?
A codified way of defining compliance policies.
Requirements :
. A common language that defines the policy
. A language that defines the tests ( might be same with the policy )
. Ability to answer: What are we testing here ?
. Ability to answer: Why are we testing this ?
> terraform-compliance -h
Compliance as Code terraform-compliance.com
“ “
What is Compliance as Code ?
A codified way of defining compliance policies.
Requirements :
. A common language that defines the policy
. A language that defines the tests ( might be same with the policy )
. Ability to answer: What are we testing here ?
. Ability to answer: Why are we testing this ?
> terraform-compliance -h
Compliance as Code terraform-compliance.com
“ “
> terraform plan -out plan.out
Compliance as Code terraform-compliance.com
Implementation terraform plan terraform-compliance terraform apply
terraform plan -detailed-exitcode
> terraform-compliance ?
Compliance as Code terraform-compliance.com
. Based on Behaviour Driven Development. Why ?
. All interpolations and modules are supported. Why is this important ?
. Drilling down, just like another BDD step.
. Resource mounting are supported. Why is this important ?
. Can perform complex Security Group calculations.
. Mostly focused on negative testing. What is negative testing ?
. Filtering.
. Can be assumed as a free version of HashiCorp Sentinel. Really ?
. Runs in everywhere that can run Python or Docker.
. Needs PRs, Feature Requests, Bug Reporting and love just like every Open Source Project.
> Behaviour Driven Development
Compliance as Code terraform-compliance.com
. A branch of Test Driven Development/TDD
. Focus on end-to-end results, functional tests
. Features > Scenarios > Steps, Gherkin/Cucumber language
. Simple sentences with shared vocabulary while every step has a test code under the hood
. GIVEN, WHEN, THEN and AND
. Possible to translate the same tests as UAT, since every Scenario/Feature can be a Story/Task
. Usually takes longer time to run compared with Unit Tests
> terraform-compliance tests
Compliance as Code terraform-compliance.com
. Not an integration test, but still a functional test
. Runs against plan, and runs super-fast
. Same language structure like other BDD tests
. Can live in a separate git repository (strongly recommended!)
. Has its own - but quite universal - vocabulary for steps, e.g. ;
Scenario: Ensure all resources have tags
Given I have resource that supports tags defined
Then it must contain tags
And its value must not be null
> terraform-compliance tests
Compliance as Code terraform-compliance.com
> terraform-compliance tests: GIVEN
Compliance as Code terraform-compliance.com
. Defines the initial picture
. Every scenario has a GIVEN step
. Works as a filtering function
. Will SKIP the next steps if there is no match, so no failure if nothing is found
. Recommended to use terraform references instead of templated entities
. You can use it against resource(s), provider(s), data(s), variable(s) or output(s)
Scenario: Ensure all resources have tags
Given I have aws_s3_bucket defined
Then it must contain tags
And its value must not be null
> terraform-compliance tests: GIVEN
Compliance as Code terraform-compliance.com
. Defines the initial picture
. Every scenario has a GIVEN step
. Works as a filtering function
. Will SKIP the next steps if there is no match, so no failure if nothing is found
. Recommended to use terraform references instead of templated entities
. You can use it against resource(s), provider(s), data(s), variable(s) or output(s)
Scenario: Ensure all resources have tags
Given I have aws_s3_bucket defined
Then it must contain tags
And its value must not be null
resource “aws_s3_bucket” “some_bucket” {
bucket = “my-super-unique-bucket-name”
tags = {
cost_center = “0135134”
environment = “dev”
}
}
> terraform-compliance tests: WHEN
Compliance as Code terraform-compliance.com
. Works as a filtering function (mostly), defines the condition that you are searching for.
. Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN
. Filtered data is used as the INPUT data for the next steps.
Scenario: Ensure we only allow a port range for ingress rule
Given I have aws_security_group defined
When it contains ingress
Then it must only have tcp protocol and port 22 for 0.0.0.0/0
> terraform-compliance tests: WHEN
Compliance as Code terraform-compliance.com
. Works as a filtering function (mostly), defines the condition that you are searching for.
. Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN
. Filtered data is used as the INPUT data for the next steps.
Scenario: Ensure we only allow a port range for ingress rule
Given I have aws_security_group defined
When it contains ingress
Then it must only have tcp protocol and port 22 for 0.0.0.0/0
resource “aws_security_group” “some_group” {
name = “allow_ssh_publicly_because_we_are_just_crazy”
ingress {
from_port = 22
to_port = 22
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
}
}
> terraform-compliance tests: WHEN
Compliance as Code terraform-compliance.com
. Works as a filtering function (mostly), defines the condition that you are searching for.
. Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN
. Filtered data is used as the INPUT data for the next steps.
Scenario: Ensure we only allow a port range for ingress rule
Given I have aws_security_group defined
When it contains ingress
Then it must only have tcp protocol and port 22 for 0.0.0.0/0
resource “aws_security_group_rule” “port_22_to_public” {
type = “ingress”
from_port = 22
to_port = 22
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
security_group_id = aws_security_group.some_group.id
}
> terraform-compliance tests: WHEN
Compliance as Code terraform-compliance.com
. Works as a filtering function (mostly), defines the condition that you are searching for.
. Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN
. Filtered data is used as the INPUT data for the next steps.
Scenario: Ensure there is always 2 network_interfaces attached to instances
Given I have aws_instance defined
When it contains network_interface
And I count them
Then I expect the result is equal to 1
> terraform-compliance tests: WHEN
Compliance as Code terraform-compliance.com
. Works as a filtering function (mostly), defines the condition that you are searching for.
. Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN
. Filtered data is used as the INPUT data for the next steps.
Scenario: Ensure there is always 2 network_interfaces attached to instances
Given I have aws_instance defined
When it contains network_interface
And I count them
Then I expect the result is equal to 1
resource “aws_instance” “monero_miner” {
ami = “ami-6d1c2007”
instance_type = “t2.micro”
network_interface {
device_index = “1”
network_interface_id = “eth0”
}
}
> terraform-compliance tests: WHEN
Compliance as Code terraform-compliance.com
. Works as a filtering function (mostly), defines the condition that you are searching for.
. Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN
. Filtered data is used as the INPUT data for the next steps.
Scenario: Ensure we are using encryption on ALBs via ACM
Given I have aws_elb defined
When it contains listener
Then it must contain ssl_certificate_id
And its value must match the “.*acm.*” regex
> terraform-compliance tests: WHEN
Compliance as Code terraform-compliance.com
. Works as a filtering function (mostly), defines the condition that you are searching for.
. Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN
. Filtered data is used as the INPUT data for the next steps.
Scenario: Ensure we are using encryption on ALBs via ACM
Given I have aws_elb defined
When it contains listener
Then it must contain ssl_certificate_id
And its value must match the “.*acm.*” regex
resource “aws_elb” “bar” {
name = “foo”
...
listener {
...
}
}
> terraform-compliance tests: THEN
Compliance as Code terraform-compliance.com
. Defines the matching criteria. Decision making step.
. FAILS if it not pass.
Scenario: Ensure we are using encryption on ALBs via ACM
Given I have aws_elb defined
When it contains listener
Then it must contain ssl_certificate_id
And its value must match the “.*acm.*” regex
> terraform-compliance tests: THEN
Compliance as Code terraform-compliance.com
. Defines the matching criteria. Decision making step.
. FAILS if it not pass.
Scenario: Ensure we are using encryption on ALBs via ACM
Given I have aws_elb defined
When it contains listener
Then it must contain ssl_certificate_id
And its value must match the “.*acm.*” regex
resource “aws_elb” “bar” {
name = “foo”
listener {
instance_port = 8000
...
ssl_certificate_id = “arn:aws:iam::123456789012:server-certificate/certName”
}
}
> terraform-compliance tests: THEN
Compliance as Code terraform-compliance.com
. Defines the matching criteria. Decision making step.
. FAILS if it not pass.
Scenario: Ensure we only allow a port range for ingress rule
Given I have aws_security_group defined
When it contains ingress
Then it must only have tcp protocol and port 22 for 0.0.0.0/0
> terraform-compliance tests: THEN
Compliance as Code terraform-compliance.com
. Defines the matching criteria. Decision making step.
. FAILS if it not pass.
Scenario: Ensure we only allow a port range for ingress rule
Given I have aws_security_group defined
When it contains ingress
Then it must only have tcp protocol and port 22 for 0.0.0.0/0
resource “aws_security_group_rule” “port_22_to_public” {
type = “ingress”
from_port = 22
to_port = 22
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
security_group_id = aws_security_group.some_group.id
}
> Workflow Examples
Compliance as Code terraform-compliance.com
> Workflow Examples
Compliance as Code terraform-compliance.com
> Workflow Examples
Compliance as Code terraform-compliance.com
> Workflow Examples
Compliance as Code terraform-compliance.com
...
Scenario: Image scan to be enabled on push.
Given I have aws_ecr_repository defined
Then it must contain image_scanning_configuration
And scan_on_push must be enabled
Failure: Resource aws_ecr_repository.repo does not have scan_on_push property enabled
(scan_on_push=None)
[Container] 2020/02/13 11:48:40 Phase complete: BUILD State: FAILED
> Workflow Examples
Compliance as Code terraform-compliance.com
> Workflow Examples
Compliance as Code terraform-compliance.com
> Workflow Examples
Compliance as Code terraform-compliance.com
> Workflow Examples
Compliance as Code terraform-compliance.com
> Workflow Examples
Compliance as Code terraform-compliance.com
> Workflow Examples
Compliance as Code terraform-compliance.com
> Workflow Examples
Compliance as Code terraform-compliance.com
> Workflow Examples
Compliance as Code terraform-compliance.com
Created a PR
Get peer review
approvals
CI failed due to
compliance test failures
Security Team already introduced
new compliance checks
Read the logs, understand
what failed
Fix compliance problems
CI Pass
Merge to masterCD runs without a failureGet final notification
Δt = ~15 minutes
> Workflow Examples
Compliance as Code terraform-compliance.com
Why it was important ?
. No retrospective checks.
. Feedback loop is near-instant while keeping segregation of duties.
. No complicated troubleshooting, problem was described in plain language.
. The PR was not about the failure, it was due to something created before.
. Nothing was deployed till it is fixed.
. Keep it green.
> Workflow Examples
Compliance as Code terraform-compliance.com
What do you need to achieve this workflow ?
. Trunk Based Development, please do not use GitFlow.
. Small incremental changes, instead of huge PRs. ( or worse having a release branch ... )
. Everybody is hands-on. Engineers (including Security) is the Governance.
. Engineers are the decision makers.
. Keep It Simple Stupid.
. VERY IMPORTANT: Good repositories structure.
. You build it, you run it
. #nobuzzwords
> prediction
Compliance as Code terraform-compliance.com
Your main problem is not about testing terraform, right ?
> terraform apply
Compliance as Code terraform-compliance.com
> cat who_is_this_person.tf
resource “human_person” “me” {
name = “Emre Erkunt”
interests {
professional = [“DevOps”, “DevSecOps”, “Security”, “Automation”]
personal = [“Astrophotography”, “Aikido”, “Guitar”, “Apnea Diving”, “Gaming”]
}
recent_focus = [
“terraform”, “terraform-compliance”, “aws”, “tons of aws”, “serverless”,
“pipelines”, “cultural change”, “new ways of working”, “agile”, “#nobuzzwords”
]
twitter = “@3rkunt”
linkedin = “only person with this name and surname”
}
Compliance as Code terraform-compliance.com

More Related Content

PDF
Introduction to Elasticsearch
PDF
Airflow for Beginners
PDF
Designing Event-Driven Applications with Apache NiFi, Apache Flink, Apache Sp...
PPTX
DevOps and Continuous Delivery Reference Architectures - Volume 2
PDF
InfluxDB IOx Tech Talks: Query Engine Design and the Rust-Based DataFusion in...
PDF
RESTful Web Applications with Apache Sling
PDF
Intro to InfluxDB
PPTX
Elasticsearch - under the hood
Introduction to Elasticsearch
Airflow for Beginners
Designing Event-Driven Applications with Apache NiFi, Apache Flink, Apache Sp...
DevOps and Continuous Delivery Reference Architectures - Volume 2
InfluxDB IOx Tech Talks: Query Engine Design and the Rust-Based DataFusion in...
RESTful Web Applications with Apache Sling
Intro to InfluxDB
Elasticsearch - under the hood

What's hot (20)

PPTX
Overview of Message Queues
PDF
Spark streaming , Spark SQL
PDF
Airflow tutorials hands_on
PPTX
An Overview of Web Services: SOAP and REST
PDF
Introduction to elasticsearch
PDF
Intelligently Collecting Data at the Edge - Intro to Apache MiNiFi
PPTX
Web Application Vulnerabilities
PDF
The basics of fluentd
PDF
Enabling Googley microservices with HTTP/2 and gRPC.
PPTX
Hive + Tez: A Performance Deep Dive
PDF
Introduction to influx db
PPTX
elasticsearch_적용 및 활용_정리
PDF
AI made easy with Flink AI Flow
PPT
Spring Batch Introduction
PDF
Kernel Recipes 2018 - CPU Idle Loop Rework - Rafael J. Wysocki
PDF
Data Science Across Data Sources with Apache Arrow
PDF
Can Apache Kafka Replace a Database?
PDF
2022-06-23 Apache Arrow and DataFusion_ Changing the Game for implementing Da...
PDF
IoT Attack Surfaces -- DEFCON 2015
PPTX
Message queues
Overview of Message Queues
Spark streaming , Spark SQL
Airflow tutorials hands_on
An Overview of Web Services: SOAP and REST
Introduction to elasticsearch
Intelligently Collecting Data at the Edge - Intro to Apache MiNiFi
Web Application Vulnerabilities
The basics of fluentd
Enabling Googley microservices with HTTP/2 and gRPC.
Hive + Tez: A Performance Deep Dive
Introduction to influx db
elasticsearch_적용 및 활용_정리
AI made easy with Flink AI Flow
Spring Batch Introduction
Kernel Recipes 2018 - CPU Idle Loop Rework - Rafael J. Wysocki
Data Science Across Data Sources with Apache Arrow
Can Apache Kafka Replace a Database?
2022-06-23 Apache Arrow and DataFusion_ Changing the Game for implementing Da...
IoT Attack Surfaces -- DEFCON 2015
Message queues
Ad

Similar to Compliance as Code with terraform-compliance (20)

PDF
The Need for Async @ ScalaWorld
PDF
How to test infrastructure code: automated testing for Terraform, Kubernetes,...
ODP
Who pulls the strings?
PPTX
Terraform Abstractions for Safety and Power
PPSX
Coding standard
PPT
Introduzione al TDD
PDF
AWS Lambda from the trenches
PPT
Integris Security - Hacking With Glue ℠
PPTX
Sumo Logic Cert Jam - Security & Compliance
PPTX
Terraform training 🎒 - Basic
PDF
Serverless in production, an experience report (linuxing in london)
PDF
Serverless in production, an experience report (JeffConf)
PPTX
Deploying Cloud Native Red Team Infrastructure with Kubernetes, Istio and Envoy
PPT
Secure Programming
PPTX
JS Frameworks Day April,26 of 2014
PDF
Js fwdays unit tesing javascript(by Anna Khabibullina)
ODP
Grails unit testing
PPTX
Advanced malwareanalysis training session2 botnet analysis part1
PPTX
DevOps Fest 2019. Сергей Марченко. Terraform: a novel about modules, provider...
PDF
Serverless in production, an experience report (Going Serverless)
The Need for Async @ ScalaWorld
How to test infrastructure code: automated testing for Terraform, Kubernetes,...
Who pulls the strings?
Terraform Abstractions for Safety and Power
Coding standard
Introduzione al TDD
AWS Lambda from the trenches
Integris Security - Hacking With Glue ℠
Sumo Logic Cert Jam - Security & Compliance
Terraform training 🎒 - Basic
Serverless in production, an experience report (linuxing in london)
Serverless in production, an experience report (JeffConf)
Deploying Cloud Native Red Team Infrastructure with Kubernetes, Istio and Envoy
Secure Programming
JS Frameworks Day April,26 of 2014
Js fwdays unit tesing javascript(by Anna Khabibullina)
Grails unit testing
Advanced malwareanalysis training session2 botnet analysis part1
DevOps Fest 2019. Сергей Марченко. Terraform: a novel about modules, provider...
Serverless in production, an experience report (Going Serverless)
Ad

Recently uploaded (20)

PPT
Mechanical Engineering MATERIALS Selection
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
composite construction of structures.pdf
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
DOCX
573137875-Attendance-Management-System-original
PPTX
Construction Project Organization Group 2.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PPT
Project quality management in manufacturing
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
OOP with Java - Java Introduction (Basics)
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Lecture Notes Electrical Wiring System Components
Mechanical Engineering MATERIALS Selection
bas. eng. economics group 4 presentation 1.pptx
composite construction of structures.pdf
Operating System & Kernel Study Guide-1 - converted.pdf
573137875-Attendance-Management-System-original
Construction Project Organization Group 2.pptx
Internet of Things (IOT) - A guide to understanding
Project quality management in manufacturing
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Structs to JSON How Go Powers REST APIs.pdf
UNIT 4 Total Quality Management .pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
OOP with Java - Java Introduction (Basics)
Arduino robotics embedded978-1-4302-3184-4.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Lecture Notes Electrical Wiring System Components

Compliance as Code with terraform-compliance

  • 1. Compliance as Code Emre Erkunt terraform-compliance
  • 2. > cat who_is_this_person.tf resource “human_person” “me” { name = “Emre Erkunt” interests { professional = [“DevOps”, “DevSecOps”, “Security”, “Automation”] personal = [“Astrophotography”, “Aikido”, “Guitar”, “Apnea Diving”, “Gaming”] } recent_focus = [ “terraform”, “terraform-compliance”, “aws”, “tons of aws”, “serverless”, “pipelines”, “cultural change”, “new ways of working”, “agile”, “#nobuzzwords” ] twitter = “@3rkunt” linkedin = “only person with this name and surname” } Compliance as Code terraform-compliance.com
  • 3. > prediction Compliance as Code terraform-compliance.com Your main problem is not about testing terraform.
  • 4. Infrastructure as Code : What is was ; A codified way of defining tasks that is required to perform CRUD operations in an IT Environment. What it is now ; A codified way of defining the latest state on target IT environment. Mostly used in for Cloud Environments. Critical Requirement for both is IDEMPOTENCY and ideally IMMUTABILITY. > terraform init Compliance as Code terraform-compliance.com “ “ “ “
  • 5. Brilliant API Client that focus on desired state and has its own configuration language, HCL/HCL2. Lots of providers actively maintained. Tons of modules that can be used from the registry. Solid state management .. and most importantly ; > terraform init Compliance as Code terraform-compliance.com
  • 6. What is Compliance as Code ? A codified way of defining compliance policies. > terraform-compliance -h Compliance as Code terraform-compliance.com “ “
  • 7. What is Compliance as Code ? A codified way of defining compliance policies. Requirements : . A common language that defines the policy . A language that defines the tests ( might be same with the policy ) . Ability to answer: What are we testing here ? . Ability to answer: Why are we testing this ? > terraform-compliance -h Compliance as Code terraform-compliance.com “ “
  • 8. What is Compliance as Code ? A codified way of defining compliance policies. Requirements : . A common language that defines the policy . A language that defines the tests ( might be same with the policy ) . Ability to answer: What are we testing here ? . Ability to answer: Why are we testing this ? > terraform-compliance -h Compliance as Code terraform-compliance.com “ “
  • 9. > terraform plan -out plan.out Compliance as Code terraform-compliance.com Implementation terraform plan terraform-compliance terraform apply terraform plan -detailed-exitcode
  • 10. > terraform-compliance ? Compliance as Code terraform-compliance.com . Based on Behaviour Driven Development. Why ? . All interpolations and modules are supported. Why is this important ? . Drilling down, just like another BDD step. . Resource mounting are supported. Why is this important ? . Can perform complex Security Group calculations. . Mostly focused on negative testing. What is negative testing ? . Filtering. . Can be assumed as a free version of HashiCorp Sentinel. Really ? . Runs in everywhere that can run Python or Docker. . Needs PRs, Feature Requests, Bug Reporting and love just like every Open Source Project.
  • 11. > Behaviour Driven Development Compliance as Code terraform-compliance.com . A branch of Test Driven Development/TDD . Focus on end-to-end results, functional tests . Features > Scenarios > Steps, Gherkin/Cucumber language . Simple sentences with shared vocabulary while every step has a test code under the hood . GIVEN, WHEN, THEN and AND . Possible to translate the same tests as UAT, since every Scenario/Feature can be a Story/Task . Usually takes longer time to run compared with Unit Tests
  • 12. > terraform-compliance tests Compliance as Code terraform-compliance.com . Not an integration test, but still a functional test . Runs against plan, and runs super-fast . Same language structure like other BDD tests . Can live in a separate git repository (strongly recommended!) . Has its own - but quite universal - vocabulary for steps, e.g. ; Scenario: Ensure all resources have tags Given I have resource that supports tags defined Then it must contain tags And its value must not be null
  • 13. > terraform-compliance tests Compliance as Code terraform-compliance.com
  • 14. > terraform-compliance tests: GIVEN Compliance as Code terraform-compliance.com . Defines the initial picture . Every scenario has a GIVEN step . Works as a filtering function . Will SKIP the next steps if there is no match, so no failure if nothing is found . Recommended to use terraform references instead of templated entities . You can use it against resource(s), provider(s), data(s), variable(s) or output(s) Scenario: Ensure all resources have tags Given I have aws_s3_bucket defined Then it must contain tags And its value must not be null
  • 15. > terraform-compliance tests: GIVEN Compliance as Code terraform-compliance.com . Defines the initial picture . Every scenario has a GIVEN step . Works as a filtering function . Will SKIP the next steps if there is no match, so no failure if nothing is found . Recommended to use terraform references instead of templated entities . You can use it against resource(s), provider(s), data(s), variable(s) or output(s) Scenario: Ensure all resources have tags Given I have aws_s3_bucket defined Then it must contain tags And its value must not be null resource “aws_s3_bucket” “some_bucket” { bucket = “my-super-unique-bucket-name” tags = { cost_center = “0135134” environment = “dev” } }
  • 16. > terraform-compliance tests: WHEN Compliance as Code terraform-compliance.com . Works as a filtering function (mostly), defines the condition that you are searching for. . Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN . Filtered data is used as the INPUT data for the next steps. Scenario: Ensure we only allow a port range for ingress rule Given I have aws_security_group defined When it contains ingress Then it must only have tcp protocol and port 22 for 0.0.0.0/0
  • 17. > terraform-compliance tests: WHEN Compliance as Code terraform-compliance.com . Works as a filtering function (mostly), defines the condition that you are searching for. . Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN . Filtered data is used as the INPUT data for the next steps. Scenario: Ensure we only allow a port range for ingress rule Given I have aws_security_group defined When it contains ingress Then it must only have tcp protocol and port 22 for 0.0.0.0/0 resource “aws_security_group” “some_group” { name = “allow_ssh_publicly_because_we_are_just_crazy” ingress { from_port = 22 to_port = 22 protocol = “tcp” cidr_blocks = [“0.0.0.0/0”] } }
  • 18. > terraform-compliance tests: WHEN Compliance as Code terraform-compliance.com . Works as a filtering function (mostly), defines the condition that you are searching for. . Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN . Filtered data is used as the INPUT data for the next steps. Scenario: Ensure we only allow a port range for ingress rule Given I have aws_security_group defined When it contains ingress Then it must only have tcp protocol and port 22 for 0.0.0.0/0 resource “aws_security_group_rule” “port_22_to_public” { type = “ingress” from_port = 22 to_port = 22 protocol = “tcp” cidr_blocks = [“0.0.0.0/0”] security_group_id = aws_security_group.some_group.id }
  • 19. > terraform-compliance tests: WHEN Compliance as Code terraform-compliance.com . Works as a filtering function (mostly), defines the condition that you are searching for. . Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN . Filtered data is used as the INPUT data for the next steps. Scenario: Ensure there is always 2 network_interfaces attached to instances Given I have aws_instance defined When it contains network_interface And I count them Then I expect the result is equal to 1
  • 20. > terraform-compliance tests: WHEN Compliance as Code terraform-compliance.com . Works as a filtering function (mostly), defines the condition that you are searching for. . Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN . Filtered data is used as the INPUT data for the next steps. Scenario: Ensure there is always 2 network_interfaces attached to instances Given I have aws_instance defined When it contains network_interface And I count them Then I expect the result is equal to 1 resource “aws_instance” “monero_miner” { ami = “ami-6d1c2007” instance_type = “t2.micro” network_interface { device_index = “1” network_interface_id = “eth0” } }
  • 21. > terraform-compliance tests: WHEN Compliance as Code terraform-compliance.com . Works as a filtering function (mostly), defines the condition that you are searching for. . Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN . Filtered data is used as the INPUT data for the next steps. Scenario: Ensure we are using encryption on ALBs via ACM Given I have aws_elb defined When it contains listener Then it must contain ssl_certificate_id And its value must match the “.*acm.*” regex
  • 22. > terraform-compliance tests: WHEN Compliance as Code terraform-compliance.com . Works as a filtering function (mostly), defines the condition that you are searching for. . Will SKIP the next steps if there is no match, so no failure if nothing is found, just like GIVEN . Filtered data is used as the INPUT data for the next steps. Scenario: Ensure we are using encryption on ALBs via ACM Given I have aws_elb defined When it contains listener Then it must contain ssl_certificate_id And its value must match the “.*acm.*” regex resource “aws_elb” “bar” { name = “foo” ... listener { ... } }
  • 23. > terraform-compliance tests: THEN Compliance as Code terraform-compliance.com . Defines the matching criteria. Decision making step. . FAILS if it not pass. Scenario: Ensure we are using encryption on ALBs via ACM Given I have aws_elb defined When it contains listener Then it must contain ssl_certificate_id And its value must match the “.*acm.*” regex
  • 24. > terraform-compliance tests: THEN Compliance as Code terraform-compliance.com . Defines the matching criteria. Decision making step. . FAILS if it not pass. Scenario: Ensure we are using encryption on ALBs via ACM Given I have aws_elb defined When it contains listener Then it must contain ssl_certificate_id And its value must match the “.*acm.*” regex resource “aws_elb” “bar” { name = “foo” listener { instance_port = 8000 ... ssl_certificate_id = “arn:aws:iam::123456789012:server-certificate/certName” } }
  • 25. > terraform-compliance tests: THEN Compliance as Code terraform-compliance.com . Defines the matching criteria. Decision making step. . FAILS if it not pass. Scenario: Ensure we only allow a port range for ingress rule Given I have aws_security_group defined When it contains ingress Then it must only have tcp protocol and port 22 for 0.0.0.0/0
  • 26. > terraform-compliance tests: THEN Compliance as Code terraform-compliance.com . Defines the matching criteria. Decision making step. . FAILS if it not pass. Scenario: Ensure we only allow a port range for ingress rule Given I have aws_security_group defined When it contains ingress Then it must only have tcp protocol and port 22 for 0.0.0.0/0 resource “aws_security_group_rule” “port_22_to_public” { type = “ingress” from_port = 22 to_port = 22 protocol = “tcp” cidr_blocks = [“0.0.0.0/0”] security_group_id = aws_security_group.some_group.id }
  • 27. > Workflow Examples Compliance as Code terraform-compliance.com
  • 28. > Workflow Examples Compliance as Code terraform-compliance.com
  • 29. > Workflow Examples Compliance as Code terraform-compliance.com
  • 30. > Workflow Examples Compliance as Code terraform-compliance.com ... Scenario: Image scan to be enabled on push. Given I have aws_ecr_repository defined Then it must contain image_scanning_configuration And scan_on_push must be enabled Failure: Resource aws_ecr_repository.repo does not have scan_on_push property enabled (scan_on_push=None) [Container] 2020/02/13 11:48:40 Phase complete: BUILD State: FAILED
  • 31. > Workflow Examples Compliance as Code terraform-compliance.com
  • 32. > Workflow Examples Compliance as Code terraform-compliance.com
  • 33. > Workflow Examples Compliance as Code terraform-compliance.com
  • 34. > Workflow Examples Compliance as Code terraform-compliance.com
  • 35. > Workflow Examples Compliance as Code terraform-compliance.com
  • 36. > Workflow Examples Compliance as Code terraform-compliance.com
  • 37. > Workflow Examples Compliance as Code terraform-compliance.com
  • 38. > Workflow Examples Compliance as Code terraform-compliance.com Created a PR Get peer review approvals CI failed due to compliance test failures Security Team already introduced new compliance checks Read the logs, understand what failed Fix compliance problems CI Pass Merge to masterCD runs without a failureGet final notification Δt = ~15 minutes
  • 39. > Workflow Examples Compliance as Code terraform-compliance.com Why it was important ? . No retrospective checks. . Feedback loop is near-instant while keeping segregation of duties. . No complicated troubleshooting, problem was described in plain language. . The PR was not about the failure, it was due to something created before. . Nothing was deployed till it is fixed. . Keep it green.
  • 40. > Workflow Examples Compliance as Code terraform-compliance.com What do you need to achieve this workflow ? . Trunk Based Development, please do not use GitFlow. . Small incremental changes, instead of huge PRs. ( or worse having a release branch ... ) . Everybody is hands-on. Engineers (including Security) is the Governance. . Engineers are the decision makers. . Keep It Simple Stupid. . VERY IMPORTANT: Good repositories structure. . You build it, you run it . #nobuzzwords
  • 41. > prediction Compliance as Code terraform-compliance.com Your main problem is not about testing terraform, right ?
  • 42. > terraform apply Compliance as Code terraform-compliance.com
  • 43. > cat who_is_this_person.tf resource “human_person” “me” { name = “Emre Erkunt” interests { professional = [“DevOps”, “DevSecOps”, “Security”, “Automation”] personal = [“Astrophotography”, “Aikido”, “Guitar”, “Apnea Diving”, “Gaming”] } recent_focus = [ “terraform”, “terraform-compliance”, “aws”, “tons of aws”, “serverless”, “pipelines”, “cultural change”, “new ways of working”, “agile”, “#nobuzzwords” ] twitter = “@3rkunt” linkedin = “only person with this name and surname” } Compliance as Code terraform-compliance.com