-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Working version of basic example (#12)
- Loading branch information
1 parent
66c80ed
commit ccb0517
Showing
18 changed files
with
273 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.