-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
95 lines (77 loc) · 2.67 KB
/
Copy pathlambda_function.py
File metadata and controls
95 lines (77 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import json
import subprocess
import boto3
# Path to the Terraform executable
TERRA_PATH = "./terraform"
def run_command(command):
"""
Executes a given shell command and returns the output and error.
Args:
command (list): The command to be executed as a list of strings.
Returns:
tuple: A tuple containing an error message (if any) and the command's output.
"""
print("Incoming Command: ", command)
try:
# Run the command and capture the output and error
result = subprocess.run(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
shell=True
)
# Extract the output and error
output = result.stdout
error = result.stderr
print("Terraform Output: ", output)
print("Terraform error: ", error)
# Check if the command was executed successfully
if result.returncode != 0:
return f"Error: {error}", None
else:
return None, output
except Exception as e:
# Handle any other exceptions
return f"Exception: {str(e)}", None
def deploy_terraform(s3_bucket, path):
"""
Downloads a Terraform file from S3 and applies the Terraform plan.
Args:
s3_bucket (str): The name of the S3 bucket.
path (str): The path to the Terraform file in the S3 bucket.
Returns:
str: The result of the Terraform apply command.
"""
s3 = boto3.resource('s3')
main_file = s3.Object(s3_bucket, path)
main_file.download_file('/tmp/main.tf')
# Initialize and apply the Terraform plan
res, _ = run_command(f'{TERRA_PATH} -chdir=/tmp init')
res, _ = run_command(f'{TERRA_PATH} -chdir=/tmp apply -auto-approve')
return res
def lambda_handler(event, context):
"""
AWS Lambda handler function for applying a Terraform plan.
Args:
event (dict): The event triggering the Lambda function, expected to contain
'bucket' and 'key' keys with S3 bucket and path information.
context (LambdaContext): Provides information about the invocation, function, and runtime environment.
Returns:
dict: A response object with status code and body.
"""
s3_bucket = event.get("bucket")
path = event.get("key")
print(f"S3 Bucket: {s3_bucket} and Path: {path}")
# Validate event data
if not s3_bucket or not path:
return {
'statusCode': 404,
'body': json.dumps('Invalid event data')
}
# Apply the Terraform plan
deploy_terraform(s3_bucket=s3_bucket, path=path)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}