Skip to content

Commit 171ba41

Browse files
committed
Add PYTHONPATH to subprocess
1 parent 2a09bc1 commit 171ba41

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

lambda/main.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
12
import subprocess
23
import uuid
34
import os
4-
55
import logging
6+
import sys
7+
import json
68

79
logger = logging.getLogger()
810
logger.setLevel(logging.DEBUG)
911

1012

1113
def lambda_handler(event, context):
12-
logger.debug("Input event: {}".format(event))
14+
logger.debug("Input event: {}".format(json.dumps(event)))
1315

1416
cmd = event['interpreter']
1517
if event['command'] is not None:
@@ -25,7 +27,10 @@ def lambda_handler(event, context):
2527

2628
# For the subprocess environment, use all of the existing env vars, plus
2729
# any new ones. New ones with the same name will overwrite.
28-
new_env = os.environ.copy() | event['environment']
30+
new_env = os.environ.copy()
31+
# Set the python path to include everything that is given by default to Python functions
32+
new_env['PYTHONPATH'] = ':'.join(sys.path)
33+
new_env.update(event['environment'])
2934

3035
# Start the process
3136
p = subprocess.Popen(
@@ -84,6 +89,9 @@ def lambda_handler(event, context):
8489
{}
8590
'''.format(exit_code, stdout, stderr))
8691

92+
logger.debug("Exit code: {}".format(exit_code))
93+
logger.debug("Stdout {}".format(stdout))
94+
logger.debug("Stderr {}".format(stderr))
8795
return {
8896
'exit_code': exit_code,
8997
'stdout': stdout,

main.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ module "shell_lambda" {
2121
archive_output_directory = "${path.module}/archives/"
2222
lambda_config = {
2323
function_name = "invicton-labs-aws-lambda-shell-${random_id.lambda.hex}"
24+
description = var.lambda_description
2425
handler = "main.lambda_handler"
25-
runtime = "python3.9"
26+
runtime = var.lambda_runtime
2627
timeout = var.lambda_timeout
2728
memory_size = var.lambda_memory_size
2829
role = local.lambda_role
2930
layers = var.lambda_layer_arns
31+
architectures = [var.lambda_architecture]
3032
tags = {
3133
"ModuleAuthor" = "InvictonLabs"
3234
"ModuleUrl" = "https://registry.terraform.io/modules/Invicton-Labs/lambda-shell/aws"

outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@ output "invicton_labs_lambda_shell_arn" {
33
// Use the "complete" output of the inner Lambda module so that the function ARN can't be used until everything has been completed (permissions have been applied)
44
value = module.shell_lambda.complete ? module.shell_lambda.lambda.arn : module.shell_lambda.lambda.arn
55
}
6+
7+
output "runtime" {
8+
description = "The Lambda runtime that is used for the Lambda shell."
9+
value = var.lambda_runtime
10+
}

variables.tf

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
variable "lambda_description" {
2+
description = "The description string to apply to the Lambda function."
3+
type = string
4+
default = "Invicton-Labs/lambda-shell/aws (https://registry.terraform.io/modules/Invicton-Labs/lambda-shell/aws)"
5+
}
6+
17
variable "lambda_timeout" {
28
description = "The timeout (in seconds) for the Lambda function that is running the shell command."
39
type = number
@@ -16,6 +22,16 @@ variable "lambda_role_arn" {
1622
default = null
1723
}
1824

25+
variable "lambda_architecture" {
26+
description = "The architecture to use for the Lambda function."
27+
type = string
28+
default = "x86_64"
29+
validation {
30+
condition = contains(["x86_64", "arm64"], var.lambda_architecture)
31+
error_message = "The `lambda_architecture` variable must be `x86_64` or `arm64`."
32+
}
33+
}
34+
1935
variable "lambda_role_policies_json" {
2036
description = "A list of JSON-encoded policies to apply to a new role that will be created for the Lambda that runs shell commands. Conflicts with `lambda_role_arn`. If neither is provided, the module will attempt to use the role that the Terraform caller has assumed (if a role has been assumed)."
2137
type = list(string)
@@ -62,8 +78,18 @@ variable "lambda_vpc_config" {
6278

6379
variable "lambda_layer_arns" {
6480
description = "A list of Lambda Layer ARNs to attach to the Lambda."
65-
type = list(string)
66-
default = []
81+
type = list(string)
82+
default = []
83+
}
84+
85+
variable "lambda_runtime" {
86+
description = "The runtime to use for the lambda shell."
87+
type = string
88+
default = "python3.9"
89+
validation {
90+
condition = contains(["python3.7", "python3.8", "python3.9"], var.lambda_runtime)
91+
error_message = "The `lambda_runtime` variable must be `python3.7`, `python3.8`, or `python3.9`."
92+
}
6793
}
6894

6995
data "aws_caller_identity" "current" {}

0 commit comments

Comments
 (0)