-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transform): Add Send for AWS Lambda (#153)
* feat(transform): Add Send for AWS Lambda * docs(examples): Add S3 Retry on Failure
- Loading branch information
Showing
11 changed files
with
440 additions
and
8 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
13 changes: 13 additions & 0 deletions
13
examples/terraform/aws/s3/retry_on_failure/config/node/config.jsonnet
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,13 @@ | ||
// This config generates an error to engage the retry on failure feature. | ||
// The pipeline will retry forever until the error is resolved. Change the | ||
// transform to `sub.tf.send.stdout()` to resolve the error and print the logs | ||
// from S3. | ||
local sub = import '../../../../../../../build/config/substation.libsonnet'; | ||
|
||
{ | ||
concurrency: 1, | ||
transforms: [ | ||
sub.tf.util.err(settings={ message: 'simulating error to trigger retries' }), | ||
// sub.tf.send.stdout(), | ||
], | ||
} |
17 changes: 17 additions & 0 deletions
17
examples/terraform/aws/s3/retry_on_failure/config/retrier/config.jsonnet
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,17 @@ | ||
// This config transforms the failure record sent by the node Lambda function | ||
// so that it becomes a new request. The new request bypasses S3 and is sent | ||
// directly to the Lambda function. | ||
// | ||
// Additional information is available in the payload and can be used to make | ||
// decisions about the new request or notify external systems about the failure. | ||
local sub = import '../../../../../../../build/config/substation.libsonnet'; | ||
|
||
{ | ||
concurrency: 1, | ||
transforms: [ | ||
// If needed, then use other information from the failure record to | ||
// decide what to do or notify external systems about the failure. | ||
sub.tf.obj.cp(settings={ object: { source_key: 'requestPayload' } }), | ||
sub.tf.send.aws.lambda(settings={ function_name: 'node' }), | ||
], | ||
} |
4 changes: 4 additions & 0 deletions
4
examples/terraform/aws/s3/retry_on_failure/terraform/_provider.tf
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 @@ | ||
provider "aws" { | ||
# profile = "default" | ||
region = "us-east-1" | ||
} |
57 changes: 57 additions & 0 deletions
57
examples/terraform/aws/s3/retry_on_failure/terraform/_resources.tf
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,57 @@ | ||
module "appconfig" { | ||
source = "../../../../../../build/terraform/aws/appconfig" | ||
|
||
config = { | ||
name = "substation" | ||
environments = [{name = "example"}] | ||
} | ||
} | ||
|
||
module "ecr" { | ||
source = "../../../../../../build/terraform/aws/ecr" | ||
|
||
config = { | ||
name = "substation" | ||
force_delete = true | ||
} | ||
} | ||
|
||
resource "random_uuid" "s3" {} | ||
|
||
# Monolithic S3 bucket used to store all data. | ||
module "s3" { | ||
source = "../../../../../../build/terraform/aws/s3" | ||
|
||
config = { | ||
# Bucket name is randomized to avoid collisions. | ||
name = "${random_uuid.s3.result}-substation" | ||
} | ||
|
||
access = [ | ||
# Reads objects from the bucket. | ||
module.lambda_node.role.name, | ||
] | ||
} | ||
|
||
module "sqs" { | ||
source = "../../../../../../build/terraform/aws/sqs" | ||
|
||
config = { | ||
name = "substation_retries" | ||
|
||
# Delay for 30 seconds to allow the pipeline to recover. | ||
delay = 30 | ||
|
||
# Timeout should be at least 6x the timeout of any consuming Lambda functions, plus the batch window. | ||
# Refer to the Lambda documentation for more information: | ||
# https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html. | ||
timeout = 60 | ||
} | ||
|
||
access = [ | ||
# Sends messages to the queue. | ||
module.lambda_retrier.role.name, | ||
# Receives messages from the queue. | ||
module.lambda_node.role.name, | ||
] | ||
} |
86 changes: 86 additions & 0 deletions
86
examples/terraform/aws/s3/retry_on_failure/terraform/node_with_retrier.tf
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,86 @@ | ||
module "lambda_node" { | ||
source = "../../../../../../build/terraform/aws/lambda" | ||
appconfig = module.appconfig | ||
|
||
config = { | ||
name = "node" | ||
description = "Substation node that reads data from S3. The node will retry forever if it fails." | ||
image_uri = "${module.ecr.url}:latest" | ||
image_arm = true | ||
|
||
env = { | ||
"SUBSTATION_CONFIG" : "http://localhost:2772/applications/substation/environments/example/configurations/node" | ||
"SUBSTATION_LAMBDA_HANDLER" : "AWS_S3" | ||
"SUBSTATION_DEBUG" : true | ||
} | ||
} | ||
|
||
# The retrier Lambda must be able to invoke this | ||
# Lambda function to retry failed S3 events. | ||
access = [ | ||
module.lambda_retrier.role.name, | ||
] | ||
} | ||
|
||
resource "aws_lambda_permission" "node" { | ||
statement_id = "AllowExecutionFromS3Bucket" | ||
action = "lambda:InvokeFunction" | ||
function_name = module.lambda_node.name | ||
principal = "s3.amazonaws.com" | ||
source_arn = module.s3.arn | ||
} | ||
|
||
resource "aws_s3_bucket_notification" "node" { | ||
bucket = module.s3.id | ||
|
||
lambda_function { | ||
lambda_function_arn = module.lambda_node.arn | ||
events = ["s3:ObjectCreated:*"] | ||
} | ||
|
||
depends_on = [ | ||
aws_lambda_permission.node | ||
] | ||
} | ||
|
||
# Configures the Lambda function to send failed events to the SQS queue. | ||
resource "aws_lambda_function_event_invoke_config" "node" { | ||
function_name = module.lambda_node.name | ||
|
||
# This example disables the built-in retry mechanism. | ||
maximum_retry_attempts = 0 | ||
|
||
destination_config { | ||
on_failure { | ||
destination = module.sqs.arn | ||
} | ||
} | ||
} | ||
|
||
module "lambda_retrier" { | ||
source = "../../../../../../build/terraform/aws/lambda" | ||
appconfig = module.appconfig | ||
|
||
config = { | ||
name = "retrier" | ||
description = "Substation node that receives events from the retry queue and invokes the original Lambda function." | ||
image_uri = "${module.ecr.url}:latest" | ||
image_arm = true | ||
|
||
# This value should be 1/6th of the visibility timeout of the SQS queue. | ||
timeout = 5 | ||
|
||
env = { | ||
"SUBSTATION_CONFIG" : "http://localhost:2772/applications/substation/environments/example/configurations/retrier" | ||
"SUBSTATION_LAMBDA_HANDLER" : "AWS_SQS" | ||
"SUBSTATION_DEBUG" : true | ||
} | ||
} | ||
} | ||
|
||
resource "aws_lambda_event_source_mapping" "retrier" { | ||
event_source_arn = module.sqs.arn | ||
function_name = module.lambda_retrier.arn | ||
maximum_batching_window_in_seconds = 30 | ||
batch_size = 10 | ||
} |
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
Oops, something went wrong.