@@ -24,6 +24,30 @@ module "ec_deployment" {
24
24
tags = module. tags . tags
25
25
}
26
26
27
+ locals {
28
+ runtimeVars = {
29
+ " nodejs" = {
30
+ " source_file" = " ./function/index.js"
31
+ " handler" = " index.handler"
32
+ " runtime" = " nodejs18.x"
33
+ " agent_layer" = " arn:aws:lambda:${ var . aws_region } :267093732750:layer:elastic-apm-node-ver-4-3-0:1"
34
+ " envvars" = {
35
+ " NODE_OPTIONS" = " -r elastic-apm-node/start"
36
+ }
37
+ }
38
+ " python" = {
39
+ " source_file" = " ./function/main.py"
40
+ " handler" = " main.handler"
41
+ " runtime" = " python3.9"
42
+ " agent_layer" = " arn:aws:lambda:${ var . aws_region } :267093732750:layer:elastic-apm-python-ver-6-22-3:1"
43
+ " envvars" = {
44
+ " AWS_LAMBDA_EXEC_WRAPPER" = " /opt/python/bin/elasticapm-lambda"
45
+ }
46
+ }
47
+ }
48
+ }
49
+
50
+
27
51
data "aws_iam_policy_document" "assume_role" {
28
52
statement {
29
53
effect = " Allow"
@@ -44,35 +68,71 @@ resource "aws_iam_role" "lambda" {
44
68
45
69
data "archive_file" "lambda" {
46
70
type = " zip"
47
- source_file = " ../testdata/function/index.js "
71
+ source_file = local . runtimeVars [ var . function_runtime ][ " source_file " ]
48
72
output_path = " lambda_function_payload.zip"
49
73
}
50
74
51
75
resource "aws_lambda_function" "test_lambda" {
52
76
filename = " lambda_function_payload.zip"
53
77
function_name = " ${ var . user_name } -smoke-testing-test"
54
78
role = aws_iam_role. lambda . arn
55
- handler = " index. handler"
79
+ handler = local . runtimeVars [ var . function_runtime ][ " handler" ]
56
80
57
- source_code_hash = data . archive_file . lambda . output_base64sha256
81
+ runtime = local . runtimeVars [ var . function_runtime ][ " runtime " ]
58
82
59
- runtime = " nodejs16.x "
83
+ source_code_hash = data . archive_file . lambda . output_base64sha256
60
84
61
85
layers = [
62
86
aws_lambda_layer_version . lambda_layer . arn ,
63
- " arn:aws:lambda: ${ var . aws_region } :267093732750:layer:elastic-apm-node-ver-4-3-0:1 " ,
87
+ local . runtimeVars [ var . function_runtime ][ " agent_layer " ]
64
88
]
65
89
66
90
environment {
67
- variables = {
68
- NODE_OPTIONS = " -r elastic-apm-node/start"
91
+ variables = merge ({
69
92
ELASTIC_APM_LOG_LEVEL = var.log_level
70
93
ELASTIC_APM_LAMBDA_APM_SERVER = module.ec_deployment.apm_url
71
94
ELASTIC_APM_SECRETS_MANAGER_SECRET_TOKEN_ID = aws_secretsmanager_secret.apm_secret_token.id
72
- }
95
+ }, local. runtimeVars [var . function_runtime ][" envvars" ])
96
+ }
97
+
98
+ depends_on = [
99
+ aws_iam_role_policy_attachment . lambda_logs ,
100
+ aws_iam_role_policy_attachment . secrets_manager_elastic_apm_policy_attach ,
101
+ aws_cloudwatch_log_group . example ,
102
+ ]
103
+ }
104
+
105
+ resource "aws_cloudwatch_log_group" "example" {
106
+ name = " /aws/lambda/${ var . user_name } -smoke-testing-test"
107
+ retention_in_days = 1
108
+ }
109
+
110
+ data "aws_iam_policy_document" "lambda_logging" {
111
+ statement {
112
+ effect = " Allow"
113
+
114
+ actions = [
115
+ " logs:CreateLogGroup" ,
116
+ " logs:CreateLogStream" ,
117
+ " logs:PutLogEvents" ,
118
+ ]
119
+
120
+ resources = [" arn:aws:logs:*:*:*" ]
73
121
}
74
122
}
75
123
124
+ resource "aws_iam_policy" "lambda_logging" {
125
+ name = " smoketest_extension_lambda_logging"
126
+ path = " /"
127
+ description = " IAM policy for logging during smoketest for apm aws lambda extension"
128
+ policy = data. aws_iam_policy_document . lambda_logging . json
129
+ }
130
+
131
+ resource "aws_iam_role_policy_attachment" "lambda_logs" {
132
+ role = aws_iam_role. lambda . name
133
+ policy_arn = aws_iam_policy. lambda_logging . arn
134
+ }
135
+
76
136
resource "aws_secretsmanager_secret" "apm_secret_token" {
77
137
name_prefix = " apm-aws-lambda-smoke-testing-secret"
78
138
recovery_window_in_days = 0
@@ -103,13 +163,12 @@ resource "aws_iam_role_policy_attachment" "secrets_manager_elastic_apm_policy_at
103
163
}
104
164
105
165
locals {
106
- zip_files = tolist (fileset (" ../dist/" , " *-linux-amd64.zip" ))
166
+ zip_files = tolist (fileset (" ../../ dist/" , " *-linux-amd64.zip" ))
107
167
}
108
168
109
169
resource "aws_lambda_layer_version" "lambda_layer" {
110
- filename = " ../dist/${ local . zip_files [0 ]} "
170
+ filename = " ../../ dist/${ local . zip_files [0 ]} "
111
171
layer_name = " apm-aws-lambda-smoke-testing-lambda_layer_name"
112
172
113
- description = " AWS Lambda Extension Layer for Elastic APM - smoke testing"
114
- compatible_runtimes = [" nodejs16.x" ]
173
+ description = " AWS Lambda Extension Layer for Elastic APM - smoke testing"
115
174
}
0 commit comments