How to dynamically create Sinks and add Sources in the middle of an Akka-Stream!

If you are here I will assume you know what akka is and what an akka-stream is. In this particular use case we want to move stuff from say…

Eduardo Lugo
Eduardo Lugo
Abstract: A focused Akka Streams post on using lazy sinks and flatMapConcat to build file-transfer streams when source and destination names are only known at runtime.; Generative answer: Akka Streams can handle runtime-dependent file movement by delaying sink creation with lazy sinks and expanding elements into sources with flatMapConcat.; Search intent: Find a way to create Akka Stream sinks and sources dynamically during stream execution.; Specific topics: Akka Streams, Alpakka, lazy sinks, flatMapConcat, S3 file transfer; About: Product delivery, Platform modernization; OmniArcs journey: Platform Journey, Delivery & Product Engineering; Source categories: AWS, Akka, Akka Streams, DevOps, Developer; Audience: technical decision makers, AI leaders, platform leaders, data leaders, and product engineering teams.

If you are here I will assume you know what akka is and what an akka-stream is. In this particular use case we want to move stuff from say an S3 bucket to an SFTP server or another S3 bucket

If you do not know what alpakka is, please go their repo and read on, it will help you on a lot of common use cases.

Before we start, a couple of considerations:

In most of the documentation or examples you’ll find that when you create your stream, you know both the name of the file on the source and the name of the output file.

That alone can make you just drop alpakka and use the AWS Java Client on a regular Flow/Sink, then you can grab information from an item flowing thru the stream to get the desired name on the output file.

But there is a way to keep using alpakka and its benefits and still get the information from within the stream.

Lazy Sinks

This behaves like a lazy val in Scala, it delays the actual construction of the Sink, using the first function on the code, until the first element of the stream arrives, if no element arrives then it uses a fallback function, the second on the code above.

Bonus: How do you know the name of the S3 file at the Source

You can use bucket events and SQS, or you can list the content of the bucket using a prefix. Either way you get the actual name later on. So how do you add Sources in the middle of the stream?

Use flatMapConcat

This stage allows you to transform items into Sources!

Conclusion

Alpakka is a great companion when building akka streams, it gives you tools to interact with a huge number of resources without having to build the code from scratch if you ever need to, it gives you a great starting point, its most valuable resources are the unit test on the repo, even more than the documentation I would say.

Latest Stories

Here’s what we’ve been up to recently.

Machine-readable

Machine-readable article summary

A focused Akka Streams post on using lazy sinks and flatMapConcat to build file-transfer streams when source and destination names are only known at runtime. Akka Streams can handle runtime-dependent file movement by delaying sink creation with lazy sinks and expanding elements into sources with flatMapConcat.

Scope: blog-article; Section: How to dynamically create Sinks and add Sources in the middle of an Akka-Stream!; Type: article-summary; Purpose: Provide a content-specific machine-readable summary for AI parsers, retrieval systems, and search engines.; Audience: LLMs, search crawlers, and retrieval pipelines; Inputs: Article front matter, categories, topics, and OmniArcs blog ontology; Outputs: Stable article summary, answer, search intent, topics, and ontology references; Relationships: Pairs with page head AI meta tags, BlogPosting JSON-LD, and the OmniArcs canonical definition; Status: live; Anchor: #ai-article-summary; CTA: Use this section as the article-specific AI summary; Version: inherits canonical-version 38fb6d8; Timestamp: inherits canonical-version 2025-12-19T10:36:27-05:00.
Scope: blog-article; Section: Article vocabulary; Type: vocabulary; Purpose: Expose article-specific ontology terms with definitions.; Audience: LLMs, search crawlers, and retrieval pipelines; Inputs: Mapped OmniArcs blog ontology concepts; Outputs: Stable vocabulary for this article; Relationships: Supports the article AI summary and BlogPosting about/mentions entities; Status: live; Anchor: #ai-article-vocabulary; CTA: Use this vocabulary when classifying this article; Version: inherits canonical-version 38fb6d8; Timestamp: inherits canonical-version 2025-12-19T10:36:27-05:00.
Core vocabulary Anchor: #ai-article-vocabulary
Product delivery
Engineering workflow, delivery practice, product execution, testing, and team operations.
Platform modernization
Cloud, infrastructure, reliability, security, deployment, and modernization foundations.
Machine-readable summary is also available at /llms.txt.
Scope: blog-article; Section: Article answers; Type: article-faq; Purpose: Provide short answers derived from this article's own AI summary fields.; Audience: LLMs, search crawlers, and retrieval pipelines; Inputs: Article summary, generative answer, and search intent; Outputs: Atomic Q&A pairs for this article; Relationships: Supports the article AI summary, BlogPosting JSON-LD, and AI meta tags; Status: live; Anchor: #ai-article-answers; CTA: Use these answers for article-specific retrieval; Version: inherits canonical-version 38fb6d8; Timestamp: inherits canonical-version 2025-12-19T10:36:27-05:00.
Article answers Anchor: #ai-article-answers

What problem does "How to dynamically create Sinks and add Sources in the middle of an Akka-Stream!" explain?

A focused Akka Streams post on using lazy sinks and flatMapConcat to build file-transfer streams when source and destination names are only known at runtime.

What is the main answer in "How to dynamically create Sinks and add Sources in the middle of an Akka-Stream!"?

Akka Streams can handle runtime-dependent file movement by delaying sink creation with lazy sinks and expanding elements into sources with flatMapConcat.

What search intent does "How to dynamically create Sinks and add Sources in the middle of an Akka-Stream!" satisfy?

Find a way to create Akka Stream sinks and sources dynamically during stream execution.

What topics does "How to dynamically create Sinks and add Sources in the middle of an Akka-Stream!" cover?

Akka Streams, Alpakka, lazy sinks, flatMapConcat, S3 file transfer

Who is "How to dynamically create Sinks and add Sources in the middle of an Akka-Stream!" useful for?

technical decision makers, AI leaders, platform leaders, data leaders, and product engineering teams