今回は、TerraformでS3 + Lambda + CloudWatchを使ってS3のバケットに置いたLambda関数(zipファイル)を定期実行する方法をまとめていきます。
基本的にそれぞれのリソースのソースコードを分けて載せていきますので、 お使いの環境に併せて必要な部分を参考にしてもらえたら嬉しいです!
1. Lambdaを置くS3バケットを作成
Lambda関数(zipファイル)を置くS3のバケットを作成します。
versioningはお好みでtrueにしてください。(デフォルトはfalseです)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Lambda関数(zipファイル)用バケット | |
resource "aws_s3_bucket" "lambda_bucket" { | |
bucket = "lambda-bucket" | |
acl = "private" | |
versioning { | |
enabled = true | |
} | |
} |
2. Lambdaを作成
Lambdaを作成します。
あくまでインフラリソースとしての作成なので、Lambdaの関数(zipファイル)は任意のものを使用してください。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Lambda本体 | |
resource "aws_lambda_function" "example_lambda" { | |
function_name = "example_lambda" | |
s3_bucket = aws_s3_bucket.lambda_bucket.bucket | |
s3_key = "example.zip" | |
handler = "example" | |
runtime = "go1.x" | |
timeout = "300" | |
role = aws_iam_role.iam_for_lambda.arn | |
publish = "true" | |
} | |
# Lambda用AssumeRole | |
resource "aws_iam_role" "iam_for_lambda" { | |
name = "iam_for_lambda" | |
assume_role_policy = data.aws_iam_policy_document.iam_for_lambda_document.json | |
} | |
# Lambda用AssumeRoleドキュメント | |
data "aws_iam_policy_document" "iam_for_lambda_document" { | |
statement { | |
sid = "1" | |
effect = "Allow" | |
actions = ["sts:AssumeRole"] | |
principals { | |
type = "Service" | |
identifiers = [ | |
"lambda.amazonaws.com", | |
] | |
} | |
} | |
} |
function_name | 任意の関数名 |
s3_bucket | zipファイルを置くバケット名(ここでは上記バケットを指定) |
s3_key | zipファイルを置く |
handler | 実行ハンドラ名 |
runtime | 使用言語バージョン |
timeout | タイムアウト |
role | Lambdaにアタッチするロールのリソース |
publish | Lambdaのバージョン管理を有効化 |
3. CloudWatchのイベントを作成
CloudWatchのScheduled Eventを作成します。
イベントは通常のクーロン形式で記入できます。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CloudWatchのルールリソース | |
resource "aws_cloudwatch_event_rule" "run_lambda_rule" { | |
name = "run_lambda_rule" | |
description = "Run Lambda Rule" | |
schedule_expression = "cron(0/5 * * * ? *)" | |
lifecycle { | |
ignore_changes = ["schedule_expression"] | |
} | |
is_enabled = "true" | |
} | |
# ルールを適用させるターゲット(今回はlambdaを指定する) | |
resource "aws_cloudwatch_event_target" "run_lambda_target" { | |
rule = aws_cloudwatch_event_rule.run_lambda_rule.name | |
target_id = "sample_run_lambda" | |
arn = aws_lambda_function.example_lambda.arn | |
depends_on = [aws_cloudwatch_event_rule.run_lambda_rule, ] | |
} |
4. イベントをLambda側で許可する
最後に、作成したCloudWatchのイベントを実行することを、Lambda側で許可します。
(Permissionを与える)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# クラウドウォッチによる定期実行を許可 | |
resource "aws_lambda_permission" "run_lambda_permission" { | |
statement_id = "AllowExecutionFromCloudwatchEvent" | |
action = "lambda:InvokeFunction" | |
function_name = aws_lambda_function.example_lambda.function_name | |
principal = "events.amazonaws.com" | |
source_arn = aws_cloudwatch_event_rule.run_lambda_rule.arn | |
} |