Batch Jobs
Batch job is a computing resource - it runs your code. Batch job runs until your code finishes processing.
The execution of a batch job is initiated by an event (such as incoming request to HTTP API Gateway, message arriving to an SQS queue, or object being created in an S3 bucket)
Batch jobs can be configured to use spot instances, which can help you save up to 90% of computing costs.
Similarly to functions and container workloads, batch jobs are serverless and fully managed. This means you don't have to worry about administration tasks such as provisioning and managing servers, scaling, VM secority, OS security & much more.
Stacktape batch job consists of:
- User-defined Docker container (runs your code)
- Lambda function & State-machine (stacktape-managed, used to manage the lifecycle, integrations and execution of the batch job)
The container image can be supplied in 3 different ways:
- built automatically from your source code by Stacktape
- built using a supplied Dockerfile by Stacktape
- pre-built images
In addition to CPU and RAM, you can also configure a GPU for your batch job's environment.
When to use
Batch jobs are ideal for long-running and resource-demanding tasks, such as data-processing and ETL pipelines, training a machine-learning model, etc.
Advantages
- Pay-per-use - You only pay for the compute resources your jobs use.
- Resource flexibility - Whether your job requires 1 CPU or 50 CPUs, 1GiB or 128Gib of memory, the self-managed compute environment always meets your needs by spinning up the optimal instance to run your job.
- Time flexibility - Unlike functions, batch jobs can run indefinitely.
- Secure by default - Underlying environment is securely managed by AWS.
- Easy integration - batch-job can be invoked by events from a wide variety of services.
Disadvantages
- Slow start time - After a job execution is triggered, the job instance is put into an execution queue and can take anywhere from few seconds up to few minutes to start.
Basic usage
resources:myBatchJob:type: batch-jobproperties:container:imageConfig:filePath: path/to/my/batch-job.tsresources:cpu: 2memory: 1800events:- type: scheduleproperties:scheduleRate: cron(0 14 * * ? *) # every day at 14:00 UTC
(async () => {const event = JSON.parse(process.env.STP_TRIGGER_EVENT_DATA);// process the event})();
Container
- Batch jobs execution runs a Docker container inside a fully managed batch environment.
- You can configure the following properties of the container:
Configures an image for the container used in this batch job
Type: (FilePathBasedImage or BatchJobDockerfileBasedImage or BatchJobPrebuiltImage)
Environment variables injected to the batch job's environment
Type: Array of EnvironmentVar
- Environment variables are often used to inject information about other parts of the infrastrucutre (such as database URLs, secrets, etc.).
Image
- Docker container is a running instance of an Docker image.
- The image for your container can be supplied in 3 different ways:
Built from source code
- Stacktape bundles your source code with all of its dependencies and builds the image for you.
- During deployment, Stacktape pushes the image to the stack's private AWS container registry.
Path to the entry point of your workload (relative to the stacktape config file)
Type: string
- Stacktape tries to bundle all your source code with its dependencies into a single file.
- If a certain dependency doesn't support static bundling (because it has binary, uses dynamic require() calls, etc.), Stacktape will install it copy it to the bundle
Configuration of packaging properties specific to given language
Files that should be explicitly included in the deployment package (glob pattern)
Type: Array of string
- Example glob pattern:
images/*.jpg
Files that should be explicitly excluded from deployment package (glob pattern)
Type: Array of string
Example glob pattern: images/*.jpg
Dependencies to ignore.
Type: Array of string
- These dependencies won't be a part of your deployment package.
resources:mybatchJob:type: batch-jobproperties:container:imageConfig:filePath: path/to/my/batch-job.ts
Built from a Dockerfile
- Stacktape builds the image using the specified Dockerfile.
- During the deployment, Stacktape pushes the image to the stack's private AWS container registry.
- If you are not familiar with Docker or writing Dockerfiles, you can refer to Docker's official guide on writing Dockerfiles.
resources:myBatchJob:type: batch-jobproperties:container:imageConfig:dockerfilePath: src/DockerfilebuildContextPath: src
Path to directory (relative to stacktape config file) used as build context
Type: string
Path to Dockerfile (relative to buildContextPath
) used to build application image.
Type: string
List of arguments passed to the docker build
command when building the image
Type: Array of DockerBuildArg
Command to be executed when the container starts. Overrides CMD instruction in the Dockerfile.
Type: Array of string
Pre-built ahead of time
- Pre-built image from the Docker registry is used. Stacktape does not build the image for you.
Name or the URL of the image used in this workload.
Type: string
Command to be executed when the container starts. Overrides CMD instruction in the Dockerfile.
Type: Array of string
resources:myBatchJob:type: batch-jobproperties:container:imageConfig:image: mypublicrepo/my-containercommand: ['index.js']
Environment variables
Most commonly used types of environment variables:
- static - any value (will be stringified)
- result of a custom directive Learn more about directives
- referenced property of another resource (using $Param directive) Learn more about referencing resources
- secret (using $Secret directive) Learn more about using secrets
environment:STATIC_ENV_VAR: 'my-env-var'DYNAMICALLY_SET_ENV_VAR: "$MyCustomDirective('input-for-my-directive')"DB_HOST: "$Param('myPgSql', 'DbInstance::Endpoint.Address')"DB_PASSWORD: "$Secret('dbSecret.password')"
Pre-set environment variables
Stacktape pre-sets the following environment variables:
Name | Value |
---|---|
STP_TRIGGER_EVENT_DATA | Contains JSON stringified event from event integration that triggered this batch job. |
STP_MAXIMUM_ATTEMPTS | Absolute amount of attempts this batch-job gets, before it is considered failed. |
STP_CURRENT_ATTEMPT | Serial number of this attempt |
Logging
- Every time your code outputs (prints) something to the
stdout
orstderr
, your log will be captured and stored in a AWS CloudWatch log group. - You can browse your logs in 2 ways:
- go to your batch job's log-group in the AWS CloudWatch console. You can use
stacktape stack-info
command to get a direct link. - use stacktape logs command that will print logs to the console
- go to your batch job's log-group in the AWS CloudWatch console. You can use
Computing resources
- You can configure the amount of resource your batch job will have access to.
- In addition to CPU and RAM, batch jobs also allow you to configure GPU. To learn more about GPU instances, refer to AWS Docs.
- Behind the scenes, AWS Batch selects an instance type (from the C4, M4, and R4 instance families) that best fits the needs of the jobs with a preference for the lowest-cost instance type (BEST_FIT strategy).
If you define memory required for your batchjob in multiples of 1024 be aware:
Your self managed environment might spin up instances that are much bigger than
expected. This can happen because the instances in your environment need memory to handle the
management processes
(managed by AWS) associated with running the batch job.
Example: If you define 8192 memory for your batchjob, you might expect
that the self managed environment will primarily try to spin up
one of the instances from used families with memory
8GiB(8192MB). However, the self managed environment knows that instance with
such memory would not be sufficient for both the batch job and management
processes. As a result, it will try to spin up a bigger instance. To learn more about this issue, refer to
AWS Docs
Due to this behaviour, we advise to specify memory for your batchjobs smartly.
I.e instead of specifing 8192, consider specifing lower value, i.e 7680. This
way the self managed environment will be able to use instances with 8GiB
(8192MB) of memory, which can lead to cost saving.
If you define GPUs, instances are chosen according to your need from the GPU accelerated families:
Amount of virtual CPUs accessible to the batch job
Type: number
Amount of memory accessible to the batch job
Type: number
If you define memory required for your batchjob in multiples of 1024 be aware:
Your self managed environment might spin up instances that are much bigger than
expected. This can happen because the instances in your environment need memory to handle the
management processes
(managed by AWS) associated with running the batch job.
Example: If you define 8192 memory for your batchjob, you might expect
that the self managed environment will primarily try to spin up
one of the instances from used families with memory
8GiB(8192MB). However, the self managed environment knows that instance with
such memory would not be sufficient for both the batch job and management
processes. As a result, it will try to spin up a bigger instance. To learn more about this issue, refer to
AWS Docs
Due to this behaviour, we advise to specify memory for your batchjobs smartly.
I.e instead of specifing 8192, consider specifing lower value, i.e 7680. This
way the self managed environment will be able to use instances with 8GiB
(8192MB) of memory, which can lead to cost saving.
Number of physical GPUs accessible to the batch job
Type: number
If you define GPUs, instances are chosen according to your need from the GPU accelerated families:
resources:myBatchJob:type: batch-jobproperties:container:imageConfig:filePath: batch-jobs/js-batch-job.jsresources:cpu: 2memory: 1800events:- type: scheduleproperties:scheduleRate: 'cron(0 14 * * ? *)' # every day at 14:00 UTC
Spot instances
- Batch jobs can be configured to use spot instances.
- Spot instances leverage AWS's spare computing capacity and can cost up to 90% less than "onDemand" (normal) instances.
- However, your batch job can be interupted at any time, if AWS needs the capacity back. When this happens, your batch job receives a SIGTERM signal and you then have 120 seconds to save your progress or clean up.
- Interruptions are usually infrequent as can be seen in the AWS Spot instance advisor.
- To learn more about spot instances, refer to AWS Docs.
resources:myBatchJob:type: batch-jobproperties:container:imageConfig:filePath: path/to/my/batch-job.tsresources:cpu: 2memory: 1800useSpotInstances: true
Retries
- If the batch job exits with non-zero exit code (due to internal failure, timeout, spot instance interuption from AWS, etc.) and attempts are not exhausted, it can be retried.
Maximum number of times the batch job will try to execute before considered failed.
Type: number
- If the batch job exits with non-zero exit code (due to internal failure, timeout, spot instance interuption from AWS, etc.) and attempts are not exhausted, it will be retried.
- When there are no attempts left, the batch job is considered failed.
Amount of time (in seconds) to wait between the attempts.
Type: number
Multiplier for retryIntervalSeconds
Type: number
- Every time the batch job is retried, the amount of time between the executions will be multiplied by this number.
- This can be used to implement a backoff strategy.
Timeout
- When the timeout is reached, the batch job will be stopped.
- If the batch job fails and maximum attempts are not yet exhausted, it will be retried.
resources:myBatchJob:type: batch-jobproperties:container:imageConfig:filePath: path/to/my/batch-job.tsresources:cpu: 2memory: 1800timeout: 1200
Storage
- Each batch job instance has access to its own ephemeral storage. It's removed after the batch job finishes processing or fails.
- It has a fixed size of 20GB.
- To store data persistently, consider using Buckets.
Lifecycle process
The lifecycle of your batch job is fully managed. Stacktape leverages 2 extra resources to achieve this:
Trigger function
- Stacktape-managed AWS lambda function used to connect event integration to the batch job and start the execution of the batch job state machine
Batch job state machine
- Stacktape-managed AWS State machine used to control the lifecycle of the batch job container.
Batch job execution flow:
- Trigger function receives the event from one of its integrations
- Trigger function starts the execution of the batch job state machine
- Batch job state machine spawns the batch job instance (Docker container) and controls its lifecycle.
Trigger events
- Batch jobs are invoked ("triggered") in a reaction to an event.
- Each batch job can have multiple event integrations.
- Payload (data) received by the batch job depends on the event integration. It is accessible using the
STP_TRIGGER_EVENT_DATA
environment variable as a JSON stringified value.
- Be careful when connecting your batch jobs to event integrations that can spawn your batch job. Your batch job can get triggered many times a second, and this can get very costly.
- Example: connecting your batch job to an HTTP API Gateway and receiving 1000 HTTP requests will result in 1000 invocations.
HTTP Api event
- The batch job is triggered in a reaction to an incoming request to the specified HTTP API Gateway.
- HTTP API Gateway selects the route with the most-specific match. To learn more about how paths are evaluated, refer to AWS Docs
resources:myHttpApi:type: http-api-gatewaymyBatchJob:type: batch-jobproperties:container:imageConfig:filePath: path/to/my/batch-job.tsresources:cpu: 2memory: 1800events:- type: http-api-gatewayproperties:httpApiGatewayName: myHttpApipath: /hellomethod: GET
Lambda function connected to an HTTP API Gateway "myHttpApi"
Type of the event integration
Type: string "http-api-gateway"
Name of the HTTP API Gateway
Type: string
HTTP method that the request should match to be routed by this event integration
Type: string ENUM
Possible values: *DELETEGETHEADOPTIONSPATCHPOSTPUT
Can be either:
- exact method (e.g.
GET
orPUT
) - wildcard matching any method (
*
)
URL path that the request should match to be routed by this event integration
Type: string
Can be either:
- Exact URL Path - e.g.
/post
- Path with a positional parameter - e.g.
/post/{id}
. This matches anyid
parameter, e.g./post/6
. The parameter will be available to the workload usingevent.pathParameters.id
- Greedy path variable - e.g.
/pets/{anything+}
. This catches all child resources of the route. Example:/post/{anything+}
catches both/post/something/param1
and/post/something2/param
Configures authorization rules for this event integration
Type: (CognitoAuthorizer or LambdaAuthorizer)
- Only the authorized requests will be forwarded to the workload.
- All other requests will receive
{ "message": "Unauthorized" }
The format of the payload that the workload will receiveed with this integration.
Type: string ENUM
Possible values: 1.02.0
- To learn more about the differences between the formats, refer to AWS Docs
Schedule event
The batch job is triggered on a specified schedule. You can use 2 different schedule types:
- Fixed rate - Runs on a specified schedule starting after the event integration is successfully created in your stack. Learn more about rate expressions
- Cron expression - Leverages Cron time-based scheduler. Learn more about Cron expressions
resources:myBatchJob:type: batch-jobproperties:container:imageConfig:filePath: path/to/my/batch-job.tsresources:cpu: 2memory: 1800events:# invoke function every two hours- type: scheduleproperties:scheduleRate: rate(2 hours)# invoke function at 10:00 UTC every day- type: scheduleproperties:scheduleRate: cron(0 10 * * ? *)
Type of the event integration
Type: string "schedule"
Invocation schedule rate
Type: string
2 different formats are supported:
rate expression
- example:rate(2 hours)
orrate(20 seconds)
cron
- example:cron(0 10 * * ? *)
orcron(0 15 3 * ? *)
No description
Type: UNSPECIFIED
No description
Type: string
No description
Type: EventInputTransformer
Event Bus event
The batch job is triggered when the specified event bus receives an event matching the specified pattern.
2 types of event buses can be used:
Default event bus
- Default event bus is pre-created by AWS and shared by the whole AWS account.
- Can receive events from multiple AWS services. Full list of supported services.
- To use the default event bus, set the
useDefaultBus
property.
resources:myBatchJob:type: batch-jobproperties:container:imageConfig:filePath: path/to/my/batch-job.tsresources:cpu: 2memory: 1800events:- type: event-busproperties:useDefaultBus: trueeventPattern:source:- 'aws.autoscaling'region:- 'us-west-2'
Batch job connected to the default event bus
- Custom event bus
- Your own, custom Event bus.
- This event bus can receive your own, custom events.
- To use custom event bus, specify either
eventBusArn
oreventBusName
property.
resources:myEventBus:type: event-busmyBatchJob:type: batch-jobproperties:container:imageConfig:filePath: path/to/my/batch-job.tsresources:cpu: 2memory: 1800events:- type: event-busproperties:eventBusName: myEventBuseventPattern:source:- 'mycustomsource'
Batch job connected to a custom event bus
Type of the event integration
Type: string "event-bus"
Used to filter the events from the event bus based on a pattern
Type: EventBusIntegrationPattern
- Each event received by the Event Bus gets evaluated against this pattern. If the event matches this pattern, the integration invokes the workload.
- To learn more about the event bus filter pattern syntax, refer to AWS Docs
Arn of the event-bus
Type: string
- Use this, if you want to use an event bus defined outside of the stack resources.
- You need to specify exactly one of
eventBusArn
,eventBusName
oruseDefaultBus
.
Name of the Event Bus defined within the Stacktape resources
Type: string
- Use this, if you want to use an event bus defined within the stack resources.
- You need to specify exactly one of
eventBusArn
,eventBusName
oruseDefaultBus
.
Configures the integration to use the default (AWS created) event bus
Type: boolean
- You need to specify exactly one of
eventBusArn
,eventBusName
oruseDefaultBus
.
No description
Type: UNSPECIFIED
No description
Type: string
No description
Type: EventInputTransformer
SNS event
The batch job is triggered every time a specified SNS topic receives a new message.
- Amazon SNS is a fully managed messaging service for both application-to-application (A2A) and application-to-person (A2P) communication.
- Messages (notifications) are published to the topics
- To add your custom SNS topic to your stack, add Cloudformation resource to the cloudformationResources section of your config.
resources:myBatchJob:type: batch-jobproperties:container:imageConfig:filePath: path/to/my/batch-job.tsresources:cpu: 2memory: 1800events:- type: snsproperties:topicArn: $Param('mySnsTopic', 'Arn')onDeliveryFailure:sqsQueueArn: $Param('mySnsTopic', 'Arn')sqsQueueUrl: $Param('mySqsQueue', 'QueueURL')cloudformationResources:mySnsTopic:Type: AWS::SNS::TopicmySqsQueue:Type: AWS::SQS::Queue
Type of the event integration
Type: string "sns"
Arn of the SNS topic. Messages arriving to this topic will invoke the workload.
Type: string
No description
Type: UNSPECIFIED
SQS Destination for messages that fail to be delivered to the workload
Type: SnsOnDeliveryFailure
- Failure to deliver can happen in rare cases, i.e. when function is not able to scale fast enough to react to incoming messages.
SQS event
The function is triggered whenever there are messages in the specified SQS Queue.
- Messages are processed in batches
- If the SQS queue contains multiple messages, the batch job is invoked with multiple messages in its payload
- A single queue should always be "consumed" by a workload. SQS message can only be read once from the queue and while it's being processed, it's invisible to other workloads. If multiple different workloads are processing messages from the same queue, each will get their share of the messages, but one message won't be delivered to more than one workload at a time. If you need to consume the same message by multiple consumers (Fanout pattern), consider using EventBus integration or SNS integration.
- To add your custom SQS queue to your stack, simply add Cloudformation resource to the cloudformationResources section of your config.
Batching behavior can be configured. The batch job is triggered when any of the following things happen:
- Batch window expires. Batch window can be configured using
maxBatchWindowSeconds
property. - Maximum Batch size (amount of messages in the queue) is reached. Batch size can be configured using
batchSize
property. - Maximum Payload limit is reached. Maximum payload size is 6 MB.
resources:myBatchJob:type: batch-jobproperties:container:imageConfig:filePath: path/to/my/batch-job.tsresources:cpu: 2memory: 1800events:- type: sqsproperties:queueArn: $Param('mySqsQueue', 'Arn')cloudformationResources:mySqsQueue:Type: AWS::SQS::Queue
Type of the event integration
Type: string "sqs"
Arn of sqs queue from which function consumes messages.
Type: string
- Failure to deliver can happen in rare cases, i.e. when the workload is not able to scale fast enough to react to incoming messages.
Configures how many records to collect in a batch, before function is invoked.
Type: number
- Maximum
10,000
Configures maximum amount of time (in seconds) to gather records before invoking the workload
Type: number
- By default, the batch window is not configured
- Maximum 300 seconds
Kinesis event
The batch job is triggered whenever there are messages in the specified Kinesis Stream.
- Messages are processed in batches.
- If the stream contains multiple messages, the batch job is invoked with multiple messages in its payload.
- To add a custom Kinesis stream to your stack, simply add Cloudformation resource to the cloudformationResources section of your config.
- Similarly to SQS, Kinesis is used to process messages in batches. To learn the differences, refer to AWS Docs
Batching behavior can be configured. The batch job is triggered when any of the following things happen:
- Batch window expires. Batch window can be configured using
maxBatchWindowSeconds
property. - Maximum Batch size (amount of messages in the queue) is reached. Batch size can be configured using
batchSize
property. - Maximum Payload limit is reached. Maximum payload size is 6 MB.
Consoming messages from a kinesis stream can be done in 2 ways:
- Consuming directly from the stream - polling each shard in your Kinesis stream for records once per second. Read throughput of the kinesis shard is shared with other stream consumers.
- Consuming using a stream consumer - To minimize latency and maximize read throughput, use "stream consumer" with enhanced fan-out. Enhanced fan-out consumers get a dedicated connection to each shard that doesn't impact other applications reading from the stream. You can either pass reference to the consumer using consumerArn property, or you can let Stacktape auto-create consumer using autoCreateConsumer property.
resources:myBatchJob:type: batch-jobproperties:container:imageConfig:filePath: path/to/my/batch-job.tsresources:cpu: 2memory: 1800events:- type: kinesisproperties:autoCreateConsumer: truemaxBatchWindowSeconds: 30batchSize: 200streamArn: $Param('myKinesisStream', 'Arn')onFailure:arn: $Param('myOnFailureSqsQueue', 'Arn')type: sqscloudformationResources:myKinesisStream:Type: AWS::Kinesis::StreamProperties:ShardCount: 1myOnFailureSqsQueue:Type: AWS::SQS::Queue
Type of the event integration
Type: string "kinesis"
Arn of Kinesis stream from which function consumes records.
Type: string
Arn of the consumer which will be used by integration.
Type: string
- This parameter CAN NOT be used is combination with
autoCreateConsumer
Specifies whether to create separate consumer for this integration
Type: boolean
- Specifies whether Stacktape creates the consumer for this integration
- Using a consumer can help minimize latency and maximize read throughput
- To learn more about stream consumers, refer to AWS Docs
- This parameter CAN NOT be used when in combination with
consumerArn
Configures maximum amount of time (in seconds) to gather the records before invoking the workload
Type: number
- By default batch window is not configured
- Maximum
300
seconds
Configures how many records to collect in a batch, before function is invoked.
Type: number
- Maximum
10,000
- @default 10
Specifies position in the stream from which to start reading.
Type: string ENUM
Possible values: LATESTTRIM_HORIZON
Available values are:
LATEST
- Read only new records.TRIM_HORIZON
- Process all available records
Configures the number of times failed "record batches" are retried
Type: number
- If the workload fails, the entire batch of records is retried (not only the failed ones). This means that even the records that you processed successfully can get retried. You should implement your function with idempotency in mind.
Configures the on-failure destination for failed record batches
Type: DestinationOnFailure
SQS queue
orSNS topic
Allows to process more than one shard of the stream simultaneously
Type: number
If the workload returns an error, split the batch in two before retrying.
Type: boolean
- This can help in cases, when the failure happened because the batch was too large to be successfully processed.
DynamoDb event
The batch job is triggered whenever there are processable records in the specified DynamoDB streams.
- DynamoDB stream captures a time-ordered sequence of item-level modifications in a DynamoDB table and durably stores the information for up to 24 hours.
- Records from the stream are processed in batches. This means that multiple records are included in a single batch job invocation.
- DynamoDB stream must be enabled in a DynamoDB table definition. Learn how to enable streams in dynamo-table docs
resources:myDynamoDbTable:type: dynamo-db-tableproperties:primaryKey:partitionKey:attributeName: idattributeType: stringdynamoStreamType: NEW_AND_OLD_IMAGESmyBatchJob:type: batch-jobproperties:container:imageConfig:filePath: path/to/my/batch-job.tsresources:cpu: 2memory: 1800events:- type: dynamo-dbproperties:streamArn: $Param('myDynamoDbTable', 'DynamoTable::StreamArn')# OPTIONALbatchSize: 200
Type of the event integration
Type: string "dynamo-db"
Arn of the DynamoDb table stream from which the workload consumes records.
Type: string
Configures maximum amount of time (in seconds) to gather records before invoking the workload
Type: number
- By default, the batch window is not configured
Configures how many records to collect in a batch, before the workload is invoked.
Type: number
- Maximum
1000
Specifies position in the stream from which to start reading.
Type: string
Available values are:
LATEST
- Read only new records.TRIM_HORIZON
- Process all available records
Configures the number of times failed "record batches" are retried
Type: number
- If the workload fails, the entire batch of records is retried (not only the failed ones). This means that even the records that you processed successfully can get retried. You should implement your function with idempotency in mind.
Configures the on-failure destination for failed record batches
Type: DestinationOnFailure
SQS queue
orSNS topic
Allows to process more than one shard of the stream simultaneously
Type: number
If the workload returns an error, split the batch in two before retrying.
Type: boolean
- This can help in cases, when the failure happened because the batch was too large to be successfully processed.
S3 event
The batch job is triggered when a specified event occurs in your bucket.
Supported events are listed in the
s3EventType
API Reference.To learn more about the even types, refer to AWS Docs.
resources:myBucket:type: bucketmyBatchJob:type: batch-jobproperties:container:imageConfig:filePath: path/to/my/batch-job.tsresources:cpu: 2memory: 1800events:- type: s3properties:bucketArn: $Param('myBucket', 'Bucket::Arn')s3EventType: 's3:ObjectCreated:*'filterRule:prefix: order-suffix: .jpg
Type of the event integration
Type: string "s3"
Arn of the S3 bucket, events of which can invoke the workload
Type: string
Specifies which event types invokes the workload
Type: string ENUM
Possible values: s3:ObjectCreated:*s3:ObjectCreated:CompleteMultipartUploads3:ObjectCreated:Copys3:ObjectCreated:Posts3:ObjectCreated:Puts3:ObjectRemoved:*s3:ObjectRemoved:Deletes3:ObjectRemoved:DeleteMarkerCreateds3:ObjectRestore:*s3:ObjectRestore:Completeds3:ObjectRestore:Posts3:ReducedRedundancyLostObjects3:Replication:*s3:Replication:OperationFailedReplications3:Replication:OperationMissedThresholds3:Replication:OperationNotTrackeds3:Replication:OperationReplicatedAfterThreshold
Allows to filter the objects that can invoke the workload
Type: S3FilterRule
Prefix of the object which can invoke function
Type: string
Suffix of the object which can invoke function
Type: string
Cloudwatch Log event
The batch job is triggered when a log record arrives to the specified log group.
- Event payload arriving to the batch job is BASE64 encoded and has the following format:
{ "awslogs": { "data": "BASE64ENCODED_GZIP_COMPRESSED_DATA" } }
- To read access the log data, event payload needs to be decoded and decompressed first.
resources:myLogProducingLambda:type: functionproperties:packageConfig:filePath: lambdas/log-producer.tsmyBatchJob:type: batch-jobproperties:container:imageConfig:filePath: path/to/my/batch-job.tsresources:cpu: 2memory: 1800events:- type: cloudwatch-logproperties:logGroupArn: $Param('myLogProducingLambda', 'LogGroup::Arn')
Type of the event integration
Type: string "cloudwatch-log"
Arn of the watched Log group
Type: string
Allows to filter the logs that invoke the workload based on a pattern
Type: string
- To learn more about the filter pattern, refer to AWS Docs
Application Load Balancer event
The batch job is triggered when a specified Application load Balancer receives an HTTP request that matches the integration's conditions.
- You can filter requests based on HTTP Method, Path, Headers, Query parameters, and IP Address.
resources:# load balancer which routes traffic to the functionmyLoadBalancer:type: application-load-balancerproperties:listeners:- port: 80protocol: HTTPmyBatchJob:type: batch-jobproperties:container:imageConfig:filePath: path/to/my/batch-job.tsresources:cpu: 2memory: 1800events:- type: application-load-balancerproperties:# referencing load balancer defined abovepriority: 1loadBalancerName: myLoadBalancerlistenerPort: 80paths:- /invoke-my-lambda- /another-path
Type of the event integration
Type: string "application-load-balancer"
Name of the Load balancer
Type: string
Port of the Load balancer listener
Type: number
Priority of the integration
Type: number
- Load balancers evaluate integrations according to priority.
- If multiple event integrations match the same conditions (paths, methods ...), request will be forwarded to the event integration with the highest priority.
List of URL paths that the request should match to be routed by this event integration
Type: Array of string
- The condition is satisfied if any of the paths matches the request URL
- The maximum size is 128 characters
- The comparison is case sensitive
The following patterns are supported:
- basic URL path, i.e.
/post
*
- wildcard (matches 0 or more characters)?
- wildcard (matches 1 or more characters)
List of HTTP methods that the request should match to be routed by this event integration
Type: Array of string
List of hostnames that the request should match to be routed by this event integration
Type: Array of string
- Hostname is parsed from the host header of the request
The following wildcard patterns are supported:
*
- wildcard (matches 0 or more characters)?
- wildcard (matches 1 or more characters)
List of header conditions that the request should match to be routed by this event integration
Type: Array of LbHeaderCondition
- All conditions must be satisfied.
List of query parameters conditions that the request should match to be routed by this event integration
Type: Array of LbQueryParamCondition
- All conditions must be satisfied.
List of IP addresses that the request should match to be routed by this event integration
Type: Array of string
- IP addresses must be in a CIDR format.
- If a client is behind a proxy, this is the IP address of the proxy, not the IP address of the client.
Accessing other resources
For most of the AWS resources, resource-to-resource communication is not allowed by default. This helps to enforce security and resource isolation. Access must be explicitly granted using IAM (Identity and Access Management) permissions.
Access control of Relational Databases is not managed by IAM. These resources are not "cloud-native" by design and have their own access control mechanism (connection string with username and password). They are accessible by default, and you don't need to grant any extra IAM permissions. You can further restrict the access to your relational databases by configuring their access control mode.
Stacktape automatically handles IAM permissions for the underlying AWS services that it creates (i.e. granting functions permission to write logs to Cloudwatch, allowing functions to communicate with their event source and many others).
If your workload needs to communicate with other infrastructure components, you need to add permissions manually. You can do this in 2 ways:
Using allowAccessTo
- List of resource names that this function will be able to access (basic IAM permissions will be granted automatically). Granted permissions differ based on the resource.
- Works only for resources managed by Stacktape (not arbitrary Cloudformation resources)
- This is useful if you don't want to deal with IAM permissions yourself. Handling permissions using raw IAM role statements can be cumbersome, time-consuming and error-prone.
resources:photosBucket:type: bucketmyBatchJob:type: batch-jobproperties:container:imageConfig:filePath: path/to/my/batch-job.tsaccessControl:allowAccessTo:- photosBucket
Granted permissions:
Bucket
- list objects in a bucket
- create / get / delete / tag object in a bucket
DynamoDb Table
- get / put / update / delete item in a table
- scan / query a table
- describe table stream
MongoDb Atlas Cluster
- Allows connection to a cluster with
accessibilityMode
set toscoping-workloads-in-vpc
. To learn more about MongoDb Atlas clusters accessibility modes, refer to MongoDB Atlas cluster docs.
Relational database
- Allows connection to a relational database with
accessibilityMode
set toscoping-workloads-in-vpc
. To learn more about relational database accessibility modes, refer to Relational databases docs.
Redis cluster
- Allows connection to a redis cluster with
accessibilityMode
set toscoping-workloads-in-vpc
. To learn more about redis cluster accessibility modes, refer to Redis clusters docs.
Event bus
- publish events to the specified Event bus
Function
- invoke the specified function
Batch job
- submit batch-job instance into batch-job queue
- list submitted job instances in a batch-job queue
- describe / terminate a batch-job instance
- list executions of state machine which executes the batch-job according to its strategy
- start / terminate execution of a state machine which executes the batch-job according to its strategy
Using iamRoleStatements
- List of raw IAM role statement objects. These will be appended to the function's role.
- Allow you to set granular control over your function's permissions.
- Can be used to give access to any Cloudformation resource
resources:myBatchJob:type: batch-jobproperties:resources:cpu: 2memory: 1800container:imageConfig:filePath: path/to/my/batch-job.tsaccessControl:iamRoleStatements:- Resource:- $Param('NotificationTopic', 'Arn')Effect: AllowAction:- 'sns:Publish'cloudformationResources:NotificationTopic:Type: AWS::SNS::Topic
Default VPC connection
- Certain AWS services (such as MongoDb Atlas Clusters) must be connected to a
VPC (Virtual private cloud) to be able to run. For stacks that include these resources, Stacktape
does 2 things:
- creates a default VPC
- connects the VPC-requiring resources to the default VPC.
- Batch jobs are connected to the default VPC of your stack by default. This means that batch jobs can communcate with
resources that have their accessibility mode set to
vpc
without any extra configuration. - To learn more about VPCs and accessibility modes, refer to VPC docs, accessing relational databases, accessing redis clusters and accessing MongoDb Atlas clusters.
Pricing
- You are charged for the instances running in your batch job compute environment.
- Instance sizes are automatically chosen to best suit the needs of your batch jobs.
- You are charged only for the time your batch job runs. After your batch job finishes processing, the instances are automatically killed.
- Price depends on region and instance used. (https://aws.amazon.com/ec2/pricing/on-demand/)
- You can use spot instances to save costs. These instances can be up to 90% cheaper. (https://aws.amazon.com/ec2/spot/pricing/)
- You are also paying a very neglibile price for lambda functions and state machines used to manage the execution and integrations of your batch job.
API reference
No description
Type: string "batch-job"
Configures properties of the Docker container that will run in this batch job.
Type: BatchJobContainer
Configures computing resources for this batch job.
Type: BatchJobResources
Maximum number of seconds the batch job is allowed to run.
Type: number
- When the timeout is reached, the batch job will be stopped.
- If the batch job fails and maximum attempts are not yet exhausted, it will be retried.
Configures the batch job to use spot instances
Type: boolean
- Batch jobs can be configured to use spot instances.
- Spot instances leverage AWS's spare computing capacity and can cost up to 90% less than "onDemand" (normal) instances.
- However, your batch job can be interupted at any time, if AWS needs the capacity back. When this happens, your batch job receives a SIGTERM signal and you then have 120 seconds to save your progress or clean up.
- Interruptions are usually infrequent as can be seen in the AWS Spot instance advisor.
- To learn more about spot instances, refer to AWS Docs.
Configures logging behavior for the batch job
Type: BatchJobLogging
- Container logs (stdout and stderr) are automatically sent to a pre-created CloudWatch log group.
- By default, logs are retained for 180 days.
- You can browse your logs in 2 ways:
- go to the log group page in the AWS CloudWatch console. You can use
stacktape stack-info
command to get a direct link. - use stacktape logs command to print logs to the console
- go to the log group page in the AWS CloudWatch console. You can use
Configures retries for the batch job
Type: BatchJobRetryConfiguration
- If the batch job exits with non-zero exit code (due to internal failure, timeout, spot instance interuption from AWS, etc.) and attempts are not exhausted, it can be retried.
Configures events (triggers) that will trigger the execution of this batch job.
Type: Array of (LoadBalancerIntegration or SnsIntegration or SqsIntegration or KinesisIntegration or DynamoDbIntegration or S3Integration or ScheduleIntegration or CloudwatchLogIntegration or HttpApiIntegration or EventBusIntegration)
- Triggering of batch jobs leverages
trigger functions
(special purpose lambda functions). - Event integrations are attached to the
trigger function
Configures access to other resources of your stack (such as relational-databases, buckets, event-buses, etc.).
Type: AccessControl
Overrides one or more properties of the specified child resource.
Type: Object
- Child resouces are specified using their descriptive name (e.g.
DbInstance
orEvents.0.HttpApiRoute
). - To see all configurable child resources for given Stacktape resource, use
stacktape stack-info --detailed
command. - To see the list of properties that can be overriden, refer to AWS Cloudformation docs.
Argument name
Type: string
Argument value
Type: string
No description
Type: string
No description
Type: UNSPECIFIED
No description
Type: UNSPECIFIED
No description
Type: UNSPECIFIED
No description
Type: UNSPECIFIED
No description
Type: UNSPECIFIED
No description
Type: UNSPECIFIED
No description
Type: UNSPECIFIED
No description
Type: UNSPECIFIED
No description
Type: UNSPECIFIED
Arn of the SQS queue
Type: string
Url of the SQS queue
Type: string
Arn of the SNS topic or SQS queue into which failed record batches are sent
Type: string
Type of destination being used are using
Type: string ENUM
Possible values: snssqs
Prefix of the object which can invoke function
Type: string
Suffix of the object which can invoke function
Type: string
Header name
Type: string
List of header values
Type: Array of string
- The Condition is satisfied if at least one of the request headers matches the values in this list.
Name of the query parameter
Type: string
List of query parameter values
Type: Array of string
- The Condition is satisfied if at least one of the request query parameters matches the values in this list.
Builds image with support for glibc-based binaries
Type: boolean
-
- You can use this option to add support for glibc-based native dependencies.
- This means that Stacktape will use different (and significantly larger) base-image for your container.
- Stacktape uses alpine Docker images by default. These images use musl, instead of glibc.
- Packages with C-based binaries compiled using glibc doesn't work with musl.
Path to tsconfig.json file to use.
Type: string
This is used mostly to resolve path aliases.
Emits decorator metadata to the final bundle.
Type: boolean
- This is used by frameworks like NestJS or ORMs like TypeORM.
- This is not turned on by default, because it can slow down the build process.
Dependencies to exclude from main bundle.
Type: Array of string
- These dependencies will be treated as
external
and won't be statically built into the main bundle - Instead, they will be installed and copied to the deployment package.
- Using
*
means all of the workload's dependencies will be treated as external
Type of the event integration
Type: string "kinesis"
Arn of Kinesis stream from which function consumes records.
Type: string
Arn of the consumer which will be used by integration.
Type: string
- This parameter CAN NOT be used is combination with
autoCreateConsumer
Specifies whether to create separate consumer for this integration
Type: boolean
- Specifies whether Stacktape creates the consumer for this integration
- Using a consumer can help minimize latency and maximize read throughput
- To learn more about stream consumers, refer to AWS Docs
- This parameter CAN NOT be used when in combination with
consumerArn
Configures maximum amount of time (in seconds) to gather the records before invoking the workload
Type: number
- By default batch window is not configured
- Maximum
300
seconds
Configures how many records to collect in a batch, before function is invoked.
Type: number
- Maximum
10,000
- @default 10
Specifies position in the stream from which to start reading.
Type: string ENUM
Possible values: LATESTTRIM_HORIZON
Available values are:
LATEST
- Read only new records.TRIM_HORIZON
- Process all available records
Configures the number of times failed "record batches" are retried
Type: number
- If the workload fails, the entire batch of records is retried (not only the failed ones). This means that even the records that you processed successfully can get retried. You should implement your function with idempotency in mind.
Configures the on-failure destination for failed record batches
Type: DestinationOnFailure
SQS queue
orSNS topic
Allows to process more than one shard of the stream simultaneously
Type: number
If the workload returns an error, split the batch in two before retrying.
Type: boolean
- This can help in cases, when the failure happened because the batch was too large to be successfully processed.
Name of the environment variable
Type: string
Value of the environment variable
Type: (string or number or boolean)
Raw AWS IAM role statements appended to your resources's role.
Type: Array of StpIamRoleStatement
Names of the resources that will recieve basic permissions.
Type: Array of string
Granted permissions:
Bucket
- list objects in a bucket
- create / get / delete / tag object in a bucket
DynamoDb Table
- get / put / update / delete item in a table
- scan / query a table
- describe table stream
MongoDb Atlas Cluster
- Allows connection to a cluster with
accessibilityMode
set toscoping-workloads-in-vpc
. To learn more about MongoDb Atlas clusters accessibility modes, refer to MongoDB Atlas cluster docs.
Relational database
- Allows connection to a relational database with
accessibilityMode
set toscoping-workloads-in-vpc
. To learn more about relational database accessibility modes, refer to Relational databases docs.
Redis cluster
- Allows connection to a redis cluster with
accessibilityMode
set toscoping-workloads-in-vpc
. To learn more about redis cluster accessibility modes, refer to Redis clusters docs.
Event bus
- publish events to the specified Event bus
Function
- invoke the specified function
Batch job
- submit batch-job instance into batch-job queue
- list submitted job instances in a batch-job queue
- describe / terminate a batch-job instance
- list executions of state machine which executes the batch-job according to its strategy
- start / terminate execution of a state machine which executes the batch-job according to its strategy
No description
Type: UNSPECIFIED