Skip to content

lambda-sqs-terraform: Update runtime to nodejs22.x #2813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lambda-sqs-terraform/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lambda.zip
24 changes: 15 additions & 9 deletions lambda-sqs-terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.22"
version = "~> 5.0"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Older version does not support Node.js 22 runtime. So I updated the version to v5.

}
}

Expand All @@ -22,7 +22,7 @@ resource "aws_lambda_function" "lambda_function" {
source_code_hash = data.archive_file.lambda_zip_file.output_base64sha256
handler = "app.handler"
role = aws_iam_role.lambda_iam_role.arn
runtime = "nodejs16.x"
runtime = "nodejs22.x"
environment {
variables = {
SQSqueueName = aws_sqs_queue.sqs_queue.url
Expand All @@ -41,11 +41,7 @@ data "aws_iam_policy" "lambda_basic_execution_role_policy" {
}

resource "aws_iam_role" "lambda_iam_role" {
name_prefix = "LambdaSQSRole-"
managed_policy_arns = [
data.aws_iam_policy.lambda_basic_execution_role_policy.arn,
aws_iam_policy.lambda_policy.arn
]
name_prefix = "LambdaSQSRole-"

assume_role_policy = <<EOF
{
Expand All @@ -64,11 +60,21 @@ resource "aws_iam_role" "lambda_iam_role" {
EOF
}

resource "aws_iam_role_policy_attachment" "lambda_basic_execution" {
role = aws_iam_role.lambda_iam_role.name
policy_arn = data.aws_iam_policy.lambda_basic_execution_role_policy.arn
}

resource "aws_iam_role_policy_attachment" "lambda_sqs" {
role = aws_iam_role.lambda_iam_role.name
policy_arn = aws_iam_policy.lambda_policy.arn
}

Copy link
Contributor Author

@kakakakakku kakakakakku Aug 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: The managed_policy_arns argument is already deprecated. I update code correctly😀 See document below.

Image

data "aws_iam_policy_document" "lambda_policy_document" {
statement {

effect = "Allow"

actions = [
"sqs:SendMessage*"
]
Expand Down
13 changes: 8 additions & 5 deletions lambda-sqs-terraform/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
* SPDX-License-Identifier: MIT-0
*/

const AWS = require('aws-sdk')
AWS.config.region = process.env.AWS_REGION
const sqs = new AWS.SQS({apiVersion: '2012-11-05'})
const { SQSClient, SendMessageCommand } = require('@aws-sdk/client-sqs')

const sqsClient = new SQSClient({
region: process.env.AWS_REGION
})

// The Lambda handler
exports.handler = async (event) => {
Expand All @@ -15,6 +17,7 @@ exports.handler = async (event) => {
}

// Send to SQS
const result = await sqs.sendMessage(params).promise()
const command = new SendMessageCommand(params)
const result = await sqsClient.send(command)
console.log(result)
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: The following error occurs. Because in Node.js 18 and later runtimes, AWS SDK v3 is bundled, so I rewrote it to v3. See articles below.

{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'aws-sdk'\nRequire stack:\n- /var/task/app.js\n- /var/runtime/index.mjs","trace":["Runtime.ImportModuleError: Error: Cannot find module 'aws-sdk'","Require stack:","- /var/task/app.js","- /var/runtime/index.mjs","    at _loadUserApp (file:///var/runtime/index.mjs:1109:17)","    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1148:21)","    at async start (file:///var/runtime/index.mjs:1332:23)","    at async file:///var/runtime/index.mjs:1339:1"]}%