When creating IaC (Infrastructure as Code), it can be useful, just as in other software development, to be able to run the environment locally. This isn't always possible for DevOps and IaC work since much of it relies on cloud providers that can change how their API responds from day to day. However, we still want to increase our confidence that when we use a particular service from a cloud provider, such as AWS S3 or AWS EKS or CloudFront or Lambdas, that those will work in an expected way.
For Terraform, a few years ago I set up a way to test Terraform changes with LocalStack, specifically with Terratest, the automated test framework for Terraform. It is not a requirement for testing Terraform, you can still do that by changing the endpoints that the AWS Terraform provider points to. LocalStack is still a fantastic tool for testing IaC and it came in very handy for testing Lambdas with S3 and SQS. LocalStack has a monthly license for features such as local state persistence and basic IAM policy enforcement. It's a great product and I recommend buying the license if it will give you greater confidence in more complex IaC.
Now there's a new option for testing Terraform with AWS: the AWS Emulator. The AWS Emulator is vibe-coded and uses an agentic AI process to continuously fix bugs and ship features. The AWS Emulator is made for testing infrastructure, however, and should not be used for testing security, such as IAM policies and permissions boundaries or resource-based policies. AWS Emulator's API coverage does vary so while it's a good fast feedback layer, similar to LocalStack, the real test is applying the IaC.
So I updated the code I have for testing Terraform with Terratest to use AWS Emulator alongside LocalStack as an option.
- Workflow for running Terraform with LocalStack in a GitHub Action
- Workflow for running Terraform with AWS Emulator in GitHub Action
Within the many log lines, you can see the tests are passing, and the timing is only slightly shorter for the test run itself (45 seconds vs 48.5 seconds), the overall time was 3m 2s for the LocalStack run and 2m 48s for the AWS Emulator run with some time saved from an RPM package install instead of a Docker image download.
To use AWS Emulator or LocalStack, you will need to set up the provider.tf to have overrides for each of the endpoints you plan to use. You can test it with terraform plan to check that the overrides are working. AWS Emulator can be run on Linux with an RPM package, or on MacOS or any other platform with a Docker image.
Setting the endpoints in infra/provider.tf:
locals {
localstack = jsondecode(file("${path.module}/test-backends.json")).localstack
aws_gateway_endpoint = coalesce(var.aws_emulator_endpoint, local.localstack.gateway)
}
provider "aws" {
region = var.region
access_key = var.aws_emulator_endpoint == null ? local.localstack.access_key : null
secret_key = var.aws_emulator_endpoint == null ? local.localstack.secret_key : null
skip_credentials_validation = true
skip_metadata_api_check = true
skip_requesting_account_id = true
s3_use_path_style = var.aws_emulator_endpoint != null
endpoints {
apigateway = local.aws_gateway_endpoint
apigatewayv2 = local.aws_gateway_endpoint
cloudformation = local.aws_gateway_endpoint
cloudwatch = local.aws_gateway_endpoint
dynamodb = local.aws_gateway_endpoint
ec2 = local.aws_gateway_endpoint
es = local.aws_gateway_endpoint
elasticache = local.aws_gateway_endpoint
firehose = local.aws_gateway_endpoint
iam = local.aws_gateway_endpoint
kinesis = local.aws_gateway_endpoint
lambda = local.aws_gateway_endpoint
rds = local.aws_gateway_endpoint
redshift = local.aws_gateway_endpoint
route53 = local.aws_gateway_endpoint
s3 = coalesce(var.aws_emulator_endpoint, local.localstack.s3)
secretsmanager = local.aws_gateway_endpoint
ses = local.aws_gateway_endpoint
sns = local.aws_gateway_endpoint
sqs = local.aws_gateway_endpoint
ssm = local.aws_gateway_endpoint
stepfunctions = local.aws_gateway_endpoint
sts = local.aws_gateway_endpoint
}
}
Endpoint differences between AWS Emulator and LocalStack in infra/test-backends.json:
{
"localstack": {
"gateway": "http://localhost:4566",
"s3": "http://s3.localhost.localstack.cloud:4566",
"access_key": "test",
"secret_key": "test"
},
"aws-emulator": {
"endpoint_base": "https://127.0.0.1",
"port": 9297,
"region": "us-east-1"
}
}
Either AWS Emulator or LocalStack can be part of the testing strategy to increase confidence in your IaC.
Top comments (0)