feat(lambda): mount host AWS config for real credential discovery (#703)

When FLOCI_SERVICES_LAMBDA_AWS_CONFIG_PATH is set, bind-mount the host
path read-only into Lambda containers at /opt/aws-config and set
AWS_SHARED_CREDENTIALS_FILE and AWS_CONFIG_FILE env vars so the SDK
discovers credentials regardless of container HOME directory.

When unset, credentials are read from Floci's own environment with a
fallback to test/test/test. Blank values are treated as absent.

Closes #686
This commit is contained in:
Taewoo Ryu authored and GitHub committed 2026-05-05 18:22:12 -05:00
1 parent d620c18534
commit aee46fe2da
7 files changed
+209 -14

No files matched your search

@@ -659,6 +659,19 @@ public interface EmulatorConfig {
@WithDefault("100")
int unreservedConcurrencyMin();
/**
* Host path to bind-mount (read-only) into Lambda containers at /opt/aws-config.
* When set, no AWS credential env vars are injected; instead
* AWS_SHARED_CREDENTIALS_FILE and AWS_CONFIG_FILE are set to point at
* the mounted files, ensuring SDK discovery works regardless of container HOME.
* When absent, Floci injects credentials from its own environment
* (AWS_ACCESS_KEY_ID, etc.) or falls back to test/test/test.
* Blank values are treated as absent.
*
* Env var: FLOCI_SERVICES_LAMBDA_AWS_CONFIG_PATH
*/
Optional<String> awsConfigPath();
HotReload hotReload();
interface HotReload {
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.core.common.docker;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;
import com.github.dockerjava.api.model.AccessMode;
import com.github.dockerjava.api.model.Bind;
import com.github.dockerjava.api.model.LogConfig;
import com.github.dockerjava.api.model.Mount;
@@ -216,6 +217,11 @@ public class ContainerBuilder {
return this;
}
public Builder withReadOnlyBind(String hostPath, String containerPath) {
this.binds.add(new Bind(hostPath, new Volume(containerPath), AccessMode.ro));
return this;
}
/**
* Adds a named volume mount.
*/
@@ -29,6 +29,7 @@ import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
/**
* Starts and stops Docker containers for Lambda function execution.
@@ -169,9 +170,22 @@ public class ContainerLauncher {
}
env.add("AWS_DEFAULT_REGION=" + lambdaRegion);
env.add("AWS_REGION=" + lambdaRegion);
env.add("AWS_ACCESS_KEY_ID=test");
env.add("AWS_SECRET_ACCESS_KEY=test");
env.add("AWS_SESSION_TOKEN=test");
Optional<String> awsConfigPath = config.services().lambda().awsConfigPath()
.filter(s -> !s.isBlank());
if (awsConfigPath.isPresent()) {
// ~/.aws will be mounted — don't inject credentials, let SDK discover them.
// Set explicit file paths so discovery works regardless of container HOME.
env.add("AWS_SHARED_CREDENTIALS_FILE=/opt/aws-config/credentials");
env.add("AWS_CONFIG_FILE=/opt/aws-config/config");
} else {
// Use Floci's own env vars, fallback to test/test/test
String ak = System.getenv("AWS_ACCESS_KEY_ID");
String sk = System.getenv("AWS_SECRET_ACCESS_KEY");
String st = System.getenv("AWS_SESSION_TOKEN");
env.add("AWS_ACCESS_KEY_ID=" + (ak != null ? ak : "test"));
env.add("AWS_SECRET_ACCESS_KEY=" + (sk != null ? sk : "test"));
env.add("AWS_SESSION_TOKEN=" + (st != null ? st : "test"));
}
env.add("FLOCI_HOSTNAME=" + flociHostname);
env.add("FLOCI_ENDPOINT=" + flociEndpoint);
env.add("AWS_ENDPOINT_URL=" + flociEndpoint);
@@ -208,6 +222,15 @@ public class ContainerLauncher {
specBuilder.withCmd(fn.getHandler());
}
// Mount host AWS config into Lambda container (read-only) for SDK credential discovery
awsConfigPath.ifPresent(hostPath -> {
if (!Files.isDirectory(Path.of(hostPath))) {
LOG.warnv("awsConfigPath '{0}' does not exist or is not a directory; "
+ "Lambda containers may fail to discover credentials", hostPath);
}
specBuilder.withReadOnlyBind(hostPath, "/opt/aws-config");
});
ContainerSpec spec = specBuilder.build();
// Create container without starting — provided.* runtimes exec