Skip to content

Commit

Permalink
feat(DMVP-1050): subscription module which can link topic with lambda (
Browse files Browse the repository at this point in the history
…#1)

* feat(DMVP-1050): subscription module which can link topic with lambda
* fix(DMVP-1050): adjust docs/examples
* chore(DMVP-1050): updated documentation
  • Loading branch information
aramkarapetian authored Aug 18, 2022
1 parent 4b3f14c commit 0363ffa
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 2 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,29 @@ git config core.hooksPath githooks
- terraform-docs
- terraform fmt
- pre-commit hooks

<!-- BEGIN_TF_DOCS -->
## Requirements

No requirements.

## Providers

No providers.

## Modules

No modules.

## Resources

No resources.

## Inputs

No inputs.

## Outputs

No outputs.
<!-- END_TF_DOCS -->
1 change: 1 addition & 0 deletions modules/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.terraform*
38 changes: 38 additions & 0 deletions modules/subscription/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 2.50.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.26.0 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [aws_sns_topic_subscription.user_updates_sqs_target](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic_subscription) | resource |
| [aws_lambda_alias.lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/lambda_alias) | data source |
| [aws_sns_topic.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/sns_topic) | data source |
| [aws_sqs_queue.example](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/sqs_queue) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_endpoint"></a> [endpoint](#input\_endpoint) | Endpoint of subscription messages will be delivered to (SQS name in case of sqs). | `any` | n/a | yes |
| <a name="input_protocol"></a> [protocol](#input\_protocol) | Protocol of subscription (lambda, sqs, ...). | `any` | n/a | yes |
| <a name="input_topic"></a> [topic](#input\_topic) | The name of SNS topic subscription should be attached to (not arn). | `any` | n/a | yes |

## Outputs

No outputs.
<!-- END_TF_DOCS -->
3 changes: 3 additions & 0 deletions modules/subscription/endpoint.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
locals {
endpoint = var.protocol == "lambda" ? data.aws_lambda_alias.lambda[0].arn : var.endpoint
}
Empty file.
30 changes: 30 additions & 0 deletions modules/subscription/examples/lambda.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module "topic" {
source = "terraform-aws-modules/sns/aws"
version = "~> 3.0"

name = "my-topic"
}

module "lambda" {
source = "terraform-aws-modules/lambda/aws"
version = "3.3.1"

function_name = "my-lambda1"
# description = "My awesome lambda function"
# handler = "index.lambda_handler"
# runtime = "python3.8"

source_path = "./lambda-source"
}

module "subscription" {
topic = "my-topic"
protocol = "lambda"
endpotin = "my-lambda1"

// this is necessary as otherwise resource will not be create
depends_on = [
module.topic,
module.lambda
]
}
5 changes: 5 additions & 0 deletions modules/subscription/lambda.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
data "aws_lambda_alias" "lambda" {
count = var.protocol == "lambda" ? 1 : 0
function_name = var.endpoint
name = var.endpoint
}
5 changes: 5 additions & 0 deletions modules/subscription/sqs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
data "aws_sqs_queue" "example" {
count = var.protocol == "sqs" ? 1 : 0

name = var.endpoint
}
5 changes: 5 additions & 0 deletions modules/subscription/subscription.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "aws_sns_topic_subscription" "user_updates_sqs_target" {
topic_arn = local.topic_arn
protocol = var.protocol
endpoint = local.endpoint
}
7 changes: 7 additions & 0 deletions modules/subscription/topic.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
locals {
topic_arn = data.aws_sns_topic.main.arn
}

data "aws_sns_topic" "main" {
name = var.topic
}
16 changes: 16 additions & 0 deletions modules/subscription/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
variable "topic" {
description = "The name of SNS topic subscription should be attached to (not arn)."
}

variable "protocol" {
description = "Protocol of subscription (lambda, sqs, ...)."

validation {
condition = can(regex("^lambda$|^sqs$", var.protocol))
error_message = "Protocol value must be in (lambda, sqs, ...)."
}
}

variable "endpoint" {
description = "Endpoint of subscription messages will be delivered to (SQS name in case of sqs)."
}
8 changes: 8 additions & 0 deletions modules/subscription/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 2.50.0"
}
}
}
37 changes: 37 additions & 0 deletions modules/topic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,40 @@ module "subscriptions" {
| <a name="input_create_sns_topic"></a> [create\_sns\_topic](#input\_create\_sns\_topic) | Bool topic | `bool` | `true` | no |
| <a name="input_sns_topic_subscriptions"></a> [sns\_topic\_subscriptions](#input\_sns\_topic\_subscriptions) | SNS Subscriptions | <pre>list(object({<br> name = string<br> topic_arn = string<br> protocol = string<br> endpoint = string<br> endpoint_auto_confirms = bool<br> }))</pre> | `[]` | no |
| <a name="input_topic_name"></a> [topic\_name](#input\_topic\_name) | SNS topic name. | `string` | `"topic"` | no |

<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 2.50.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 2.50.0 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [aws_sns_topic.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic) | resource |
| [aws_sns_topic_subscription.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic_subscription) | resource |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_create_sns_topic"></a> [create\_sns\_topic](#input\_create\_sns\_topic) | Bool topic | `bool` | `true` | no |
| <a name="input_sns_topic_subscriptions"></a> [sns\_topic\_subscriptions](#input\_sns\_topic\_subscriptions) | SNS Subscriptions | <pre>list(object({<br> name = string<br> topic_arn = string<br> protocol = string<br> endpoint = string<br> endpoint_auto_confirms = bool<br> }))</pre> | `[]` | no |
| <a name="input_topic_name"></a> [topic\_name](#input\_topic\_name) | SNS topic name. | `string` | `"topic"` | no |

## Outputs

No outputs.
<!-- END_TF_DOCS -->
4 changes: 2 additions & 2 deletions modules/topic/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
resource "aws_sns_topic" "this" {
count = var.create_sns_topic ? 1 : 0
name = "${var.topic_name}"
name = var.topic_name

delivery_policy = <<EOF
{
Expand Down Expand Up @@ -29,4 +29,4 @@ resource "aws_sns_topic_subscription" "this" {
protocol = each.value.protocol
endpoint = each.value.endpoint
endpoint_auto_confirms = each.value.endpoint_auto_confirms
}
}

0 comments on commit 0363ffa

Please sign in to comment.