Skip to content

Commit f1402b3

Browse files
authored
[SVLS-6600] Add provided.al2023 runtime (#32)
* added provided.al2023 as supported runtime for go lambda * documentation updated for go lambda examples * al2 go runtime added
1 parent f4902f4 commit f1402b3

File tree

10 files changed

+242
-0
lines changed

10 files changed

+242
-0
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,32 @@ module "lambda-datadog" {
116116
}
117117
```
118118

119+
### Go
120+
```
121+
module "lambda-datadog" {
122+
source = "DataDog/lambda-datadog/aws"
123+
version = "3.0.0"
124+
125+
filename = "example.zip"
126+
function_name = "example-function"
127+
role = aws_iam_role.lambda_role.arn
128+
handler = "bootstrap"
129+
runtime = "provided.al2023"
130+
memory_size = 1024
131+
132+
environment_variables = {
133+
"DD_API_KEY_SECRET_ARN" : "arn:aws:secretsmanager:us-east-1:000000000000:secret:example-secret"
134+
"DD_ENV" : "dev"
135+
"DD_SERVICE" : "example-service"
136+
"DD_SITE": "datadoghq.com"
137+
"DD_VERSION" : "1.0.0"
138+
}
139+
140+
datadog_extension_layer_version = 74
141+
}
142+
```
143+
144+
119145
## Configuration
120146

121147
### Lambda Function

examples/go/README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Golang Example
2+
3+
A simple Go Lambda function with out of the box Datadog instrumentation.
4+
5+
## Usage
6+
7+
* Create a [Datadog API Key](https://app.datadoghq.com/organization-settings/api-keys)
8+
* Create a secret in [AWS Secrets Manager](https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html) and add the Datadog API Key as the secret value in plaintext
9+
* Create a `terraform.tfvars` file
10+
- Set the `datadog_secret_arn` to the arn of the secret you just created
11+
- Set the `datadog_service_name` to the name of the service you want to use to filter for the resource in Datadog
12+
- Set the `datadog_site` to the [Datadog destination site](https://docs.datadoghq.com/getting_started/site/) for your metrics, traces, and logs
13+
* Run the following commands
14+
15+
```
16+
go mod tidy
17+
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o bootstrap src/main.go
18+
terraform init
19+
terraform plan
20+
terraform apply
21+
```
22+
23+
<!-- BEGIN_TF_DOCS -->
24+
## Requirements
25+
26+
| Name | Version |
27+
|------|---------|
28+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.5.0 |
29+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.77.0 |
30+
31+
## Providers
32+
33+
| Name | Version |
34+
|------|---------|
35+
| <a name="provider_archive"></a> [archive](#provider\_archive) | 2.4.2 |
36+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.77.0 |
37+
38+
## Modules
39+
40+
| Name | Source | Version |
41+
|------|--------|---------|
42+
| <a name="module_lambda-datadog"></a> [lambda-datadog](#module\_lambda-datadog) | ../../ | n/a |
43+
44+
## Resources
45+
46+
| Name | Type |
47+
|------|------|
48+
| [aws_iam_policy.secrets_manager_read_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
49+
| [aws_iam_role.lambda_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
50+
| [aws_iam_role_policy_attachment.attach_iam_policy_to_iam_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
51+
| [aws_iam_role_policy_attachment.attach_secrets_manager_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
52+
| [archive_file.zip_code](https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file) | data source |
53+
54+
## Inputs
55+
56+
| Name | Description | Type | Default | Required |
57+
|------|-------------|------|---------|:--------:|
58+
| <a name="input_datadog_secret_arn"></a> [datadog\_secret\_arn](#input\_datadog\_secret\_arn) | Secret for Datadog API Key | `string` | n/a | yes |
59+
| <a name="input_datadog_service_name"></a> [datadog\_service\_name](#input\_datadog\_service\_name) | Service used to filter for resources in Datadog | `string` | n/a | yes |
60+
| <a name="input_datadog_site"></a> [datadog\_site](#input\_datadog\_site) | Destination site for your metrics, traces, and logs | `string` | n/a | yes |
61+
62+
## Outputs
63+
64+
| Name | Description |
65+
|------|-------------|
66+
| <a name="output_arn"></a> [arn](#output\_arn) | Amazon Resource Name (ARN) identifying your Lambda Function. |
67+
| <a name="output_function_name"></a> [function\_name](#output\_function\_name) | Unique name for your Lambda Function |
68+
| <a name="output_invoke_arn"></a> [invoke\_arn](#output\_invoke\_arn) | ARN to be used for invoking Lambda Function from API Gateway. |
69+
<!-- END_TF_DOCS -->

examples/go/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/DataDog/terraform-aws-lambda-datadog/examples/go
2+
3+
go 1.24.3
4+
5+
require github.com/aws/aws-lambda-go v1.49.0

examples/go/go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/aws/aws-lambda-go v1.49.0 h1:z4VhTqkFZPM3xpEtTqWqRqsRH4TZBMJqTkRiBPYLqIQ=
2+
github.com/aws/aws-lambda-go v1.49.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A=
3+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
8+
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
9+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

examples/go/main.tf

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
resource "aws_iam_role" "lambda_role" {
2+
name = "terraform-example-go-${var.datadog_service_name}-role"
3+
assume_role_policy = jsonencode(
4+
{
5+
"Version" : "2012-10-17",
6+
"Statement" : [
7+
{
8+
"Sid" : "",
9+
"Effect" : "Allow",
10+
"Action" : "sts:AssumeRole",
11+
"Principal" : {
12+
"Service" : "lambda.amazonaws.com"
13+
}
14+
}
15+
]
16+
})
17+
}
18+
19+
resource "aws_iam_policy" "secrets_manager_read_policy" {
20+
name = "terraform-example-go-${var.datadog_service_name}-secrets-manager-policy"
21+
description = "Policy to allow read access to Secrets Manager"
22+
policy = jsonencode({
23+
Version = "2012-10-17"
24+
Statement = [
25+
{
26+
Sid = "ReadSecret"
27+
Effect = "Allow"
28+
Action = "secretsmanager:GetSecretValue"
29+
Resource = var.datadog_secret_arn
30+
}
31+
]
32+
})
33+
}
34+
35+
resource "aws_iam_role_policy_attachment" "attach_iam_policy_to_iam_role" {
36+
role = aws_iam_role.lambda_role.name
37+
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
38+
}
39+
40+
resource "aws_iam_role_policy_attachment" "attach_secrets_manager_policy" {
41+
role = aws_iam_role.lambda_role.name
42+
policy_arn = aws_iam_policy.secrets_manager_read_policy.arn
43+
}
44+
45+
data "archive_file" "zip_code" {
46+
type = "zip"
47+
source_file = "${path.module}/bootstrap" # Point directly to the bootstrap file
48+
output_path = "${path.module}/hello-go.zip"
49+
}
50+
51+
52+
53+
module "lambda-datadog" {
54+
source = "../../"
55+
56+
filename = "${path.module}/hello-go.zip"
57+
function_name = "terraform-example-go-${var.datadog_service_name}-function"
58+
role = aws_iam_role.lambda_role.arn
59+
handler = "bootstrap"
60+
runtime = "provided.al2023"
61+
architectures = ["arm64"]
62+
memory_size = 256
63+
64+
environment_variables = {
65+
"DD_API_KEY_SECRET_ARN" : var.datadog_secret_arn
66+
"DD_ENV" : "dev"
67+
"DD_SERVICE" : var.datadog_service_name
68+
"DD_SITE": var.datadog_site
69+
"DD_VERSION" : "1.0.0"
70+
}
71+
}
72+

examples/go/outputs.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
output "arn" {
2+
description = "Amazon Resource Name (ARN) identifying your Lambda Function."
3+
value = module.lambda-datadog.arn
4+
}
5+
6+
output "invoke_arn" {
7+
description = "ARN to be used for invoking Lambda Function from API Gateway."
8+
value = module.lambda-datadog.invoke_arn
9+
}
10+
11+
output "function_name" {
12+
description = "Unique name for your Lambda Function"
13+
value = module.lambda-datadog.function_name
14+
}

examples/go/src/main.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"log"
7+
8+
"github.com/aws/aws-lambda-go/lambda"
9+
)
10+
11+
// HandleRequest processes the lambda event
12+
func HandleRequest(ctx context.Context, event json.RawMessage) error {
13+
log.Println("Hello, World!")
14+
15+
return nil
16+
}
17+
18+
func main() {
19+
lambda.Start(HandleRequest)
20+
}

examples/go/variables.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
variable "datadog_secret_arn" {
2+
description = "Secret for Datadog API Key"
3+
type = string
4+
}
5+
6+
variable "datadog_service_name" {
7+
description = "Service used to filter for resources in Datadog"
8+
type = string
9+
}
10+
11+
variable "datadog_site" {
12+
description = "Destination site for your metrics, traces, and logs"
13+
type = string
14+
}

examples/go/versions.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">= 1.5.0"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 5.77.0"
8+
}
9+
}
10+
}

main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ check "runtime_support" {
111111
"python3.11",
112112
"python3.12",
113113
"python3.13",
114+
"provided.al2",
115+
"provided.al2023",
114116
],
115117
var.runtime)
116118
error_message = "${var.runtime} Lambda runtime is not supported by the lambda-datadog Terraform module"

0 commit comments

Comments
 (0)