日本語

Batching Amazon Data Firehose Records into S3 with AWS Emulator

​​AWS Emulator​Amazon Data Firehose,Amazon S3,AWS CLI

Records sent to an Amazon Data Firehose (Firehose below) delivery stream are not written one at a time. Firehose buffers them for a while and then writes them to S3 as one object. I ran a pipeline that stores access and click logs in S3 on a local AWS Emulator, without an AWS account. I checked how much data has to accumulate before delivery, and what the delivered object's name and contents look like. Commands and output are copied as I ran them. Long output is shortened in places.

Note

The test environment was Ubuntu 24.04.5 (x86_64), AWS Emulator 1.0.757 (released 2026-11-28, Debian package), AWS CLI 2.42.5, and jq 1.7. I did not use an AWS account or credentials. I did not test the real AWS behavior, including the actual delivery timing, retries after a failed delivery, pricing, and throughput limits. Where the article mentions these, it relies on the official documentation. I did not test a setup that uses Kinesis Data Streams as the source, data transformation with Lambda, format conversion to Parquet or similar, or dynamic partitioning. The emulator's official documentation states that it does not evaluate IAM policies the same way AWS does.

Start the emulator and point the AWS CLI at it

I installed AWS Emulator from a Debian package, after verifying its signature, following the official Linux installation guide. I started it in another terminal (or in the background) with aws-emulator -t off. -t off turns off sending usage data (telemetry).

In the terminal where I run the AWS CLI, I set the environment variables from the official instructions for pointing SDKs and the CLI at the emulator. I added the final AWS_PAGER="" myself so that output would not stop in a pager.

bash
export AWS_ENDPOINT_URL=https://127.0.0.1:9297
export AWS_SHARED_CREDENTIALS_FILE=~/.aws/emulator/9297/emulator_credentials
export AWS_CONFIG_FILE=~/.aws/emulator/9297/config
export AWS_CA_BUNDLE=~/.aws/emulator/9297/ca.pem
export AWS_EC2_METADATA_DISABLED=true
export AWS_DISABLE_HOST_PREFIX_INJECTION=true
export AWS_REGION=us-east-1
export AWS_DEFAULT_REGION=us-east-1
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN AWS_PROFILE
export AWS_PAGER=""

Create the destination bucket and the role Firehose uses

Firehose writes to S3 using the IAM role specified for the delivery stream. First, I created the destination bucket and a role that Firehose can assume.

console
$ aws s3api create-bucket --bucket example-firehose-dest
{
    "Location": "/example-firehose-dest",
    "BucketArn": "arn:aws:s3:::example-firehose-dest"
}
$ cat trust.json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": { "Service": "firehose.amazonaws.com" },
      "Action": "sts:AssumeRole"
    }
  ]
}
$ aws iam create-role --role-name example-firehose-role --assume-role-policy-document file://trust.json --query Role.Arn
"arn:aws:iam::<12桁のアカウントID>:role/example-firehose-role"

I gave the role permission to write to the bucket. I copied only the S3 part of the list of actions from the official example of granting Firehose permission to write to S3. In the account ID placeholder, <12桁のアカウントID> stands for the fixed account ID that the emulator uses.

console
$ cat s3-write.json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "s3:AbortMultipartUpload",
        "s3:GetBucketLocation",
        "s3:GetObject",
        "s3:ListBucket",
        "s3:ListBucketMultipartUploads",
        "s3:PutObject"
      ],
      "Resource": [
        "arn:aws:s3:::example-firehose-dest",
        "arn:aws:s3:::example-firehose-dest/*"
      ]
    }
  ]
}
$ aws iam put-role-policy --role-name example-firehose-role --policy-name s3-write --policy-document file://s3-write.json

I test what the emulator does when this policy is missing later in the article.

Create a stream that batches by 1 MiB or 60 seconds

I wrote the delivery stream configuration to a JSON file. BufferingHints controls how much data to buffer before writing. According to the official BufferingHints reference, SizeInMBs is the buffer size in MiB (1 to 128, default 5), and IntervalInSeconds is the buffer time in seconds (0 to 900, default 300). If you specify one, you must also specify the other. Here I used 1 MiB and 60 seconds so that the results would appear quickly.

console
$ cat clicks.json
{
  "RoleARN": "arn:aws:iam::<12桁のアカウントID>:role/example-firehose-role",
  "BucketARN": "arn:aws:s3:::example-firehose-dest",
  "Prefix": "clicks/",
  "BufferingHints": { "SizeInMBs": 1, "IntervalInSeconds": 60 },
  "CompressionFormat": "UNCOMPRESSED"
}
$ aws firehose create-delivery-stream --delivery-stream-name example-clicks --delivery-stream-type DirectPut --extended-s3-destination-configuration file://clicks.json
{
    "DeliveryStreamARN": "arn:aws:firehose:us-east-1:<12桁のアカウントID>:deliverystream/example-clicks"
}
$ aws firehose describe-delivery-stream --delivery-stream-name example-clicks --query DeliveryStreamDescription.DeliveryStreamStatus
"CREATING"

--delivery-stream-type DirectPut specifies that the application sends records directly. Right after creation, the status was CREATING. According to the official CreateDeliveryStream reference, the stream accepts data only after it becomes ACTIVE, and sending to a stream that is not ACTIVE raises an exception. The AWS CLI firehose service had no wait command, so I waited with a loop that checked the status every second.

console
$ until [ "$(aws firehose describe-delivery-stream --delivery-stream-name example-clicks --query DeliveryStreamDescription.DeliveryStreamStatus --output text)" = ACTIVE ]; do sleep 1; done; echo ACTIVE
ACTIVE

Send records: add a newline at the end

To send records one at a time, I used put-record. To send them in a batch, I used put-record-batch. Both need --cli-binary-format raw-in-base64-out so that the string written in Data is sent as it is. AWS CLI v2 treats binary arguments as base64 strings by default. Without this option, the command failed as follows.

console
$ aws firehose put-record --delivery-stream-name example-clicks --record '{"Data":"{\"page\":\"/oops\"}"}'

aws: [ERROR]: Invalid base64: "{"page":"/oops"}"

Each JSON string ends with a newline (\n). The next section explains why.

console
$ aws firehose put-record --delivery-stream-name example-clicks --record '{"Data":"{\"page\":\"/top\"}\n"}' --cli-binary-format raw-in-base64-out
{
    "RecordId": "000000000000000018ec1a90849befe90000000000000001",
    "Encrypted": false
}
$ cat records.json
[
  {"Data": "{\"page\":\"/a\"}\n"},
  {"Data": "{\"page\":\"/b\"}\n"},
  {"Data": "{\"page\":\"/c\"}\n"}
]
$ aws firehose put-record-batch --delivery-stream-name example-clicks --records file://records.json --cli-binary-format raw-in-base64-out
{
    "FailedPutCount": 0,
    "Encrypted": false,
    "RequestResponses": [
        {
            "RecordId": "000000000000000018ec1a90b04c45680000000000000002"
        },
        ...
    ]
}

FailedPutCount in the put-record-batch response is the number of records that were not accepted. (The ... replaces the RecordId entries for the remaining two records.) The official PutRecordBatch reference says that with real AWS, some records can fail even when the call succeeds, and recommends resending only the records that have an ErrorCode. A single call accepts up to 500 records. In the emulator, sending a file with 501 records (501.json) returned a ValidationException.

console
$ aws firehose put-record-batch --delivery-stream-name example-clicks --records file://501.json --cli-binary-format raw-in-base64-out

aws: [ERROR]: An error occurred (ValidationException) when calling the PutRecordBatch operation: 1 validation error detected: Value at 'records' failed to satisfy constraint: Member must have length less than or equal to 500

Sending to a stream that does not exist, for example because of a mistyped stream name, returned a ResourceNotFoundException.

console
$ aws firehose put-record --delivery-stream-name example-missing --record '{"Data":"x\n"}' --cli-binary-format raw-in-base64-out

aws: [ERROR]: An error occurred (ResourceNotFoundException) when calling the PutRecord operation: Firehose not found under account <12桁のアカウントID>.

Four records arrived in one object in under a minute

Right after sending, the bucket was empty, and it was still empty 30 seconds later. I checked every second for a file to appear. It appeared at 04:20:14, 56 seconds after the time I printed just before sending the first record (04:19:18).

console
$ until [ -n "$(aws s3 ls s3://example-firehose-dest --recursive)" ]; do sleep 1; done; date -u +%H:%M:%S; aws s3 ls s3://example-firehose-dest --recursive
04:20:15
2026-11-28 04:20:14         58 clicks/2026/11/28/04/example-clicks-1-2026-11-28-04-20-14-91d2c9b3-c256-483b-af67-03f2b96be07f
$ K=$(aws s3api list-objects-v2 --bucket example-firehose-dest --query 'Contents[0].Key' --output text); aws s3 cp s3://example-firehose-dest/$K -
{"page":"/top"}
{"page":"/a"}
{"page":"/b"}
{"page":"/c"}

The four records I sent in two calls were in one object, one line per record, in the order I sent them.

Diagram. On the left are the four records sent. In the middle is the Firehose buffer (SizeInMBs and IntervalInSeconds), labeled in Japanese with the meaning "write when either of these comes first". On the right is the S3 object containing the four records combined. Below is the test result: with the 1 MiB and 60-second setting, the four records were combined and delivered in under a minute. With the 1 MiB and 900-second setting, about 0.58 MiB did not trigger delivery, and delivery happened once the total passed 1 MiB. (The labels in the image are in Japanese.)
Firehose buffers records and then writes them to S3 as one object

The object key starts with clicks/, followed by the date and hour as 2026/11/28/04/, and then delivery stream name-1-timestamp-random string. This matches the official page describing the S3 object name format: the time prefix YYYY/MM/dd/HH (UTC by default), and the pattern "stream name-stream version-year-month-day-hour-minute-second-UUID". The -1- is the stream version, which goes up by 1 each time you change the configuration.

Without a newline, JSON runs together on delivery

For the remaining streams in this article, I wrote JSON files with different Prefix and BufferingHints values, created each stream with create-delivery-stream without --delivery-stream-type, and waited for ACTIVE. In this section, example-nonl uses the prefix nonl/ and IntervalInSeconds set to 0 so that it delivers immediately.

The official PutRecordBatch reference says that a common approach is to add a delimiter such as a newline to the data, so that the receiving side can tell records apart. I sent two records in the emulator without newlines. Firehose added nothing between them, and the delivered file had them run together on one line. I omitted the loop that waits for the file to appear.

console
$ aws firehose put-record-batch --delivery-stream-name example-nonl --records '[{"Data":"{\"page\":\"/a\"}"},{"Data":"{\"page\":\"/b\"}"}]' --cli-binary-format raw-in-base64-out --query FailedPutCount
0
$ K=$(aws s3api list-objects-v2 --bucket example-firehose-dest --prefix nonl/ --query 'Contents[0].Key' --output text); aws s3 cp s3://example-firehose-dest/$K - > nonl.out; cat nonl.out; echo; jq -c . nonl.out
{"page":"/a"}{"page":"/b"}
{"page":"/a"}
{"page":"/b"}

The first line is the content of the delivered file. The following two lines are the output of jq -c ., which read the two records separately. However, a process that reads one record per line would see both records as one line. If the sender adds newlines, either way of reading the data works.

When the size limit comes first, delivery happens without waiting

I tested the size condition with a stream named example-size, which uses the prefix size/ and an IntervalInSeconds of 900 seconds (15 minutes). I sent 300 records, each with 2,000 characters of padding, for a total of 606,490 bytes. After waiting 5 seconds, nothing had been delivered. Then I sent the same 300 records again. The total went over 1 MiB (1,048,576 bytes), and delivery happened right away, without waiting 15 minutes. big.json is the file containing these 300 records. I omitted the command that created it.

console
$ date -u +%H:%M:%S; aws firehose put-record-batch --delivery-stream-name example-size --records file://big.json --cli-binary-format raw-in-base64-out --query FailedPutCount
04:20:31
0
$ sleep 5; aws s3 ls s3://example-firehose-dest/size/ --recursive
$ date -u +%H:%M:%S; aws firehose put-record-batch --delivery-stream-name example-size --records file://big.json --cli-binary-format raw-in-base64-out --query FailedPutCount
04:20:37
0
$ sleep 5; date -u +%H:%M:%S; aws s3 ls s3://example-firehose-dest/size/ --recursive
04:20:43
2026-11-28 04:20:38    1212980 size/2026/11/28/04/example-size-1-2026-11-28-04-20-37-50aeb46b-7053-47f9-9fe3-bd2cb7638bd8

The delivered object was 1,212,980 bytes and contained 600 lines from the two calls. It was not cut at exactly 1 MiB. Everything up to the point where the limit was exceeded was written together. Delivery happens when either the size or the time limit is reached first. The official BufferingHints reference also says that these two values are treated as hints, and that Firehose may use different values if it judges that to be better. So with real AWS, delivery may not happen at the times shown here.

Compress with GZIP and put the date in the prefix

I also tested compression and per-day folders, which are common when storing logs in S3. I set CompressionFormat to GZIP and put the expression !{timestamp:yyyy-MM-dd} in Prefix.

console
$ cat gz.json
{
  "RoleARN": "arn:aws:iam::<12桁のアカウントID>:role/example-firehose-role",
  "BucketARN": "arn:aws:s3:::example-firehose-dest",
  "Prefix": "logs/dt=!{timestamp:yyyy-MM-dd}/",
  "ErrorOutputPrefix": "logs-errors/!{firehose:error-output-type}/",
  "BufferingHints": { "SizeInMBs": 1, "IntervalInSeconds": 0 },
  "CompressionFormat": "GZIP"
}
$ aws firehose put-record-batch --delivery-stream-name example-gz --records file://records.json --cli-binary-format raw-in-base64-out --query FailedPutCount
0
$ aws s3 ls s3://example-firehose-dest/logs/ --recursive
2026-11-28 04:20:50         43 logs/dt=2026-11-28/example-gz-1-2026-11-28-04-20-50-854af9c1-c4dc-4a2a-b66e-bfc0631d2045.gz
$ K=$(aws s3api list-objects-v2 --bucket example-firehose-dest --prefix logs/ --query 'Contents[0].Key' --output text); aws s3 cp s3://example-firehose-dest/$K - | gunzip
{"page":"/a"}
{"page":"/b"}
{"page":"/c"}

The key was under logs/dt=2026-11-28/, and the default YYYY/MM/dd/HH prefix was not added. The file name ends with .gz. After I decompressed it with gunzip, the three records I sent could be read. According to the official custom prefix page, !{timestamp:...} is filled with the approximate time (UTC by default) when the oldest record in that object arrived.

The same page says that if you use an expression in Prefix, you must also specify ErrorOutputPrefix and include !{firehose:error-output-type} in it. It gives a configuration without ErrorOutputPrefix as an example of invalid input. However, in the emulator I could create a stream with a configuration (gz-noerr.json) where only the ErrorOutputPrefix line was removed.

console
$ aws firehose create-delivery-stream --delivery-stream-name example-gz2 --extended-s3-destination-configuration file://gz-noerr.json
{
    "DeliveryStreamARN": "arn:aws:firehose:us-east-1:<12桁のアカウントID>:deliverystream/example-gz2"
}

This is an example of a configuration that works in the emulator but might not work with real AWS. When you use an expression in Prefix, include ErrorOutputPrefix as the official rules require.

Without permission on the role, nothing is delivered and no error appears

I created a role without permission to write to S3 (example-noperm-role, with only a trust policy). Then I created a stream (example-noperm) that uses this role as RoleARN, with the prefix noperm/ and IntervalInSeconds set to 0, and sent one record. put-record succeeded, and the stream stayed ACTIVE. However, nothing was written to the bucket even after waiting 10 seconds.

console
$ aws firehose put-record --delivery-stream-name example-noperm --record '{"Data":"{\"page\":\"/noperm\"}\n"}' --cli-binary-format raw-in-base64-out --query RecordId
"000000000000000018ec1aa80f5d35d00000000000000262"
$ sleep 10; aws s3 ls s3://example-firehose-dest/noperm/ --recursive; echo "objects: $(aws s3 ls s3://example-firehose-dest/noperm/ --recursive | wc -l)"
objects: 0
$ aws firehose describe-delivery-stream --delivery-stream-name example-noperm --query DeliveryStreamDescription.DeliveryStreamStatus
"ACTIVE"

The AWS CLI returned no error. The only clue was in the emulator's own log, which is the output of the terminal where I started it. It repeatedly showed a warning that the write was skipped because of insufficient permissions. At this point, the warning had appeared 23 times.

console
$ grep -c 'so this delivery was not written' emulator.log
23
$ grep 'so this delivery was not written' emulator.log | tail -1 | jq -c '{level, message: .fields.message, bucket: .fields.bucket, key: .fields.key}'
{"level":"WARN","message":"the permissions in force do not allow this object's write, so this delivery was not written","bucket":"example-firehose-dest","key":"noperm/2026/11/28/04/example-noperm-1-2026-11-28-04-21-11-a3017248-..."}

emulator.log is a file where I saved the emulator's output. (I omitted the end of the key.) After I attached the s3-write.json policy I created earlier to the role, the record that had been waiting was delivered almost immediately.

console
$ aws iam put-role-policy --role-name example-noperm-role --policy-name s3-write --policy-document file://s3-write.json
$ until [ -n "$(aws s3 ls s3://example-firehose-dest/noperm/ --recursive)" ]; do sleep 1; done; aws s3 ls s3://example-firehose-dest/noperm/ --recursive
2026-11-28 04:21:13         19 noperm/2026/11/28/04/example-noperm-1-2026-11-28-04-21-13-70ba3145-2cf8-45aa-95f9-bd7b512bcf08

So the emulator did check the role's policy for Firehose writes. However, as noted at the start, the emulator's IAM evaluation is not the same as AWS's, so permissions must be verified on real AWS.

Warning

According to the official page on delivery failures, with real AWS, when the role lacks access to the bucket and similar problems occur, Firehose keeps retrying for up to 24 hours. Data is lost if the retries go beyond 24 hours. After you create a stream, confirm that files actually arrive in S3.

Clean up the stream and the bucket

When I finished testing, I deleted the streams and then deleted the bucket along with its contents.

console
$ for s in example-clicks example-nonl example-size example-gz example-gz2 example-noperm; do aws firehose delete-delivery-stream --delivery-stream-name $s; done; sleep 3; aws firehose list-delivery-streams
{
    "DeliveryStreamNames": [],
    "HasMoreDeliveryStreams": false
}
$ aws s3 rb s3://example-firehose-dest --force | tail -1
remove_bucket: example-firehose-dest

The two IAM roles remain. The emulator keeps everything it creates in memory, and everything is lost when it stops. Restarting it before each test gives you a clean start.

​Posted by 86