diff --git a/testdata/function/index.js b/testdata/function/index.js new file mode 100644 index 00000000..c6cb24e0 --- /dev/null +++ b/testdata/function/index.js @@ -0,0 +1,8 @@ +exports.handler = async (event) => { + const response = { + statusCode: 200, + body: JSON.stringify('Hello from Lambda!'), + }; + return response; +}; + diff --git a/tf/.gitignore b/tf/.gitignore new file mode 100644 index 00000000..09b1e21a --- /dev/null +++ b/tf/.gitignore @@ -0,0 +1,4 @@ +.terraform/ + +*.tfstate +*.tfstate.* diff --git a/tf/main.tf b/tf/main.tf new file mode 100644 index 00000000..d37b33b1 --- /dev/null +++ b/tf/main.tf @@ -0,0 +1,52 @@ +provider "aws" { + region = var.aws_region +} + +module "ec_deployment" { + source = "github.com/elastic/apm-server//testing/infra/terraform/modules/ec_deployment?depth=1" + deployment_name_prefix = "apm-aws-lambda-smoke-testing" + integrations_server = true + apm_server_expvar = false + apm_server_pprof = false + region = var.ess_region + deployment_template = var.ess_deployment_template +} + +module "lambda_function" { + source = "terraform-aws-modules/lambda/aws" + + function_name = "smoke-testing-test" + description = "Example Lambda function for smoke testing" + handler = "index.handler" + runtime = "nodejs16.x" + + source_path = "../testdata/function/" + + layers = [ + module.lambda_layer_local.lambda_layer_arn, + "arn:aws:lambda:${var.aws_region}:267093732750:layer:elastic-apm-node-ver-3-38-0:1", + ] + + environment_variables = { + NODE_OPTIONS = "-r elastic-apm-node/start" + ELASTIC_APM_LOG_LEVEL = var.log_level + ELASTIC_APM_LAMBDA_APM_SERVER = module.ec_deployment.apm_url + ELASTIC_APM_SECRET_TOKEN = module.ec_deployment.apm_secret_token + } + + tags = { + Name = "my-lambda" + } +} + +module "lambda_layer_local" { + source = "terraform-aws-modules/lambda/aws" + + create_layer = true + + layer_name = "apm-lambda-extension-smoke-testing" + description = "AWS Lambda Extension Layer for Elastic APM - smoke testing" + compatible_runtimes = ["nodejs16.x"] + + source_path = "../bin/" +} diff --git a/tf/output.tf b/tf/output.tf new file mode 100644 index 00000000..ef0576ae --- /dev/null +++ b/tf/output.tf @@ -0,0 +1,16 @@ +output "elasticsearch_url" { + value = module.ec_deployment.elasticsearch_url + description = "The secure Elasticsearch URL" +} + +output "elasticsearch_username" { + value = module.ec_deployment.elasticsearch_username + description = "The Elasticsearch username" + sensitive = true +} + +output "elasticsearch_password" { + value = module.ec_deployment.elasticsearch_password + description = "The Elasticsearch password" + sensitive = true +} diff --git a/tf/providers.tf b/tf/providers.tf new file mode 100644 index 00000000..506fb456 --- /dev/null +++ b/tf/providers.tf @@ -0,0 +1,12 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = ">=4.28.0" + } + ec = { + source = "elastic/ec" + version = ">=0.4.1" + } + } +} diff --git a/tf/test.sh b/tf/test.sh new file mode 100755 index 00000000..3280828e --- /dev/null +++ b/tf/test.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +set -e + +export TF_IN_AUTOMATION=1 +export TF_CLI_ARGS=-no-color + +cleanup() { + terraform destroy -auto-approve >> tf.log +} + +trap "cleanup" EXIT + +echo "-> Creating the underlying infrastructure..." +terraform init | tee tf.log +terraform apply -auto-approve | tee -a tf.log + +echo "-> Calling the lambda function..." +aws lambda invoke --region=eu-central-1 --function-name smoke-testing-test response.json +aws lambda invoke --region=eu-central-1 --function-name smoke-testing-test response.json + +echo "-> Waiting for the agent documents to be indexed in Elasticsearch..." + +ES_HOST=$(terraform output -raw elasticsearch_url) +ES_USER=$(terraform output -raw elasticsearch_username) +ES_PASS=$(terraform output -raw elasticsearch_password) + +hits=0 +attempts=0 +while [ ${hits} -eq 0 ]; do + # Check that ES has transaction documents on the GET endpoint. + resp=$(curl -s -H 'Content-Type: application/json' -u ${ES_USER}:${ES_PASS} "${ES_HOST}/traces-apm-*/_search" -d '{ + "query": { + "match": { + "agent.name": "nodejs" + } + } + }') + hits=$(echo ${resp} | jq '.hits.total.value') + if [[ ${attempts} -ge 30 ]]; then + echo "-> Didn't find any traces for the NodeJS agents after ${attempts} attempts" + exit 1 + fi + let "attempts+=1" + sleep 1 +done + +echo "-> Smoke tests passed!" diff --git a/tf/variables.tf b/tf/variables.tf new file mode 100644 index 00000000..c25268e5 --- /dev/null +++ b/tf/variables.tf @@ -0,0 +1,23 @@ +variable "aws_region" { + type = string + description = "aws region" + default = "eu-central-1" +} + +variable "log_level" { + type = string + description = "lambda extension log level" + default = "trace" +} + +variable "ess_region" { + type = string + description = "ess region" + default = "gcp-us-west2" +} + +variable "ess_deployment_template" { + type = string + description = "Elastic Cloud deployment template" + default = "gcp-compute-optimized" +}