Skip to content

Commit 86c6c28

Browse files
authored
Merge pull request #15 from hong-yi/main
add support for edge functions
2 parents b8a4ed3 + fb22873 commit 86c6c28

File tree

6 files changed

+89
-14
lines changed

6 files changed

+89
-14
lines changed

README.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,28 @@
1010

1111
| Name | Version |
1212
|------|---------|
13-
| <a name="provider_archive"></a> [archive](#provider\_archive) | 2.3.0 |
14-
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.15.0 |
13+
| <a name="provider_archive"></a> [archive](#provider\_archive) | ~> 2.3.0 |
14+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.27 |
1515

1616
## Modules
1717

1818
| Name | Source | Version |
1919
|------|--------|---------|
20-
| <a name="module_lambda"></a> [lambda](#module\_lambda) | terraform-aws-modules/lambda/aws | ~> 6.0.0 |
21-
| <a name="module_lamda_gha"></a> [lamda\_gha](#module\_lamda\_gha) | philips-labs/github-oidc/aws | ~> 0.7.0 |
20+
| <a name="module_lambda"></a> [lambda](#module\_lambda) | terraform-aws-modules/lambda/aws | ~> 6.0.1 |
21+
| <a name="module_lambda_gha"></a> [lambda\_gha](#module\_lambda\_gha) | philips-labs/github-oidc/aws | ~> 0.7.0 |
2222
| <a name="module_oidc_provider"></a> [oidc\_provider](#module\_oidc\_provider) | philips-labs/github-oidc/aws//modules/provider | ~> 0.7.0 |
2323

2424
## Resources
2525

2626
| Name | Type |
2727
|------|------|
2828
| [aws_iam_role_policy.update_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
29+
| [aws_iam_role_policy.update_lambda_edge](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
2930
| [archive_file.dummy](https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file) | data source |
3031
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
3132
| [aws_iam_openid_connect_provider.github](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_openid_connect_provider) | data source |
3233
| [aws_iam_policy_document.update_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
34+
| [aws_iam_policy_document.update_lambda_edge](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
3335
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
3436

3537
## Inputs
@@ -39,20 +41,22 @@
3941
| <a name="input_allowed_triggers"></a> [allowed\_triggers](#input\_allowed\_triggers) | Map of allowed triggers to create Lambda permissions | `map(any)` | `{}` | no |
4042
| <a name="input_architectures"></a> [architectures](#input\_architectures) | Instruction set architecture for your Lambda function. Valid values are ["x86\_64"] and ["arm64"]. | `list(string)` | `null` | no |
4143
| <a name="input_attach_network_policy"></a> [attach\_network\_policy](#input\_attach\_network\_policy) | Flag to attach network policy to use VPC subnet and security group | `bool` | `false` | no |
42-
| <a name="input_attach_policies"></a> [attach\_policies](#input\_attach\_policies) | controls whether AWS managed policies should be added to IAM role for Lambda Function (e.g AWSLambdaBasicExecutionRole) | `bool` | `false` | no |
44+
| <a name="input_attach_policies"></a> [attach\_policies](#input\_attach\_policies) | Controls whether AWS managed policies should be added to IAM role for Lambda Function (e.g AWSLambdaBasicExecutionRole) | `bool` | `false` | no |
4345
| <a name="input_attach_policy_json"></a> [attach\_policy\_json](#input\_attach\_policy\_json) | Controls whether policy\_json should be added to IAM role for Lambda Function | `bool` | `false` | no |
4446
| <a name="input_attach_policy_jsons"></a> [attach\_policy\_jsons](#input\_attach\_policy\_jsons) | Controls whether policy\_jsons should be added to IAM role for Lambda Function | `bool` | `false` | no |
4547
| <a name="input_attach_policy_statements"></a> [attach\_policy\_statements](#input\_attach\_policy\_statements) | Controls whether policy\_jsons should be added to IAM role for Lambda Function | `bool` | `false` | no |
4648
| <a name="input_authorization_type"></a> [authorization\_type](#input\_authorization\_type) | The type of authentication that the Lambda Function URL uses. Set to 'AWS\_IAM' to restrict access to authenticated IAM users only. Set to 'NONE' to bypass IAM authentication and create a public endpoint. | `string` | `"NONE"` | no |
49+
| <a name="input_cf_distribution_id"></a> [cf\_distribution\_id](#input\_cf\_distribution\_id) | distribution id to allow oidc role to update edge functions that are attached | `string` | `""` | no |
4750
| <a name="input_cloudwatch_logs_retention_in_days"></a> [cloudwatch\_logs\_retention\_in\_days](#input\_cloudwatch\_logs\_retention\_in\_days) | Number of days the cloudwatch logs will be retained. Possible values are: 1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, and 3653. | `number` | `7` | no |
4851
| <a name="input_code_signing_config_arn"></a> [code\_signing\_config\_arn](#input\_code\_signing\_config\_arn) | Amazon Resource Name (ARN) for a Code Signing Configuration | `string` | `null` | no |
4952
| <a name="input_cors"></a> [cors](#input\_cors) | CORS settings to be used by the Lambda Function URL | `any` | `{}` | no |
5053
| <a name="input_create_current_version_allowed_triggers"></a> [create\_current\_version\_allowed\_triggers](#input\_create\_current\_version\_allowed\_triggers) | Whether to allow triggers on current version of Lambda Function (this will revoke permissions from previous version because Terraform manages only current resources) | `bool` | `true` | no |
54+
| <a name="input_create_github_actions_edge_role"></a> [create\_github\_actions\_edge\_role](#input\_create\_github\_actions\_edge\_role) | controls whether to create for lambda edge functions | `bool` | `false` | no |
5155
| <a name="input_create_github_actions_oidc_provider"></a> [create\_github\_actions\_oidc\_provider](#input\_create\_github\_actions\_oidc\_provider) | Controls Whether to create openid connect provider. | `bool` | `false` | no |
5256
| <a name="input_create_github_actions_role"></a> [create\_github\_actions\_role](#input\_create\_github\_actions\_role) | Controls whether to create AWS OIDC integration GitHub Actions | `bool` | `true` | no |
57+
| <a name="input_create_lambda_cloudwatch_log_group"></a> [create\_lambda\_cloudwatch\_log\_group](#input\_create\_lambda\_cloudwatch\_log\_group) | Controls whether the Lambda Role | `bool` | `true` | no |
5358
| <a name="input_create_lambda_function_url"></a> [create\_lambda\_function\_url](#input\_create\_lambda\_function\_url) | Controls whether the Lambda Function URL resource should be created | `bool` | `false` | no |
5459
| <a name="input_create_lambda_role"></a> [create\_lambda\_role](#input\_create\_lambda\_role) | Controls whether the Lambda Role | `bool` | `true` | no |
55-
| <a name="input_create_lambda_cloudwatch_log_group"></a> [create\_lambda\_cloudwatch\_log\_group](#input\_create\_lambda\_cloudwatch\_log\_group) | Controls whether the Lambda Role | `bool` | `true` | no |
5660
| <a name="input_create_unqualified_alias_lambda_function_url"></a> [create\_unqualified\_alias\_lambda\_function\_url](#input\_create\_unqualified\_alias\_lambda\_function\_url) | Whether to use unqualified alias pointing to $LATEST version in Lambda Function URL | `bool` | `true` | no |
5761
| <a name="input_dead_letter_target_arn"></a> [dead\_letter\_target\_arn](#input\_dead\_letter\_target\_arn) | The ARN of an SNS topic or SQS queue to notify when an invocation fails. | `string` | `null` | no |
5862
| <a name="input_default_conditions"></a> [default\_conditions](#input\_default\_conditions) | (Optional) Default condtions to apply, at least one of the following is madatory: 'allow\_main', 'allow\_environment', 'deny\_pull\_request' and 'allow\_all'. | `list(string)` | <pre>[<br> "allow_main",<br> "allow_environment"<br>]</pre> | no |
@@ -71,9 +75,9 @@
7175
| <a name="input_lambda_at_edge"></a> [lambda\_at\_edge](#input\_lambda\_at\_edge) | Set this to true if using Lambda@Edge, to enable publishing, limit the timeout, and allow edgelambda.amazonaws.com to invoke the function | `bool` | `false` | no |
7276
| <a name="input_lambda_role"></a> [lambda\_role](#input\_lambda\_role) | IAM role ARN attached to the Lambda Function. This governs both who / what can invoke your Lambda Function, as well as what resources our Lambda Function has access to. See Lambda Permission Model for more details. | `string` | `""` | no |
7377
| <a name="input_layers"></a> [layers](#input\_layers) | List of Lambda Layer Version ARNs (maximum of 5) to attach to your Lambda Function. | `list(string)` | `null` | no |
74-
| <a name="input_managed_policy_arns"></a> [managed\_policy\_arns](#input\_managed\_policy\_arns) | list of AWS managed policies to attach to IAM role for Lambda Function | `list(string)` | `null` | no |
78+
| <a name="input_managed_policy_arns"></a> [managed\_policy\_arns](#input\_managed\_policy\_arns) | List of AWS managed policies to attach to IAM role for Lambda Function | `list(string)` | `null` | no |
7579
| <a name="input_memory_size"></a> [memory\_size](#input\_memory\_size) | Amount of memory in MB your Lambda Function can use at runtime. Valid value between 128 MB to 10,240 MB (10 GB), in 64 MB increments. | `number` | `128` | no |
76-
| <a name="input_number_of_managed_policies"></a> [number\_of\_managed\_policies](#input\_number\_of\_managed\_policies) | number of AWS managed policies to attach to IAM role for Lambda Function | `number` | `0` | no |
80+
| <a name="input_number_of_managed_policies"></a> [number\_of\_managed\_policies](#input\_number\_of\_managed\_policies) | Number of AWS managed policies to attach to IAM role for Lambda Function | `number` | `0` | no |
7781
| <a name="input_number_of_policy_jsons"></a> [number\_of\_policy\_jsons](#input\_number\_of\_policy\_jsons) | Number of policies JSON to attach to IAM role for Lambda Function | `number` | `0` | no |
7882
| <a name="input_package_type"></a> [package\_type](#input\_package\_type) | The Lambda deployment package type. Valid options: Zip or Image | `string` | `"Zip"` | no |
7983
| <a name="input_policy_json"></a> [policy\_json](#input\_policy\_json) | An additional policy document as JSON to attach to the Lambda Function role | `string` | `null` | no |
@@ -84,6 +88,7 @@
8488
| <a name="input_role_name"></a> [role\_name](#input\_role\_name) | Name of IAM role to use for Lambda Function. | `string` | `null` | no |
8589
| <a name="input_runtime"></a> [runtime](#input\_runtime) | Lambda Function runtime | `string` | `"nodejs18.x"` | no |
8690
| <a name="input_snap_start"></a> [snap\_start](#input\_snap\_start) | (Optional) Snap start settings for low-latency startups | `bool` | `false` | no |
91+
| <a name="input_source_path"></a> [source\_path](#input\_source\_path) | The absolute path to a local file or directory containing your Lambda source code | `string` | `null` | no |
8792
| <a name="input_timeout"></a> [timeout](#input\_timeout) | The amount of time your Lambda Function has to run in seconds. | `number` | `3` | no |
8893
| <a name="input_tracing_mode"></a> [tracing\_mode](#input\_tracing\_mode) | Tracing mode of the Lambda Function. Valid value can be either PassThrough or Active. | `string` | `null` | no |
8994
| <a name="input_vpc_security_group_ids"></a> [vpc\_security\_group\_ids](#input\_vpc\_security\_group\_ids) | List of security group ids when Lambda Function should run in the VPC. | `list(string)` | `null` | no |
@@ -101,8 +106,8 @@
101106
| <a name="output_lambda_function_arn"></a> [lambda\_function\_arn](#output\_lambda\_function\_arn) | Lambda Function ARN |
102107
| <a name="output_lambda_function_name"></a> [lambda\_function\_name](#output\_lambda\_function\_name) | Lambda Function Name |
103108
| <a name="output_lambda_function_version"></a> [lambda\_function\_version](#output\_lambda\_function\_version) | Latest published version of Lambda Function |
109+
| <a name="output_lambda_gha_role_name"></a> [lambda\_gha\_role\_name](#output\_lambda\_gha\_role\_name) | The crated role that can be assumed for the configured repository. |
104110
| <a name="output_lambda_role_arn"></a> [lambda\_role\_arn](#output\_lambda\_role\_arn) | ARN of the IAM role created for the Lambda Function |
105111
| <a name="output_lambda_role_name"></a> [lambda\_role\_name](#output\_lambda\_role\_name) | The name of the IAM role created for the Lambda Function |
106112
| <a name="output_lambda_role_unique_id"></a> [lambda\_role\_unique\_id](#output\_lambda\_role\_unique\_id) | The unique id of the IAM role created for the Lambda Function |
107-
| <a name="output_lamda_gha_role_name"></a> [lamda\_gha\_role\_name](#output\_lamda\_gha\_role\_name) | The crated role that can be assumed for the configured repository. |
108113
| <a name="output_qualified_arn"></a> [qualified\_arn](#output\_qualified\_arn) | The qualified arn of the lambda function to be associated with Cloudfront as a Lambda@Edge function |

data.tf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,37 @@ data "aws_iam_policy_document" "update_lambda" {
3232
resources = ["arn:aws:lambda:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:function:${var.function_prefix}*"]
3333
}
3434
}
35+
36+
data "aws_iam_policy_document" "update_lambda_edge" {
37+
statement {
38+
sid = "EnableCFReplication"
39+
40+
actions = [
41+
"lambda:GetFunction",
42+
"lambda:EnableReplication",
43+
]
44+
45+
resources = [
46+
"arn:aws:lambda:us-east-1:${data.aws_caller_identity.current.account_id}:function:${var.function_name}:*",
47+
"arn:aws:lambda:us-east-1:${data.aws_caller_identity.current.account_id}:function:${var.function_name}:*",
48+
]
49+
}
50+
51+
dynamic "statement" {
52+
for_each = var.cf_distribution_id != null ? [] : [1]
53+
content {
54+
sid = "AllowCloudFrontUpdateDistributionAccess"
55+
56+
actions = [
57+
"cloudfront:UpdateDistribution",
58+
"cloudfront:GetDistribution",
59+
"cloudfront:GetDistributionConfig",
60+
]
61+
62+
resources = [
63+
"arn:aws:cloudfront::${data.aws_caller_identity.current.account_id}:distribution/${var.cf_distribution_id}",
64+
]
65+
66+
}
67+
}
68+
}

github_action.tf

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module "oidc_provider" {
55
count = var.create_github_actions_oidc_provider ? 1 : 0
66
}
77

8-
module "lamda_gha" {
8+
module "lambda_gha" {
99
source = "philips-labs/github-oidc/aws"
1010
version = "~> 0.7.0"
1111

@@ -27,10 +27,23 @@ module "lamda_gha" {
2727
] : []
2828
}
2929

30+
moved {
31+
from = module.lamda_gha
32+
to = module.lambda_gha
33+
}
34+
3035
resource "aws_iam_role_policy" "update_lambda" {
3136
count = var.create_github_actions_role ? 1 : 0
3237

3338
name_prefix = "UpdateLambda"
34-
role = module.lamda_gha[0].role.name
39+
role = module.lambda_gha[0].role.name
3540
policy = data.aws_iam_policy_document.update_lambda.json
3641
}
42+
43+
resource "aws_iam_role_policy" "update_lambda_edge" {
44+
count = var.create_github_actions_edge_role ? 1 : 0
45+
46+
name_prefix = "UpdateLambdaEdge"
47+
role = module.lambda_gha[0].role.name
48+
policy = data.aws_iam_policy_document.update_lambda_edge.json
49+
}

main.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ data "archive_file" "dummy" {
99

1010
module "lambda" {
1111
source = "terraform-aws-modules/lambda/aws"
12-
version = "~> 6.0.0"
12+
version = "~> 6.0.1"
1313

1414
function_name = var.function_name
1515
description = var.description
@@ -62,6 +62,7 @@ module "lambda" {
6262

6363
# dummy package, package is delegated to CI pipeline
6464
local_existing_package = data.archive_file.dummy.output_path
65+
source_path = var.source_path
6566
policies = var.managed_policy_arns
6667
policy_jsons = var.policy_jsons
6768
policy_json = var.policy_json

outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ output "lambda_role_unique_id" {
4141
}
4242

4343
# IAM Role for GHA
44-
output "lamda_gha_role_name" {
44+
output "lambda_gha_role_name" {
4545
description = "The crated role that can be assumed for the configured repository."
46-
value = try(module.lamda_gha[0].role.name, "")
46+
value = try(module.lambda_gha[0].role.name, "")
4747
}
4848

4949
# For Lambda Edge and Cloudfront Integration

variables.tf

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ variable "create_lambda_function_url" {
1616
default = false
1717
}
1818

19+
variable "create_github_actions_edge_role" {
20+
description = "controls whether to create for lambda edge functions"
21+
type = bool
22+
default = false
23+
}
24+
1925
variable "create_lambda_role" {
2026
description = "Controls whether the Lambda Role"
2127
type = bool
@@ -207,6 +213,12 @@ variable "snap_start" {
207213
default = false
208214
}
209215

216+
variable "source_path" {
217+
description = "The absolute path to a local file or directory containing your Lambda source code"
218+
type = string
219+
default = null
220+
}
221+
210222
############################################
211223
# Lambda Permissions (for allowed triggers)
212224
############################################
@@ -332,3 +344,13 @@ variable "cloudwatch_logs_retention_in_days" {
332344
type = number
333345
default = 7
334346
}
347+
348+
#############################
349+
# CloudFront (Edge Functions)
350+
#############################
351+
352+
variable "cf_distribution_id" {
353+
description = "distribution id to allow oidc role to update edge functions that are attached"
354+
type = string
355+
default = ""
356+
}

0 commit comments

Comments
 (0)