Skip to content

Commit

Permalink
feat: Working version of basic example (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianczech authored Aug 12, 2024
1 parent 66c80ed commit ccb0517
Show file tree
Hide file tree
Showing 18 changed files with 273 additions and 77 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@ Currently below AWS services & resources are supported:

## Usage

## Examples
### Basic example

```bash
terraform init

terraform apply

awscurl --service lambda --region us-east-1 --header 'Content-Type: application/json' --header 'Accept: application/json' --data '{"message": "example_post", "key": "118", "transport": "mail"}' "$(terraform output -raw lambda_url_producer)"
```
29 changes: 21 additions & 8 deletions examples/basic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,42 @@
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.5.0 |
| <a name="requirement_archive"></a> [archive](#requirement\_archive) | ~> 2.0 |

## Providers

No providers.
| Name | Version |
|------|---------|
| <a name="provider_archive"></a> [archive](#provider\_archive) | ~> 2.0 |

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_dynamodb"></a> [dynamodb](#module\_dynamodb) | sebastianczech/free-serverless-modules/aws//modules/dynamodb | n/a |
| <a name="module_lambda"></a> [lambda](#module\_lambda) | sebastianczech/free-serverless-modules/aws//modules/lambda | n/a |
| <a name="module_sns"></a> [sns](#module\_sns) | sebastianczech/free-serverless-modules/aws//modules/sns | n/a |
| <a name="module_sqs"></a> [sqs](#module\_sqs) | sebastianczech/free-serverless-modules/aws//modules/sqs | n/a |
| <a name="module_dynamodb"></a> [dynamodb](#module\_dynamodb) | ../../modules/dynamodb | n/a |
| <a name="module_lambda_consumer"></a> [lambda\_consumer](#module\_lambda\_consumer) | ../../modules/lambda | n/a |
| <a name="module_lambda_producer"></a> [lambda\_producer](#module\_lambda\_producer) | ../../modules/lambda | n/a |
| <a name="module_sns"></a> [sns](#module\_sns) | ../../modules/sns | n/a |
| <a name="module_sqs"></a> [sqs](#module\_sqs) | ../../modules/sqs | n/a |

## Resources

No resources.
| Name | Type |
|------|------|
| [archive_file.consumer](https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file) | data source |
| [archive_file.producer](https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file) | data source |

## Inputs

No inputs.
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_iam_username"></a> [iam\_username](#input\_iam\_username) | The name of the IAM user | `string` | n/a | yes |
| <a name="input_mail"></a> [mail](#input\_mail) | The email address used for notifications | `string` | n/a | yes |
| <a name="input_prefix"></a> [prefix](#input\_prefix) | The prefix for the resources | `string` | n/a | yes |

## Outputs

No outputs.
| Name | Description |
|------|-------------|
| <a name="output_lambda_url_producer"></a> [lambda\_url\_producer](#output\_lambda\_url\_producer) | The URL of the Lambda producer |
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
Empty file removed examples/basic/files/code.py
Empty file.
Binary file removed examples/basic/files/code.zip
Binary file not shown.
42 changes: 42 additions & 0 deletions examples/basic/files/consumer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import json
import boto3
import ast


print('Loading function')
sns = boto3.client('sns')
dynamodb = boto3.client('dynamodb')
topic_url = "${topic_url}"
table_url = "${table_url}"


def lambda_handler(event, context):
for record in event['Records']:
payload = record["body"].replace("'",'"')
print("Received SQS message: " + payload)
data = json.loads(payload)
print("Converted data: " + str(data))

if "message" in data and "key" in data:
item = dynamodb.put_item(
TableName=table_url,
Item=
{
'ID': {
'S': str(data["key"]),
},
'message': {
'S': str(data["message"]),
}
}
)
print("Insert item into DynamoDB: " + item['ResponseMetadata']['RequestId'])

if "transport" in data and data["transport"] == "mail":
subject = "Message from SQS"
response = sns.publish(
TopicArn=topic_url,
Message=str(payload),
Subject=subject,
)
print("Send SNS event: " + response['MessageId'])
Binary file added examples/basic/files/consumer.zip
Binary file not shown.
70 changes: 70 additions & 0 deletions examples/basic/files/producer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import json
import urllib.parse
import boto3


# Function based on documentation:
# https://boto3.amazonaws.com/v1/documentation/api/latest/guide/sqs-example-sending-receiving-msgs.html


print('Loading function')
sqs = boto3.client('sqs')
queue_url = "${queue_url}"


def lambda_handler(event, context):
print("Received event: " + json.dumps(event, indent=2))

if 'body' in event:
message = str(json.loads(str(event['body'])))
elif 'queryStringParameters' in event:
message = str(event['queryStringParameters'])
else:
message = "empty message"

response = sqs.send_message(
QueueUrl=queue_url,
DelaySeconds=10,
MessageAttributes={
'Deployment': {
'DataType': 'String',
'StringValue': 'Terraform'
},
'Language': {
'DataType': 'String',
'StringValue': 'Python'
},
'Version': {
'DataType': 'Number',
'StringValue': '1'
},
'ARN': {
'DataType': 'String',
'StringValue': context.invoked_function_arn
},
'RequestID': {
'DataType': 'String',
'StringValue': context.aws_request_id
},
'Message': {
'DataType': 'String',
'StringValue': message
},
},
MessageBody=(
message
)
)

print("Send SQS message: " + response['MessageId'])

return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps({
"RequestID ": context.aws_request_id,
"ReceivedMessage": message
})
}
Binary file added examples/basic/files/producer.zip
Binary file not shown.
91 changes: 76 additions & 15 deletions examples/basic/main.tf
Original file line number Diff line number Diff line change
@@ -1,32 +1,93 @@
module "lambda" {
source = "sebastianczech/free-serverless-modules/aws//modules/lambda"
# https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file
data "archive_file" "producer" {
type = "zip"
source {
content = templatefile("files/producer.py", {
queue_url = module.sqs.id
})
filename = "producer.py"
}
output_path = "files/producer.zip"
}

# https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file
data "archive_file" "consumer" {
type = "zip"
source {
content = templatefile("files/consumer.py", {
topic_url = module.sns.id,
table_url = module.dynamodb.id
})
filename = "consumer.py"
}
output_path = "files/consumer.zip"
}

module "lambda_producer" {
source = "../../modules/lambda"

name = "${var.prefix}-lambda-producer"
iam_user_name = var.iam_username

filename = data.archive_file.producer.output_path
handler = "producer.lambda_handler"

name = "my-lambda"
iam_user_name = "my-iam-user"
sqs = {
arn = "arn:aws:sqs:us-east-1:123456789012:my-sqs"
url = "https://sqs.us-east-1.amazonaws.com/123456789012/my-sqs"
enabled = true
arn = module.sqs.arn
}

sns = {
enabled = false
}

dynamodb = {
enabled = false
}
}

module "lambda_consumer" {
source = "../../modules/lambda"

name = "${var.prefix}-lambda-consumer"
iam_user_name = var.iam_username

filename = data.archive_file.consumer.output_path
handler = "consumer.lambda_handler"

sqs = {
enabled = true
trigger_lambda = true
arn = module.sqs.arn
}

sns = {
enabled = true
arn = module.sns.arn
}

dynamodb = {
enabled = true
arn = module.dynamodb.arn
}
}

module "dynamodb" {
source = "sebastianczech/free-serverless-modules/aws//modules/dynamodb"
source = "../../modules/dynamodb"

name = "my-dynamodb"
read_capacity = 5
write_capacity = 5
name = "${var.prefix}-dynamodb"
}

module "sns" {
source = "sebastianczech/free-serverless-modules/aws//modules/sns"
source = "../../modules/sns"

name = "my-sns"
name = "${var.prefix}-sns"
protocol = "email"
endpoint = "[email protected]"
endpoint = var.mail
}

module "sqs" {
source = "sebastianczech/free-serverless-modules/aws//modules/sqs"
source = "../../modules/sqs"

name = "my-sqs"
name = "${var.prefix}-sqs"
}
4 changes: 4 additions & 0 deletions examples/basic/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "lambda_url_producer" {
description = "The URL of the Lambda producer"
value = module.lambda_producer.url
}
14 changes: 14 additions & 0 deletions examples/basic/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
variable "prefix" {
description = "The prefix for the resources"
type = string
}

variable "iam_username" {
description = "The name of the IAM user"
type = string
}

variable "mail" {
description = "The email address used for notifications"
type = string
}
7 changes: 7 additions & 0 deletions examples/basic/versions.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
terraform {
required_providers {
archive = {
source = "hashicorp/archive"
version = "~> 2.0"
}
}

required_version = ">= 1.5.0"
}
2 changes: 1 addition & 1 deletion modules/dynamodb/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
output "id" {
description = "The ID of the DynamoDB table"
value = aws_dynamodb_table.this
value = aws_dynamodb_table.this.id
}

output "arn" {
Expand Down
24 changes: 0 additions & 24 deletions modules/lambda/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions modules/lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.5.0 |
| <a name="requirement_archive"></a> [archive](#requirement\_archive) | ~> 2.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | ~> 5.61 |
| <a name="requirement_external"></a> [external](#requirement\_external) | ~> 2.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_archive"></a> [archive](#provider\_archive) | ~> 2.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | ~> 5.61 |

## Modules
Expand All @@ -35,10 +33,10 @@ No modules.
| [aws_iam_role_policy_attachment.lambda_logging](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_role_policy_attachment.lambda_sns](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_role_policy_attachment.lambda_sqs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_lambda_event_source_mapping.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_event_source_mapping) | resource |
| [aws_lambda_function.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function) | resource |
| [aws_lambda_function_url.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function_url) | resource |
| [aws_lambda_permission.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission) | resource |
| [archive_file.this](https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file) | data source |
| [aws_iam_policy_document.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_user.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_user) | data source |

Expand All @@ -47,10 +45,12 @@ No modules.
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_dynamodb"></a> [dynamodb](#input\_dynamodb) | The DynamoDB table to put items | <pre>object({<br> enabled = optional(bool, false)<br> arn = optional(string)<br> })</pre> | n/a | yes |
| <a name="input_filename"></a> [filename](#input\_filename) | The filename of the Lambda function | `string` | n/a | yes |
| <a name="input_handler"></a> [handler](#input\_handler) | The handler of the Lambda function | `string` | n/a | yes |
| <a name="input_iam_user_name"></a> [iam\_user\_name](#input\_iam\_user\_name) | The name of the IAM user | `string` | n/a | yes |
| <a name="input_name"></a> [name](#input\_name) | The name of the Lambda function | `string` | n/a | yes |
| <a name="input_sns"></a> [sns](#input\_sns) | The SNS topic to publish events | <pre>object({<br> enabled = optional(bool, false)<br> arn = optional(string)<br> })</pre> | n/a | yes |
| <a name="input_sqs"></a> [sqs](#input\_sqs) | The SQS queue to subscribe to the SNS topic | <pre>object({<br> enabled = optional(bool, false)<br> arn = optional(string)<br> url = optional(string)<br> })</pre> | n/a | yes |
| <a name="input_sqs"></a> [sqs](#input\_sqs) | The SQS queue to subscribe to the SNS topic | <pre>object({<br> enabled = optional(bool, false)<br> trigger_lambda = optional(bool, false)<br> arn = optional(string)<br> url = optional(string)<br> })</pre> | n/a | yes |

## Outputs

Expand Down
Loading

0 comments on commit ccb0517

Please sign in to comment.