-
Notifications
You must be signed in to change notification settings - Fork 996
lambda-sns-terraform: Update runtime to nodejs22.x #2815
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
base: main
Are you sure you want to change the base?
Changes from all commits
249af05
12f2dec
205a7a3
d408928
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
lambda.zip |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ terraform { | |
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = "~> 4.22" | ||
version = "~> 5.0" | ||
} | ||
} | ||
|
||
|
@@ -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 = { | ||
SNStopic = aws_sns_topic.sns_topic.arn | ||
|
@@ -41,11 +41,7 @@ data "aws_iam_policy" "lambda_basic_execution_role_policy" { | |
} | ||
|
||
resource "aws_iam_role" "lambda_iam_role" { | ||
name_prefix = "LambdaSNSRole-" | ||
managed_policy_arns = [ | ||
data.aws_iam_policy.lambda_basic_execution_role_policy.arn, | ||
aws_iam_policy.lambda_policy.arn | ||
] | ||
name_prefix = "LambdaSNSRole-" | ||
|
||
assume_role_policy = <<EOF | ||
{ | ||
|
@@ -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 | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: The |
||
|
||
data "aws_iam_policy_document" "lambda_policy_document" { | ||
statement { | ||
|
||
effect = "Allow" | ||
|
||
actions = [ | ||
"sns:Publish" | ||
] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,11 @@ | |
* SPDX-License-Identifier: MIT-0 | ||
*/ | ||
|
||
const AWS = require('aws-sdk') | ||
AWS.config.region = process.env.AWS_REGION | ||
const sns = new AWS.SNS({apiVersion: '2012-11-05'}) | ||
const { SNSClient, PublishCommand } = require('@aws-sdk/client-sns') | ||
|
||
const snsClient = new SNSClient({ | ||
region: process.env.AWS_REGION | ||
}) | ||
|
||
// The Lambda handler | ||
exports.handler = async (event) => { | ||
|
@@ -16,6 +18,6 @@ exports.handler = async (event) => { | |
} | ||
|
||
// Send to SNS | ||
const result = await sns.publish(params).promise() | ||
const result = await snsClient.send(new PublishCommand(params)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: Some error occurs. Because in Node.js 18 and later runtimes, AWS SDK v3 is bundled, so I rewrote it to v3. See articles below. |
||
console.log(result) | ||
} | ||
} |
There was a problem hiding this comment.
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.