Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update module versions, examples and tests #24

Merged
merged 10 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2020 Cloud Posse, LLC
Copyright 2021-2022 Cloud Posse, LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
49 changes: 26 additions & 23 deletions README.md

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions README.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ related:
url: "https://github.com/cloudposse/terraform-null-label"

description: |-
This module deploys an AWS Lambda function from a zip file or from a docker image. Additionally, it creates an IAM
role for the Lambda function, which optionally attaches policies to allow for Cloudwatch Logs, Cloudwatch Insights,
This module deploys an AWS Lambda function from a Zip file or from a Docker image. Additionally, it creates an IAM
role for the Lambda function, which optionally attaches policies to allow for CloudWatch Logs, Cloudwatch Insights,
VPC Access and X-Ray tracing.
# Example usage

# How to use this module. Should be an easy example to copy and paste.
usage: |-
For a complete example, see [examples/complete](examples/complete).
For automated tests of the complete example using [bats](https://github.com/bats-core/bats-core) and [Terratest](https://github.com/gruntwork-io/terratest)
(which tests and deploys the example on AWS), see [test](test).

```hcl
module "lambda" {
source = "cloudposse/lambda-function/aws"
Expand All @@ -75,8 +75,8 @@ usage: |-
```

examples: |-
Here is an example of using this module:
- [`examples/complete`](https://github.com/cloudposse/terraform-aws-lambda-function/) - complete example of using this module
- [`examples/complete`](https://github.com/cloudposse/terraform-aws-lambda-function/examples/complete) - complete example of using this module
- [`examples/docker-image`](https://github.com/cloudposse/terraform-aws-lambda-function/examples/docker-image) - example of using Lambda with Docker images

# Other files to include in this README from the project folder
include:
Expand All @@ -89,3 +89,5 @@ contributors:
github: "mcalhoun"
- name: "PePe Amengual"
github: "jamengual"
- name: "Andriy Knysh"
github: "aknysh"
33 changes: 17 additions & 16 deletions docs/terraform.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/complete/fixtures.us-east-2.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ namespace = "eg"
environment = "ue2"
stage = "test"

function_name = "example-complete-function"
function_name = "example-complete"
handler = "handler.handler"
runtime = "nodejs14.x"
2 changes: 1 addition & 1 deletion examples/complete/handler.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
exports.handler = async function (_event, _context) {
return { data: "Hello World" };
return {data: "Hello World"};
};
18 changes: 9 additions & 9 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ locals {
join("", data.aws_partition.current.*.partition),
join("", data.aws_caller_identity.current.*.account_id),
)

policy_arn_inside = format("%s/%s", local.policy_arn_prefix, local.policy_name_inside)

policy_json = jsonencode({
Expand Down Expand Up @@ -53,7 +54,7 @@ resource "aws_iam_policy" "inside" {
count = local.enabled ? 1 : 0
name = local.policy_name_inside
path = "/"
description = "My policy attached inside the lambda module"
description = "The policy attached inside the Lambda module"

policy = local.policy_json
}
Expand All @@ -62,7 +63,7 @@ resource "aws_iam_policy" "outside" {
count = local.enabled ? 1 : 0
name = local.policy_name_outside
path = "/"
description = "My policy attached outside the lambda module"
description = "The policy attached outside the Lambda module"
aknysh marked this conversation as resolved.
Show resolved Hide resolved

policy = local.policy_json
}
Expand All @@ -76,10 +77,11 @@ resource "aws_iam_role_policy_attachment" "outside" {
module "lambda" {
source = "../.."

filename = join("", data.archive_file.lambda_zip.*.output_path)
function_name = module.label.id
handler = var.handler
runtime = var.runtime
filename = join("", data.archive_file.lambda_zip.*.output_path)
function_name = module.label.id
handler = var.handler
runtime = var.runtime
iam_policy_description = var.iam_policy_description

custom_iam_policy_arns = [
"arn:aws:iam::aws:policy/job-function/ViewOnlyAccess",
Expand All @@ -89,7 +91,5 @@ module "lambda" {

context = module.this.context

depends_on = [
aws_iam_policy.inside,
]
depends_on = [aws_iam_policy.inside]
}
10 changes: 8 additions & 2 deletions examples/complete/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ variable "region" {
}

variable "function_name" {
description = "Unique name for the Lambda Function."
type = string
description = "Unique name for the Lambda Function."
}

variable "handler" {
type = string
description = "The function entrypoint in your code."
default = ""
type = string
}

variable "runtime" {
type = string
description = "The runtime environment for the Lambda function you are uploading."
default = ""
}

variable "iam_policy_description" {
type = string
description = "Description of the IAM policy for the Lambda IAM role"
default = "Minimum SSM read permissions for Lambda"
}
2 changes: 1 addition & 1 deletion examples/complete/versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 0.13"
required_version = ">= 0.14"

required_providers {
aws = {
Expand Down
2 changes: 1 addition & 1 deletion examples/docker-image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ COPY handler.js ${LAMBDA_TASK_ROOT}
ENV NODE_ENV=production

# start app
CMD [ "handler.handler" ]
CMD [ "handler.handler" ]
2 changes: 1 addition & 1 deletion examples/docker-image/fixtures.us-east-2.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ namespace = "eg"
environment = "ue2"
stage = "test"

function_name = "example-complete-function"
function_name = "example-docker-image"
handler = "handler.handler"
runtime = "nodejs14.x"
2 changes: 1 addition & 1 deletion examples/docker-image/handler.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
exports.handler = async function (_event, _context) {
return { data: "Hello World" };
return {data: "Hello World"};
};
46 changes: 26 additions & 20 deletions examples/docker-image/main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
data "aws_region" "this" {
count = module.this.enabled ? 1 : 0
}

data "aws_caller_identity" "this" {
count = module.this.enabled ? 1 : 0
}

module "label" {
source = "cloudposse/label/null"
version = "0.25.0"
Expand All @@ -6,67 +14,65 @@ module "label" {
context = module.this.context
}

# Cloud Posse does NOT recommend building and pushing images to ECR via Terraform code. This is a job for your CI/CD
# pipeline. It is only done here for convenience and so that the example can be run locally.
module "ecr" {
source = "cloudposse/ecr/aws"
version = "0.32.3"
version = "0.34.0"
name = module.label.id

context = module.this.context
}

# Need to sleep for a few seconds after creating the ECR repository before we can push to it
resource "time_sleep" "wait_15_seconds" {
count = module.this.enabled ? 1 : 0
depends_on = [module.ecr]
count = module.this.enabled ? 1 : 0

create_duration = "15s"
}

# Cloud Posse does NOT recommend building and pushing images to ECR via Terraform code. This is a job for your CI/CD
# pipeline. It is only done here for convenience and so that the example can be run locally.
data "aws_region" "this" { count = module.this.enabled ? 1 : 0 }
data "aws_caller_identity" "this" { count = module.this.enabled ? 1 : 0 }
depends_on = [module.ecr]
}

resource "null_resource" "docker_build" {
count = module.this.enabled ? 1 : 0

provisioner "local-exec" {
command = "docker build -t ${module.ecr.repository_url} ."
}
}

resource "null_resource" "docker_login" {
count = module.this.enabled ? 1 : 0

provisioner "local-exec" {
command = "aws ecr get-login-password --region ${data.aws_region.this[0].name} | docker login --username AWS --password-stdin ${data.aws_caller_identity.this[0].account_id}.dkr.ecr.${data.aws_region.this[0].name}.amazonaws.com"
command = "aws ecr get-login-password --region ${join("", data.aws_region.this.*.name)} | docker login --username AWS --password-stdin ${join("", data.aws_caller_identity.this.*.account_id)}.dkr.ecr.${join("", data.aws_region.this.*.name)}.amazonaws.com"
}

depends_on = [
null_resource.docker_build,
]
depends_on = [null_resource.docker_build]
}

resource "null_resource" "docker_push" {
count = module.this.enabled ? 1 : 0

provisioner "local-exec" {
command = "docker push ${module.ecr.repository_url}:latest"
}

depends_on = [
time_sleep.wait_15_seconds,
null_resource.docker_login,
null_resource.docker_login
nitrocode marked this conversation as resolved.
Show resolved Hide resolved
]
}

module "lambda" {

source = "../.."

function_name = module.label.id
image_uri = "${module.ecr.repository_url}:latest"
package_type = "Image"
function_name = module.label.id
image_uri = "${module.ecr.repository_url}:latest"
package_type = "Image"
iam_policy_description = var.iam_policy_description

context = module.this.context
depends_on = [
null_resource.docker_push,
nitrocode marked this conversation as resolved.
Show resolved Hide resolved
]

depends_on = [null_resource.docker_push]
}
1 change: 0 additions & 1 deletion examples/docker-image/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

output "arn" {
description = "ARN of the lambda function"
value = module.lambda.arn
Expand Down
10 changes: 8 additions & 2 deletions examples/docker-image/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ variable "region" {
}

variable "function_name" {
description = "Unique name for the Lambda Function."
type = string
description = "Unique name for the Lambda Function."
}

variable "handler" {
type = string
description = "The function entrypoint in your code."
default = ""
type = string
}

variable "runtime" {
type = string
description = "The runtime environment for the Lambda function you are uploading."
default = ""
}

variable "iam_policy_description" {
type = string
description = "Description of the IAM policy for the Lambda IAM role"
default = "Minimum SSM read permissions for Lambda"
}
2 changes: 1 addition & 1 deletion examples/docker-image/versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 1.0"
required_version = ">= 0.14"

required_providers {
aws = {
Expand Down
13 changes: 8 additions & 5 deletions iam-role.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
resource "aws_iam_role" "this" {
count = local.enabled ? 1 : 0
count = local.enabled ? 1 : 0

name = "${var.function_name}-${local.region_name}"
assume_role_policy = join("", data.aws_iam_policy_document.assume_role_policy.*.json)
permissions_boundary = var.permissions_boundary
Expand All @@ -13,13 +14,14 @@ data "aws_iam_policy_document" "assume_role_policy" {

principals {
type = "Service"
identifiers = concat(["lambda.amazonaws.com"], var.lambda_at_edge ? ["edgelambda.amazonaws.com"] : [])
identifiers = concat(["lambda.amazonaws.com"], var.lambda_at_edge_enabled ? ["edgelambda.amazonaws.com"] : [])
}
}
}

resource "aws_iam_role_policy_attachment" "cloudwatch_logs" {
count = local.enabled ? 1 : 0
count = local.enabled ? 1 : 0

policy_arn = "arn:${local.partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
role = aws_iam_role.this[0].name
}
Expand Down Expand Up @@ -63,8 +65,8 @@ data "aws_iam_policy_document" "ssm" {
resource "aws_iam_policy" "ssm" {
count = try((local.enabled && var.ssm_parameter_names != null && length(var.ssm_parameter_names) > 0), false) ? 1 : 0

description = "Provides minimum SSM read permissions."
name = "${var.function_name}-ssm-policy-${local.region_name}"
description = var.iam_policy_description
policy = data.aws_iam_policy_document.ssm[count.index].json
}

Expand All @@ -76,7 +78,8 @@ resource "aws_iam_role_policy_attachment" "ssm" {
}

resource "aws_iam_role_policy_attachment" "custom" {
for_each = local.enabled && length(var.custom_iam_policy_arns) > 0 ? var.custom_iam_policy_arns : toset([])
for_each = local.enabled && length(var.custom_iam_policy_arns) > 0 ? var.custom_iam_policy_arns : toset([])

role = aws_iam_role.this[0].name
policy_arn = each.key
}
Loading