Skip to content

Commit d508bca

Browse files
authored
Create python sample app downstream (#578)
1 parent d44a724 commit d508bca

File tree

9 files changed

+125
-22
lines changed

9 files changed

+125
-22
lines changed

adot/utils/expected-templates/python-aws-sdk-wrapper.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"inferred":true,
2929
"http":{
3030
"request":{
31-
"url":"http://httpbin\\.org/",
31+
"url":"https://aws.amazon.com/",
3232
"method":"GET"
3333
}
3434
}

python/build.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ popd || exit
1111
cd ../opentelemetry-lambda/python/src || exit
1212
./build.sh
1313

14-
pushd ../sample-apps || exit
14+
# Build sample app
15+
16+
pushd ../../../python/sample-apps || exit
1517
./build.sh
1618
popd || exit
1719

python/integration-tests/aws-sdk/wrapper/main.tf

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,59 @@ resource "aws_lambda_layer_version" "collector_layer" {
1919
source_code_hash = filebase64sha256("${path.module}/../../../../opentelemetry-lambda/collector/build/collector-extension-${local.architecture}.zip")
2020
}
2121

22-
module "hello-lambda-function" {
23-
source = "../../../../opentelemetry-lambda/python/sample-apps/aws-sdk/deploy/wrapper"
22+
module "test-function" {
23+
source = "terraform-aws-modules/lambda/aws"
24+
25+
architectures = compact([var.architecture])
26+
function_name = var.function_name
27+
handler = "lambda_function.lambda_handler"
28+
runtime = var.runtime
29+
30+
create_package = false
31+
local_existing_package = "${path.module}/../../../sample-apps/build/function.zip"
32+
33+
timeout = 20
34+
35+
layers = compact([
36+
var.enable_collector_layer ? aws_lambda_layer_version.collector_layer[0].arn : null,
37+
aws_lambda_layer_version.sdk_layer.arn
38+
])
39+
40+
environment_variables = {
41+
AWS_LAMBDA_EXEC_WRAPPER = "/opt/otel-instrument"
42+
}
43+
44+
tracing_mode = var.tracing_mode
45+
46+
attach_policy_statements = true
47+
policy_statements = {
48+
s3 = {
49+
effect = "Allow"
50+
actions = [
51+
"s3:ListAllMyBuckets"
52+
]
53+
resources = [
54+
"*"
55+
]
56+
}
57+
}
58+
}
59+
60+
module "api-gateway" {
61+
source = "../../../../opentelemetry-lambda/utils/terraform/api-gateway-proxy"
62+
2463
name = var.function_name
25-
architecture = var.architecture
26-
collector_layer_arn = var.enable_collector_layer ? aws_lambda_layer_version.collector_layer[0].arn : null
27-
sdk_layer_arn = aws_lambda_layer_version.sdk_layer.arn
28-
tracing_mode = var.tracing_mode
29-
runtime = var.runtime
64+
function_name = module.test-function.lambda_function_name
65+
function_invoke_arn = module.test-function.lambda_function_invoke_arn
66+
enable_xray_tracing = true
3067
}
3168

3269
resource "aws_iam_role_policy_attachment" "hello-lambda-cloudwatch-insights" {
33-
role = module.hello-lambda-function.function_role_name
70+
role = module.test-function.lambda_function_name
3471
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
3572
}
3673

3774
resource "aws_iam_role_policy_attachment" "test_xray" {
38-
role = module.hello-lambda-function.function_role_name
75+
role = module.test-function.lambda_function_name
3976
policy_arn = "arn:aws:iam::aws:policy/AWSXRayDaemonWriteAccess"
4077
}

python/integration-tests/aws-sdk/wrapper/outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
output "api-gateway-url" {
2-
value = module.hello-lambda-function.api-gateway-url
2+
value = module.api-gateway.api_gateway_url
33
}
44

55
output "function_role_name" {
6-
value = module.hello-lambda-function.function_role_name
6+
value = module.test-function.lambda_role_name
77
}
88

99
output "collector_layer_arn" {

python/sample-apps/aws-sdk/deploy/wrapper/main.tf

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,54 @@ locals {
77
}
88
}
99

10-
module "app" {
11-
source = "../../../../../opentelemetry-lambda/python/sample-apps/aws-sdk/deploy/wrapper"
10+
module "test-function" {
11+
source = "terraform-aws-modules/lambda/aws"
12+
13+
architectures = compact([var.architecture])
14+
function_name = var.function_name
15+
handler = "lambda_function.lambda_handler"
16+
runtime = var.runtime
17+
18+
create_package = false
19+
local_existing_package = "${path.module}/../../../build/function.zip"
20+
21+
timeout = 20
22+
23+
layers = compact([
24+
null,
25+
local.architecture_to_arns_mapping[var.architecture][data.aws_region.current.name]
26+
])
27+
28+
environment_variables = {
29+
AWS_LAMBDA_EXEC_WRAPPER = "/opt/otel-instrument"
30+
}
31+
32+
tracing_mode = "Active"
33+
34+
attach_policy_statements = true
35+
policy_statements = {
36+
s3 = {
37+
effect = "Allow"
38+
actions = [
39+
"s3:ListAllMyBuckets"
40+
]
41+
resources = [
42+
"*"
43+
]
44+
}
45+
}
46+
}
47+
48+
module "api-gateway" {
49+
source = "../../../../../opentelemetry-lambda/utils/terraform/api-gateway-proxy"
1250

1351
name = var.function_name
14-
collector_layer_arn = null
15-
sdk_layer_arn = local.architecture_to_arns_mapping[var.architecture][data.aws_region.current.name]
16-
tracing_mode = "Active"
17-
architecture = var.architecture
18-
runtime =var.runtime
52+
function_name = module.test-function.lambda_function_name
53+
function_invoke_arn = module.test-function.lambda_function_invoke_arn
54+
enable_xray_tracing = true
1955
}
2056

2157
resource "aws_iam_role_policy_attachment" "test_xray" {
22-
role = module.app.function_role_name
58+
role = module.test-function.lambda_function_name
2359
policy_arn = "arn:aws:iam::aws:policy/AWSXRayDaemonWriteAccess"
2460
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
output "api-gateway-url" {
2-
value = module.app.api-gateway-url
2+
value = module.api-gateway.api_gateway_url
3+
}
4+
5+
output "function_role_name" {
6+
value = module.test-function.lambda_role_name
37
}

python/sample-apps/build.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
set -e
3+
4+
mkdir -p build/python
5+
python3 -m pip install -r function/requirements.txt -t build/python
6+
cp function/lambda_function.py build/python
7+
cd build/python
8+
zip -r ../function.zip ./*
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import os
2+
import json
3+
import requests
4+
import boto3
5+
6+
# lambda function
7+
def lambda_handler(event, context):
8+
9+
requests.get("https://aws.amazon.com/")
10+
11+
client = boto3.client("s3")
12+
client.list_buckets()
13+
14+
return {"body": os.environ.get("_X_AMZN_TRACE_ID")}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
requests
2+
boto3

0 commit comments

Comments
 (0)