I am trying to do AWS resource provisioning through terraform and planning to have a CICD pipeline with terratest unit test cases for the terraform code. My problem is I have CloudFront in my infrastructure and it takes about 20 mins for the creation and around the same time for removal. I don't want the CI build to take around 45 mins just for running unit test cases.
I came across localstack to mock the AWS environment but didn't find ways to point terratest to localstack resources. This is what I tried
- Created a localstack docker container
- Configured the terraform to point to localstack - added endpoint url in the provider section.
- Applied the terraform config and verified bucket created in the localstack.
- Wrote a simple terratest test case to assert if the bucket exists.
Terraform code is as follows,
terraform {
backend "s3" {
bucket = "<bucket-name>"
key = "state/terraform.tfstate"
region = "us-east-1"
profile = "default"
}
}
provider "aws" {
region = "us-east-1"
s3_force_path_style = true
skip_metadata_api_check = true
endpoints {
s3 = "http://localhost:4572"
}
}
resource "aws_s3_bucket" "test_bucket" {
bucket = "test-bucket"
acl = "public-read-write"
cors_rule {
allowed_headers = ["*"]
allowed_methods = ["GET", "HEAD", "PUT"]
allowed_origins = ["*"]
expose_headers = ["ETag"]
}
region = "us-east-1"
}
output "name" {
value = "${aws_s3_bucket.test_bucket.bucket}"
}
output "region" {
value = "${aws_s3_bucket.test_bucket.region}"
}
When the terratest test case as given below was executed a bucket was created in the localstack. But I couldn't find any api or config that would point the terratest AWS module to localstack endpoints. AssertS3BucketExists by default check the AWS environment for the bucket and the assertion fails.
Terratest code is as follows.
package aws
import (
"fmt"
"testing"
"github.com/gruntwork-io/terratest/modules/aws"
"github.com/gruntwork-io/terratest/modules/terraform"
)
func TestWebServer(t *testing.T) {
terraformOptions := &terraform.Options{
// The path to where your Terraform code is located
TerraformDir: ".",
}
terraform.InitAndApply(t, terraformOptions)
name := terraform.Output(t, terraformOptions, "name")
region := terraform.Output(t, terraformOptions, "region")
aws.AssertS3BucketExists(t, region, name)
Any help here would be much appreciated.