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

Fix lambda integration issue #7

Merged
merged 1 commit into from
Oct 3, 2023
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
5 changes: 5 additions & 0 deletions examples/sfn-state-machine-hello-world/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ module "state_machine" {
iam_role = {
enabled = true
}
service_integrations = {
"lambda" = {
enabled = true
}
}

tags = {
"project" = "terraform-aws-lambda-examples"
Expand Down
5 changes: 5 additions & 0 deletions examples/sfn-state-machine-logging/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ module "state_machine" {
iam_role = {
enabled = true
}
service_integrations = {
"lambda" = {
enabled = true
}
}


tags = {
Expand Down
5 changes: 5 additions & 0 deletions examples/sfn-state-machine-tracing/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ module "state_machine" {
iam_role = {
enabled = true
}
service_integrations = {
"lambda" = {
enabled = true
}
}


tags = {
Expand Down
1 change: 1 addition & 0 deletions modules/sfn-state-machine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ This module creates following resources.
| <a name="input_resource_group_description"></a> [resource\_group\_description](#input\_resource\_group\_description) | (Optional) The description of Resource Group. | `string` | `"Managed by Terraform."` | no |
| <a name="input_resource_group_enabled"></a> [resource\_group\_enabled](#input\_resource\_group\_enabled) | (Optional) Whether to create Resource Group to find and group AWS resources which are created by this module. | `bool` | `true` | no |
| <a name="input_resource_group_name"></a> [resource\_group\_name](#input\_resource\_group\_name) | (Optional) The name of Resource Group. A Resource Group name can have a maximum of 127 characters, including letters, numbers, hyphens, dots, and underscores. The name cannot start with `AWS` or `aws`. | `string` | `""` | no |
| <a name="input_service_integrations"></a> [service\_integrations](#input\_service\_integrations) | (Optional) A configuration of AWS service integrations to allow in the resource policy of the state machine. Supported AWS services are `lambda`. `service_integrations` as defined below.<br> (Optional) `lambda` - A configuration to integrate the state machine to AWS Lambda functions. `lambda` as defined below.<br> (Optional) `enabled` - Whether to enable the integration to AWS Lambda functions. | <pre>object({<br> lambda = optional(object({<br> enabled = optional(bool, false)<br> functions = optional(list(string), [])<br> }), {})<br> })</pre> | `{}` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | (Optional) A map of tags to add to all resources. | `map(string)` | `{}` | no |
| <a name="input_timeouts"></a> [timeouts](#input\_timeouts) | (Optional) How long to wait for the state machine to be created/updated/deleted. | <pre>object({<br> create = optional(string, "5m")<br> update = optional(string, "1m")<br> delete = optional(string, "5m")<br> })</pre> | `{}` | no |
| <a name="input_tracing"></a> [tracing](#input\_tracing) | (Optional) The configuration of AWS X-Ray tracing for the state machine. Step Functions will send traces to AWS X-Ray for state machine executions, even when a trace ID is not passed by an upstream service. Standard X-Ray charges apply. `tracing` as defined below.<br> (Optional) `enabled` - Whether to enable X-Ray tracing. | <pre>object({<br> enabled = optional(bool, false)<br> })</pre> | `{}` | no |
Expand Down
8 changes: 4 additions & 4 deletions modules/sfn-state-machine/iam.tf
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module "role" {
var.tracing.enabled ? {
"xray" = data.aws_iam_policy_document.xray[0].json,
} : {},
local.lambda_integration_detected ? {
var.service_integrations["lambda"].enabled ? {
"lambda" = data.aws_iam_policy_document.lambda[0].json,
} : {},
var.iam_role.inline_policies,
Expand Down Expand Up @@ -139,15 +139,15 @@ data "aws_iam_policy_document" "xray" {
###################################################

locals {
lambda_integration_functions = distinct(flatten(regexall(
lambda_integration_detected_functions = distinct(flatten(regexall(
"\"(arn:aws:lambda:[a-z0-9-]+:[0-9]+:function:[a-zA-Z0-9-_./]+)\"",
var.definition
)))
lambda_integration_detected = length(local.lambda_integration_functions) > 0
lambda_integration_functions = coalescelist(var.service_integrations["lambda"].functions, local.lambda_integration_detected_functions)
}

data "aws_iam_policy_document" "lambda" {
count = (!local.custom_iam_role_enabled && local.lambda_integration_detected) ? 1 : 0
count = (!local.custom_iam_role_enabled && var.service_integrations["lambda"].enabled) ? 1 : 0

statement {
sid = "InvokeLambdaFunctions"
Expand Down
16 changes: 16 additions & 0 deletions modules/sfn-state-machine/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ variable "iam_role" {
nullable = false
}

variable "service_integrations" {
description = <<EOF
(Optional) A configuration of AWS service integrations to allow in the resource policy of the state machine. Supported AWS services are `lambda`. `service_integrations` as defined below.
(Optional) `lambda` - A configuration to integrate the state machine to AWS Lambda functions. `lambda` as defined below.
(Optional) `enabled` - Whether to enable the integration to AWS Lambda functions.
EOF
type = object({
lambda = optional(object({
enabled = optional(bool, false)
functions = optional(list(string), [])
}), {})
})
default = {}
nullable = false
}

variable "logging" {
description = <<EOF
(Optional) The configuration to define what execution history events are logged and where they are logged. Standard Workflows record execution history in AWS Step Functions, although you can optionally configure logging to Amazon CloudWatch Logs. For Express state machines, you must enable logging to inspect and debug executions. `logging` as defined below.
Expand Down