Skip to content
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-eventbridge-terraform/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lambda.zip
20 changes: 13 additions & 7 deletions lambda-eventbridge-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 @@ -29,7 +29,7 @@ resource "aws_lambda_function" "publisher_function" {
source_code_hash = data.archive_file.lambda_zip_file.output_base64sha256
handler = "app.handler"
role = aws_iam_role.iam_for_lambda.arn
runtime = "nodejs16.x"
runtime = "nodejs22.x"
}

data "archive_file" "lambda_zip_file" {
Expand All @@ -43,11 +43,7 @@ data "aws_iam_policy" "lambda_basic_execution_role_policy" {
}

resource "aws_iam_role" "iam_for_lambda" {
name_prefix = "PublisherFunctionRole-"
managed_policy_arns = [
data.aws_iam_policy.lambda_basic_execution_role_policy.arn,
aws_iam_policy.event_bridge_put_events_policy.arn
]
name_prefix = "PublisherFunctionRole-"

assume_role_policy = <<EOF
{
Expand All @@ -66,6 +62,16 @@ resource "aws_iam_role" "iam_for_lambda" {
EOF
}

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

resource "aws_iam_role_policy_attachment" "lambda_eventbridge" {
role = aws_iam_role.iam_for_lambda.name
policy_arn = aws_iam_policy.event_bridge_put_events_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" "event_bridge_put_events_policy_document" {
statement {

Expand Down
10 changes: 5 additions & 5 deletions lambda-eventbridge-terraform/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

'use strict'

const AWS = require('aws-sdk')
AWS.config.update({ region: process.env.AWS_REGION })
const eventbridge = new AWS.EventBridge()
const { EventBridgeClient, PutEventsCommand } = require('@aws-sdk/client-eventbridge')
const eventbridge = new EventBridgeClient({ region: process.env.AWS_REGION })

exports.handler = async (event) => {
const params = {
Expand All @@ -23,7 +22,8 @@ exports.handler = async (event) => {
}
]
}

// Publish to EventBridge
const result = await eventbridge.putEvents(params).promise()
const result = await eventbridge.send(new PutEventsCommand(params))
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.

console.log(result)
}
}