Я пытаюсь создать инфраструктуру AWS, используя Terraform.SQS, Lambda и CloudWatch.Я могу успешно создать инфраструктуру, но не могу связать ее должным образом.
т.е. CloudWatch будет запускать событие для лямбда-функции при добавлении / отправке сообщений SQS?
Я что-то здесь упускаю?
-----------------------------------------------------------
provider "aws" {
region = "${var.REGION}"
}
# SQS
resource "aws_sqs_queue" "sqs-test" {
name = "sqs-test"
delay_seconds = 0
max_message_size = 262144
message_retention_seconds = 345600
receive_wait_time_seconds = 10
}
#creating IAM roles to access Lamda.
resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
# Creating a Lambda function
resource "aws_lambda_function" "lambda-test" {
filename = "test.zip"
function_name = "lambda-test"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "main"
runtime = "go1.x"
environment {
variables = {
SQS_KEY = "${var.SQS_KEY}"
SQS_SECRET = "${var.SQS_SECRET}"
TESTING_QUEUE = "${aws_sqs_queue.sqs-test.id}"
}
}
}
# Creating a CloudWatch Events Rules where any changes in SQS will trigger Lamda function.
resource "aws_cloudwatch_event_rule" "console-cl-test" {
name = "capture-cl-sqs-test"
description = "Captures SQS events"
event_pattern = <<PATTERN
{
"detail-type": [
"Simple Queue Service"
]
}
PATTERN
}
# Assigning that event to trigger a Lamda function
resource "aws_cloudwatch_event_target" "lambda-tr-test" {
rule = "${aws_cloudwatch_event_rule.console-cl-test.name}"
target_id = "lambda-test"
arn = "${aws_lambda_function.lambda-test.arn}"
}
-----------------------------------------------------------