Skip to content

Instantly share code, notes, and snippets.

@dcorrea777
Created May 14, 2022 01:39
Show Gist options
  • Save dcorrea777/860fb6ad5bc24396c2f661baffc18779 to your computer and use it in GitHub Desktop.
Save dcorrea777/860fb6ad5bc24396c2f661baffc18779 to your computer and use it in GitHub Desktop.
example-terraform-cloudfront-lambda
default_cache_behavior {
    lambda_function_association {
        event_type   = "origin-response"
        lambda_arn   = "${aws_lambda_function.sso_cloudfront_trigger.arn}:${aws_lambda_function.sso_cloudfront_trigger.version}"
        include_body = false
    }
}   

resource "aws_lambda_function" "sso_cloudfront_trigger" {
    s3_bucket       = local.data.bucket_artifacts
    s3_key          = "sso/${var.project_name}-cloudfront-trigger.zip"
    # If the file is not in the current working directory you will need to include a
    # path.module in the filename.
    function_name   = "${var.project_name}-cloudfront-trigger"
    role            = aws_iam_role.lambda_cloudfront_response_viewer.arn
    handler         = "index.handler"

    # The filebase64sha256() function is available in Terraform 0.11.12 and later
    # For Terraform 0.11.11 and earlier, use the base64sha256() function and the file() function:
    # source_code_hash = "${base64sha256(file("lambda_function_payload.zip"))}"
    source_code_hash = data.archive_file.zipfile.output_base64sha256
    runtime = "nodejs14.x"
    publish = true
}

data "archive_file" "zipfile" {
    type                = "zip"
    source_dir          = "lambda/src"
    output_file_mode    = "0666"
    output_path         = "lambda/${var.project_name}-cloudfront-trigger.zip"
}

resource "aws_s3_object" "artifact" {
    bucket = local.data.bucket_artifacts
    key    = "sso/${var.project_name}-cloudfront-trigger.zip"
    source = data.archive_file.zipfile.output_path
    etag   = data.archive_file.zipfile.output_base64sha256
    tags   = local.data.tags
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment