We're hosting our webapp on CloudFront and S3. This infrastructure is configured in a Terraform module. We're using the same module (managed by Terragrunt) to deploy our webapp to our staging and production environments.
Obviously, we don't want public access to our staging environment. As such, we've created a Lambda function to enabled Basic HTTP Auth and are using the lambda_function_association within the aws_cloudfront_distribution resource to enable it.
The issue is we don't want the Lambda to run on our prod environment as well. I haven't been able to conditionally set the association on the resource.
I've also tried creating two resources with the same name and setting the count property so that only of the resources exists.
e.g.
# Basic Auth Guard
resource "aws_cloudfront_distribution" "default" {
count = "${var.behind_auth_guard}"
...
}
# No Basic Auth Guard
resource "aws_cloudfront_distribution" "default" {
count = "${var.behind_auth_guard ? 0 : 1}"
}
However when I try to deploy the code, I get aws_cloudfront_distribution.default: resource repeated multiple times.
Is there any way to achieve what I want?
Another option that I've considered is setting the Lambda on both versions, but having it not do anything in prod. However, this seems inefficient and costly as the Lamdba will be called on every request, and would like to avoid it if possible.