Skip to content

Commit 1252d9e

Browse files
committed
(infra): Deploy the first lambda with OpenTofu
1 parent 9400039 commit 1252d9e

File tree

15 files changed

+237
-6
lines changed

15 files changed

+237
-6
lines changed

.opentofu-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.9.1

README.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,28 @@ Each lambda implementation has the same functionality:
1212

1313
## Requirements
1414

15-
Node >= v 20
16-
NPM
17-
ESBuild availble globally
15+
- Node >= v20
16+
- NPM
17+
- ESBuild availble globally
18+
- OpenTofu (see `.opentofu-version`)
19+
20+
## Deploying
21+
22+
The prototype is deployed with [OpenTofu](https://opentofu.org/).
23+
Valid AWS credentials must be present in your shell.
24+
25+
First, build the lambda:
26+
27+
```bash
28+
cd lambda/typescript
29+
npm run build
30+
31+
```
32+
33+
Then deploy:
34+
35+
```bash
36+
cd infra
37+
tofu init
38+
tofu apply
39+
```

TODO.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
## Infrastucture
1111

12-
- Decide Terraform vs. Cloudformation
13-
- How to build each lambda?
12+
- Deploy the other lambdas
13+
- Refactor queue / logs / lambda into a module?
1414

1515
## Benchmark
1616

infra/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.terraform
2+
production.auto.tfvars
3+
terraform.tfstate*

infra/.terraform.lock.hcl

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

infra/cloudwatch.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
resource "aws_cloudwatch_log_group" "lambda_benchmark_typescript_logs" {
2+
name = "/aws/lambda/lambda_benchmark_typescript_lambda"
3+
retention_in_days = 3
4+
tags = var.default_tags
5+
}

infra/dynamodb.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
resource "aws_dynamodb_table" "table" {
2+
name = var.dynamodb_table_name
3+
billing_mode = "PAY_PER_REQUEST"
4+
hash_key = "eventId"
5+
6+
attribute {
7+
name = "eventId"
8+
type = "S"
9+
}
10+
11+
}

infra/example.tfvars

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
owner = ""

infra/iam.tf

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
data "aws_iam_policy_document" "assume_role" {
2+
statement {
3+
effect = "Allow"
4+
5+
principals {
6+
type = "Service"
7+
identifiers = ["lambda.amazonaws.com"]
8+
}
9+
10+
actions = ["sts:AssumeRole"]
11+
}
12+
}
13+
14+
resource "aws_iam_role" "lambda_benchmark_lambda_role" {
15+
name = "lambda_benchmark_lambda_role"
16+
assume_role_policy = data.aws_iam_policy_document.assume_role.json
17+
tags = var.default_tags
18+
}
19+
20+
data "aws_iam_policy_document" "lambda_logging" {
21+
statement {
22+
effect = "Allow"
23+
24+
actions = [
25+
"logs:CreateLogGroup",
26+
"logs:CreateLogStream",
27+
"logs:PutLogEvents",
28+
]
29+
30+
resources = ["arn:aws:logs:*:*:*"]
31+
}
32+
}
33+
34+
resource "aws_iam_policy" "lambda_benchmark_allow_logging" {
35+
name = "lambda_benchmark_allow_logging"
36+
path = "/"
37+
description = "Allow lambdas in the lambda_benchmark app to log to Cloudwatch"
38+
policy = data.aws_iam_policy_document.lambda_logging.json
39+
tags = var.default_tags
40+
}
41+
42+
resource "aws_iam_role_policy_attachment" "lambda_benchmark_allow_logging" {
43+
role = aws_iam_role.lambda_benchmark_lambda_role.name
44+
policy_arn = aws_iam_policy.lambda_benchmark_allow_logging.arn
45+
}
46+
47+
data "aws_iam_policy_document" "dynamodb_access" {
48+
statement {
49+
effect = "Allow"
50+
51+
actions = [
52+
"dynamodb:PutItem",
53+
]
54+
55+
resources = [aws_dynamodb_table.table.arn]
56+
}
57+
}
58+
59+
60+
resource "aws_iam_policy" "lambda_benchmark_dynamodb_table_access" {
61+
name = "lambda_benchmark_dynamodb_table_access"
62+
path = "/"
63+
description = "IAM policy to allow lambdas in the lambda_benchmark app to write to DynamoDB"
64+
policy = data.aws_iam_policy_document.dynamodb_access.json
65+
tags = var.default_tags
66+
}
67+
68+
resource "aws_iam_role_policy_attachment" "dynamodb_table_access" {
69+
role = aws_iam_role.lambda_benchmark_lambda_role.name
70+
policy_arn = aws_iam_policy.lambda_benchmark_dynamodb_table_access.arn
71+
}
72+
73+
data "aws_iam_policy_document" "sqs_access" {
74+
statement {
75+
sid = "AllowSQSPermissions"
76+
effect = "Allow"
77+
resources = [aws_sqs_queue.typescript_lambda_queue.arn]
78+
79+
actions = [
80+
"sqs:ChangeMessageVisibility",
81+
"sqs:DeleteMessage",
82+
"sqs:GetQueueAttributes",
83+
"sqs:ReceiveMessage",
84+
]
85+
}
86+
87+
}
88+
89+
resource "aws_iam_policy" "lambda_benchmark_sqs_access" {
90+
name = "lambda_benchmark_sqs_access"
91+
path = "/"
92+
description = "IAM policy to allow lambdas in the lambda_benchmark app to read from SQS"
93+
policy = data.aws_iam_policy_document.sqs_access.json
94+
tags = var.default_tags
95+
}
96+
97+
resource "aws_iam_role_policy_attachment" "sqs_access" {
98+
role = aws_iam_role.lambda_benchmark_lambda_role.name
99+
policy_arn = aws_iam_policy.lambda_benchmark_sqs_access.arn
100+
}

infra/lambda.tf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
resource "aws_lambda_function" "lambda_benchmark_typescript_lambda" {
2+
function_name = "lambda_benchmark_typescript_lambda"
3+
filename = "../lambda/typescript/dist/index.zip"
4+
role = aws_iam_role.lambda_benchmark_lambda_role.arn
5+
handler = "index.handler"
6+
source_code_hash = filebase64sha256("../lambda/typescript/dist/index.zip")
7+
runtime = "nodejs20.x"
8+
architectures = ["arm64"]
9+
timeout = 5
10+
tags = var.default_tags
11+
12+
environment {
13+
variables = {
14+
TABLE_NAME = var.dynamodb_table_name,
15+
}
16+
}
17+
18+
depends_on = [
19+
aws_iam_role_policy_attachment.lambda_benchmark_allow_logging,
20+
aws_cloudwatch_log_group.lambda_benchmark_typescript_logs,
21+
]
22+
}
23+
24+
resource "aws_lambda_event_source_mapping" "lambda_benchmark_typescript_lambda" {
25+
event_source_arn = aws_sqs_queue.typescript_lambda_queue.arn
26+
function_name = aws_lambda_function.lambda_benchmark_typescript_lambda.arn
27+
}

0 commit comments

Comments
 (0)