Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(cloudformation): forward Lambda environment variables during stack provisioning (#510)

Christine Nguyen8ac4cdcd94ca
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java
index 324cc38ba3..be468bfc0a 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java
@@ -356,6 +356,16 @@ public class CloudFormationResourceProvisioner {
req.put("Description", description);
}
+ if (props != null && props.has("Environment")) {
+ JsonNode envNode = engine.resolveNode(props.get("Environment"));
+ if (envNode != null && envNode.has("Variables")) {
+ JsonNode varsNode = envNode.get("Variables");
+ Map<String, String> vars = new HashMap<>();
+ varsNode.fields().forEachRemaining(e -> vars.put(e.getKey(), e.getValue().asText()));
+ req.put("Environment", Map.of("Variables", vars));
+ }
+ }
+
io.github.hectorvent.floci.services.lambda.model.LambdaFunction func;
try {
func = lambdaService.createFunction(region, req);
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java
index a6da49eb42..3017b72d1e 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java
@@ -244,6 +244,61 @@ class CloudFormationIntegrationTest {
.body("Configuration.FunctionName", equalTo("cfn-nocode-func"));
}
+ @Test
+ void createStack_lambdaWithEnvironmentVariables() {
+ String template = """
+ {
+ "Resources": {
+ "MyFunction": {
+ "Type": "AWS::Lambda::Function",
+ "Properties": {
+ "FunctionName": "cfn-env-func",
+ "Runtime": "nodejs20.x",
+ "Handler": "index.handler",
+ "Role": "arn:aws:iam::000000000000:role/cfn-test-lambda-role",
+ "Environment": {
+ "Variables": {
+ "MY_VAR": "hello",
+ "STAGE": "local"
+ }
+ }
+ }
+ }
+ }
+ }
+ """;
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "CreateStack")
+ .formParam("StackName", "cfn-env-stack")
+ .formParam("TemplateBody", template)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("<StackId>"));
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", "cfn-env-stack")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"));
+
+ given()
+ .when()
+ .get("/2015-03-31/functions/cfn-env-func")
+ .then()
+ .statusCode(200)
+ .body("Configuration.FunctionName", equalTo("cfn-env-func"))
+ .body("Configuration.Environment.Variables.MY_VAR", equalTo("hello"))
+ .body("Configuration.Environment.Variables.STAGE", equalTo("local"));
+ }
+
@Test
void createStack_lambdaWithImageUri() {
String template = """