Skip to content

lambda-ssm-parameter-terraform: Update runtime to nodejs22.x #2814

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-ssm-parameter-terraform/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lambda.zip
24 changes: 15 additions & 9 deletions lambda-ssm-parameter-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 Down Expand Up @@ -31,7 +31,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 = {
SSMParameterName = var.ssm_parameter_name
Expand All @@ -50,11 +50,7 @@ data "aws_iam_policy" "lambda_basic_execution_role_policy" {
}

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

assume_role_policy = <<EOF
{
Expand All @@ -73,11 +69,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

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.


data "aws_iam_policy_document" "lambda_policy_document" {
statement {

effect = "Allow"

actions = [
"ssm:GetParameter",
"ssm:PutParameter"
Expand Down
13 changes: 8 additions & 5 deletions lambda-ssm-parameter-terraform/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
// 2. GET or PUT an SSM Parameter Store parameter.
// 3. Return a response with parameter result.

const AWS = require("aws-sdk")
const ssm = new AWS.SSM()
const { SSMClient, GetParameterCommand, PutParameterCommand } = require('@aws-sdk/client-ssm')

const ssmClient = new SSMClient({
region: process.env.AWS_REGION
})

exports.handler = async (event, context) => {
try {
Expand All @@ -34,12 +37,12 @@ exports.handler = async (event, context) => {
Overwrite: true,
Type: "String",
};
result = await ssm.putParameter(ssmPutParams).promise()
result = await ssmClient.send(new PutParameterCommand(ssmPutParams))
} else if (method == "GET") {
const ssmGetParams = {
Name: parameterName,
};
result = await ssm.getParameter(ssmGetParams).promise()
result = await ssmClient.send(new GetParameterCommand(ssmGetParams))
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: 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.

} else {
result = "Method not supported"
}
Expand All @@ -57,4 +60,4 @@ exports.handler = async (event, context) => {
console.error(error);
throw new Error(error);
}
}
}