Skip to content

Commit

Permalink
Merge branch 'master' into add_recreate_missing_lambda_package_parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
pdecat authored Aug 20, 2021
2 parents 47f5030 + 87d8a58 commit ab703f2
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 5 deletions.
30 changes: 29 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@ All notable changes to this project will be documented in this file.



<a name="v2.11.0"></a>
## [v2.11.0] - 2021-08-20

- fix: No need to set `aws_s3_bucket_object` `etag` as filename is already a hash of the content ([#180](https://github.com/terraform-aws-modules/terraform-aws-lambda/issues/180))


<a name="v2.10.0"></a>
## [v2.10.0] - 2021-08-20

- feat: Add support for separate deployments of infra and code ([#175](https://github.com/terraform-aws-modules/terraform-aws-lambda/issues/175))


<a name="v2.9.0"></a>
## [v2.9.0] - 2021-08-20

- feat: Add topics parameter support for lambda event source ([#166](https://github.com/terraform-aws-modules/terraform-aws-lambda/issues/166))


<a name="v2.8.0"></a>
## [v2.8.0] - 2021-08-14

- feat: Expose ecr tag & scan variables in docker-build module ([#189](https://github.com/terraform-aws-modules/terraform-aws-lambda/issues/189))


<a name="v2.7.0"></a>
## [v2.7.0] - 2021-07-08

Expand Down Expand Up @@ -446,7 +470,11 @@ All notable changes to this project will be documented in this file.
- first commit


[Unreleased]: https://github.com/terraform-aws-modules/terraform-aws-lambda/compare/v2.7.0...HEAD
[Unreleased]: https://github.com/terraform-aws-modules/terraform-aws-lambda/compare/v2.11.0...HEAD
[v2.11.0]: https://github.com/terraform-aws-modules/terraform-aws-lambda/compare/v2.10.0...v2.11.0
[v2.10.0]: https://github.com/terraform-aws-modules/terraform-aws-lambda/compare/v2.9.0...v2.10.0
[v2.9.0]: https://github.com/terraform-aws-modules/terraform-aws-lambda/compare/v2.8.0...v2.9.0
[v2.8.0]: https://github.com/terraform-aws-modules/terraform-aws-lambda/compare/v2.7.0...v2.8.0
[v2.7.0]: https://github.com/terraform-aws-modules/terraform-aws-lambda/compare/v2.6.0...v2.7.0
[v2.6.0]: https://github.com/terraform-aws-modules/terraform-aws-lambda/compare/v2.5.0...v2.6.0
[v2.5.0]: https://github.com/terraform-aws-modules/terraform-aws-lambda/compare/v2.4.0...v2.5.0
Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,32 @@ module "lambda_function_existing_package_local" {
}
```

### Lambda Function or Lambda Layer with the deployable artifact maintained separately from the infrastructure

If you want to manage function code and infrastructure resources (such as IAM permissions, policies, events, etc) in separate flows (e.g., different repositories, teams, CI/CD pipelines).

Disable source code tracking to turn off deployments (and rollbacks) using the module by setting `ignore_source_code_hash = true` and deploy a _dummy function_.

When the infrastructure and the dummy function is deployed, you can use external tool to update the source code of the function (eg, using [AWS CLI](https://docs.aws.amazon.com/cli/latest/reference/lambda/update-function-code.html)) and keep using this module via Terraform to manage the infrastructure.

Be aware that changes in `local_existing_package` value may trigger deployment via Terraform.

```hcl
module "lambda_function_externally_managed_package" {
source = "terraform-aws-modules/lambda/aws"
function_name = "my-lambda-externally-managed-package"
description = "My lambda function code is deployed separately"
handler = "index.lambda_handler"
runtime = "python3.8"
create_package = false
local_existing_package = "./lambda_functions/code.zip"
ignore_source_code_hash = true
}
```

### Lambda Function with existing package (prebuilt) stored in S3 bucket

Note that this module does not copy prebuilt packages into S3 bucket. This module can only store packages it builds locally and in S3 bucket.
Expand Down Expand Up @@ -664,6 +690,7 @@ No modules.
| <a name="input_function_name"></a> [function\_name](#input\_function\_name) | A unique name for your Lambda Function | `string` | `""` | no |
| <a name="input_handler"></a> [handler](#input\_handler) | Lambda Function entrypoint in your code | `string` | `""` | no |
| <a name="input_hash_extra"></a> [hash\_extra](#input\_hash\_extra) | The string to add into hashing function. Useful when building same source path for different functions. | `string` | `""` | no |
| <a name="input_ignore_source_code_hash"></a> [ignore\_source\_code\_hash](#input\_ignore\_source\_code\_hash) | Whether to ignore changes to the function's source code hash. Set to true if you manage infrastructure and code deployments separately. | `bool` | `false` | no |
| <a name="input_image_config_command"></a> [image\_config\_command](#input\_image\_config\_command) | The CMD for the docker image | `list(string)` | `[]` | no |
| <a name="input_image_config_entry_point"></a> [image\_config\_entry\_point](#input\_image\_config\_entry\_point) | The ENTRYPOINT for the docker image | `list(string)` | `[]` | no |
| <a name="input_image_config_working_directory"></a> [image\_config\_working\_directory](#input\_image\_config\_working\_directory) | The working directory for the docker image | `string` | `null` | no |
Expand Down
2 changes: 2 additions & 0 deletions examples/complete/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ Note that this example may create resources which cost money. Run `terraform des
| <a name="module_lambda_function"></a> [lambda\_function](#module\_lambda\_function) | ../../ | |
| <a name="module_lambda_function_existing_package_local"></a> [lambda\_function\_existing\_package\_local](#module\_lambda\_function\_existing\_package\_local) | ../../ | |
| <a name="module_lambda_function_for_each"></a> [lambda\_function\_for\_each](#module\_lambda\_function\_for\_each) | ../../ | |
| <a name="module_lambda_function_with_package_deploying_externally"></a> [lambda\_function\_with\_package\_deploying\_externally](#module\_lambda\_function\_with\_package\_deploying\_externally) | ../../ | |
| <a name="module_lambda_layer_local"></a> [lambda\_layer\_local](#module\_lambda\_layer\_local) | ../../ | |
| <a name="module_lambda_layer_s3"></a> [lambda\_layer\_s3](#module\_lambda\_layer\_s3) | ../../ | |
| <a name="module_lambda_layer_with_package_deploying_externally"></a> [lambda\_layer\_with\_package\_deploying\_externally](#module\_lambda\_layer\_with\_package\_deploying\_externally) | ../../ | |
| <a name="module_lambda_with_mixed_trusted_entities"></a> [lambda\_with\_mixed\_trusted\_entities](#module\_lambda\_with\_mixed\_trusted\_entities) | ../../ | |
| <a name="module_lambda_with_provisioned_concurrency"></a> [lambda\_with\_provisioned\_concurrency](#module\_lambda\_with\_provisioned\_concurrency) | ../../ | |
| <a name="module_s3_bucket"></a> [s3\_bucket](#module\_s3\_bucket) | terraform-aws-modules/s3-bucket/aws | |
Expand Down
38 changes: 38 additions & 0 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,26 @@ module "lambda_layer_local" {
source_path = "${path.module}/../fixtures/python3.8-app1"
}

####################################################
# Lambda Layer with package deploying externally
# (e.g., using separate CI/CD pipeline)
####################################################

module "lambda_layer_with_package_deploying_externally" {
source = "../../"

create_layer = true

layer_name = "${random_pet.this.id}-layer-local"
description = "My amazing lambda layer (deployed from local)"
compatible_runtimes = ["python3.8"]

create_package = false
local_existing_package = "../fixtures/python3.8-zip/existing_package.zip"

ignore_source_code_hash = true
}

###############################
# Lambda Layer (storing on S3)
###############################
Expand Down Expand Up @@ -277,6 +297,24 @@ module "lambda_function_for_each" {
local_existing_package = "${path.module}/../fixtures/python3.8-zip/existing_package.zip"
}

####################################################
# Lambda Function with package deploying externally
# (e.g., using separate CI/CD pipeline)
####################################################

module "lambda_function_with_package_deploying_externally" {
source = "../../"

function_name = "${random_pet.this.id}-lambda-with-package-deploying-externally"
handler = "index.lambda_handler"
runtime = "python3.8"

create_package = false
local_existing_package = "../fixtures/python3.8-zip/existing_package.zip"

ignore_source_code_hash = true
}

###########
# Disabled
###########
Expand Down
6 changes: 3 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ resource "aws_lambda_function" "this" {
package_type = var.package_type

filename = local.filename
source_code_hash = (local.filename == null ? false : fileexists(local.filename)) && !local.was_missing ? filebase64sha256(local.filename) : null
source_code_hash = var.ignore_source_code_hash ? null : (local.filename == null ? false : fileexists(local.filename)) && !local.was_missing ? filebase64sha256(local.filename) : null

s3_bucket = local.s3_bucket
s3_key = local.s3_key
Expand Down Expand Up @@ -100,7 +100,7 @@ resource "aws_lambda_layer_version" "this" {
compatible_runtimes = length(var.compatible_runtimes) > 0 ? var.compatible_runtimes : [var.runtime]

filename = local.filename
source_code_hash = (local.filename == null ? false : fileexists(local.filename)) && !local.was_missing ? filebase64sha256(local.filename) : null
source_code_hash = var.ignore_source_code_hash ? null : (local.filename == null ? false : fileexists(local.filename)) && !local.was_missing ? filebase64sha256(local.filename) : null

s3_bucket = local.s3_bucket
s3_key = local.s3_key
Expand All @@ -116,7 +116,6 @@ resource "aws_s3_bucket_object" "lambda_package" {
acl = var.s3_acl
key = data.external.archive_prepare[0].result.filename
source = data.external.archive_prepare[0].result.filename
etag = fileexists(data.external.archive_prepare[0].result.filename) ? filemd5(data.external.archive_prepare[0].result.filename) : null
storage_class = var.s3_object_storage_class

server_side_encryption = var.s3_server_side_encryption
Expand Down Expand Up @@ -228,6 +227,7 @@ resource "aws_lambda_event_source_mapping" "this" {
maximum_retry_attempts = lookup(each.value, "maximum_retry_attempts", null)
maximum_record_age_in_seconds = lookup(each.value, "maximum_record_age_in_seconds", null)
bisect_batch_on_function_error = lookup(each.value, "bisect_batch_on_function_error", null)
topics = lookup(each.value, "topics", null)

dynamic "destination_config" {
for_each = lookup(each.value, "destination_arn_on_failure", null) != null ? [true] : []
Expand Down
3 changes: 3 additions & 0 deletions modules/docker-build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ No modules.
| <a name="input_create_ecr_repo"></a> [create\_ecr\_repo](#input\_create\_ecr\_repo) | Controls whether ECR repository for Lambda image should be created | `bool` | `false` | no |
| <a name="input_docker_file_path"></a> [docker\_file\_path](#input\_docker\_file\_path) | Path to Dockerfile in source package | `string` | `"Dockerfile"` | no |
| <a name="input_ecr_repo"></a> [ecr\_repo](#input\_ecr\_repo) | Name of ECR repository to use or to create | `string` | `null` | no |
| <a name="input_ecr_repo_tags"></a> [ecr\_repo\_tags](#input\_ecr\_repo\_tags) | A map of tags to assign to ECR repository | `map(string)` | `{}` | no |
| <a name="input_image_tag"></a> [image\_tag](#input\_image\_tag) | Image tag to use. If not specified current timestamp in format 'YYYYMMDDhhmmss' will be used. This can lead to unnecessary rebuilds. | `string` | `null` | no |
| <a name="input_image_tag_mutability"></a> [image\_tag\_mutability](#input\_image\_tag\_mutability) | The tag mutability setting for the repository. Must be one of: `MUTABLE` or `IMMUTABLE` | `string` | `"MUTABLE"` | no |
| <a name="input_scan_on_push"></a> [scan\_on\_push](#input\_scan\_on\_push) | Indicates whether images are scanned after being pushed to the repository | `bool` | `false` | no |
| <a name="input_source_path"></a> [source\_path](#input\_source\_path) | Path to folder containing application code | `string` | `null` | no |

## Outputs
Expand Down
9 changes: 8 additions & 1 deletion modules/docker-build/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,12 @@ resource "docker_registry_image" "this" {
resource "aws_ecr_repository" "this" {
count = var.create_ecr_repo ? 1 : 0

name = var.ecr_repo
name = var.ecr_repo
image_tag_mutability = var.image_tag_mutability

image_scanning_configuration {
scan_on_push = var.scan_on_push
}

tags = var.ecr_repo_tags
}
19 changes: 19 additions & 0 deletions modules/docker-build/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,22 @@ variable "docker_file_path" {
type = string
default = "Dockerfile"
}


variable "image_tag_mutability" {
description = "The tag mutability setting for the repository. Must be one of: `MUTABLE` or `IMMUTABLE`"
type = string
default = "MUTABLE"
}

variable "scan_on_push" {
description = "Indicates whether images are scanned after being pushed to the repository"
type = bool
default = false
}

variable "ecr_repo_tags" {
description = "A map of tags to assign to ECR repository"
type = map(string)
default = {}
}
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,12 @@ variable "artifacts_dir" {
default = "builds"
}

variable "ignore_source_code_hash" {
description = "Whether to ignore changes to the function's source code hash. Set to true if you manage infrastructure and code deployments separately."
type = bool
default = false
}

variable "local_existing_package" {
description = "The absolute path to an existing zip-file to use"
type = string
Expand Down

0 comments on commit ab703f2

Please sign in to comment.