Skip to content

Commit

Permalink
Update aws_lambda_function.filename in aws_static_site to not introdu…
Browse files Browse the repository at this point in the history
…ce absolute paths to Terraform state.
  • Loading branch information
jareware committed Apr 4, 2019
1 parent 5b77d7a commit 097eb46
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions aws_static_site/lambda.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ data "template_file" "lambda" {
}

# Lambda functions can only be uploaded as ZIP files, so we need to package our JS file into one
data "archive_file" "lambda" {
data "archive_file" "lambda_zip" {
type = "zip"
output_path = "${path.module}/lambda.zip"

Expand All @@ -33,9 +33,14 @@ data "archive_file" "lambda" {
}

resource "aws_lambda_function" "viewer_request" {
provider = "aws.us_east_1" # because: error creating CloudFront Distribution: InvalidLambdaFunctionAssociation: The function must be in region 'us-east-1'
filename = "${path.module}/lambda.zip"
source_code_hash = "${data.archive_file.lambda.output_base64sha256}"
provider = "aws.us_east_1" # because: error creating CloudFront Distribution: InvalidLambdaFunctionAssociation: The function must be in region 'us-east-1'

# lambda_zip.output_path will be absolute, i.e. different on different machines.
# This can cause Terraform to notice differences that aren't actually there, so let's convert it to a relative one.
# https://github.com/hashicorp/terraform/issues/7613#issuecomment-332238441
filename = "${substr(data.archive_file.lambda_zip.output_path, length(path.cwd) + 1, -1)}"

source_code_hash = "${data.archive_file.lambda_zip.output_base64sha256}"
function_name = "${local.prefix_with_domain}---viewer_request"
role = "${aws_iam_role.this.arn}"
description = "${var.comment_prefix}${var.site_domain} (request handler)"
Expand All @@ -45,9 +50,14 @@ resource "aws_lambda_function" "viewer_request" {
}

resource "aws_lambda_function" "viewer_response" {
provider = "aws.us_east_1" # because: error creating CloudFront Distribution: InvalidLambdaFunctionAssociation: The function must be in region 'us-east-1'
filename = "${path.module}/lambda.zip"
source_code_hash = "${data.archive_file.lambda.output_base64sha256}"
provider = "aws.us_east_1" # because: error creating CloudFront Distribution: InvalidLambdaFunctionAssociation: The function must be in region 'us-east-1'

# lambda_zip.output_path will be absolute, i.e. different on different machines.
# This can cause Terraform to notice differences that aren't actually there, so let's convert it to a relative one.
# https://github.com/hashicorp/terraform/issues/7613#issuecomment-332238441
filename = "${substr(data.archive_file.lambda_zip.output_path, length(path.cwd) + 1, -1)}"

source_code_hash = "${data.archive_file.lambda_zip.output_base64sha256}"
function_name = "${local.prefix_with_domain}---viewer_response"
role = "${aws_iam_role.this.arn}"
description = "${var.comment_prefix}${var.site_domain} (response handler)"
Expand Down

0 comments on commit 097eb46

Please sign in to comment.