-
Notifications
You must be signed in to change notification settings - Fork 9.1k
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
Support for IOT Rule Action "dynamoDBv2" #4534
Comments
Hi, Also need dynamodbv2 for some projects, any updates ? Regards, |
I've hacked together support for this as I needed it in the meantime. I do not recommend using this patch if you want your statefile to be valid once support for DDBv2 rules is properly added. https://gist.github.com/gmarkey/1fe21a6d16b35d8b73c2600b73684f74 |
Hi, Any update ? |
here is my workaround locals {
prefix = "test-iot-action-dynamodb2"
topic = "${local.prefix}/statuslog"
rule = "${replace("${local.prefix}-rule", "-", "_")}"
}
resource "aws_dynamodb_table" "dynamodb_table" {
name = "${local.prefix}-table"
read_capacity = 2
write_capacity = 2
hash_key = "DeviceId"
range_key = "TimeStamp"
attribute = [{
name = "DeviceId"
type = "S"
}, {
name = "TimeStamp"
type = "N"
}]
}
resource "aws_iam_role" "iot_rule" {
name = "${local.prefix}-iot_rule"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "iot.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
data "template_file" "iot_rule" {
template = <<EOF
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Action": "dynamodb:PutItem",
"Resource": "$${table_arn}"
}
}
EOF
vars {
table_arn = "${aws_dynamodb_table.dynamodb_table.arn}"
}
}
resource "aws_iam_policy" "iot_rule" {
name = "${local.prefix}-iot_rule"
policy = "${data.template_file.iot_rule.rendered}"
}
resource "aws_iam_role_policy_attachment" "iot_rule" {
role = "${aws_iam_role.iot_rule.name}"
policy_arn = "${aws_iam_policy.iot_rule.arn}"
}
# resource "aws_iot_topic_rule" "iot_rule" {
# # MUST be ^[a-zA-Z0-9_]+$
# name = "${replace("${local.rule}", "-", "_")}"
# sql = "SELECT * FROM '${local.topic}'"
# sql_version = "2016-03-23"
# dynamodbv2 {
# role_arn = "${aws_iam_role.iot_rule.arn}"
# table_name = "${aws_dynamodb_table.dynamodb_table.arn}"
# }
# }
data "template_file" "iot_rule_patch" {
template = <<EOF
{
"sql": "SELECT * FROM '${local.topic}'",
"awsIotSqlVersion": "2016-03-23",
"actions": [
{
"dynamoDBv2": {
"roleArn": "$${role_arn}",
"putItem": {
"tableName": "$${table_name}"
}
}
}
]
}
EOF
vars {
role_arn = "${aws_iam_role.iot_rule.arn}"
table_name = "${aws_dynamodb_table.dynamodb_table.arn}"
}
}
resource "null_resource" "iot_rule_patch" {
provisioner "local-exec" {
command = "(aws iot delete-topic-rule --rule-name ${local.rule} || : ) && aws iot create-topic-rule --rule-name ${local.rule} --topic-rule-payload '${data.template_file.iot_rule_patch.rendered}'"
}
depends_on = [
"aws_iam_role_policy_attachment.iot_rule"
]
} |
Thanks for your workaround! Now I ran into another problem: I simply cannot use the '+' wildcard character within the sql statement: The solution for me was to not load the template_file directly into the cli call, but store it in a local_file instead. When calling create-topic-rule I then reference the locally stored file. Following is my code working with wildcard characters within the sql statement: data "template_file" "iot_rule_patch" {
template = <<EOF
{
"sql": "SELECT CONCAT(\"(\",topic(3),\":\",topic(5),\")\") AS idTuple, timestamp, temperature, humidity, pressure, batteryLevel FROM '$${iot_rule_data_topic}/Gateways/+/Sensors/+/data/'",
"awsIotSqlVersion": "2016-03-23",
"actions": [
{
"dynamoDBv2": {
"roleArn": "$${role_arn}",
"putItem": {
"tableName": "$${table_name}"
}
}
}
]
}
EOF
vars {
role_arn = "${aws_iam_role.iot_rule_role.arn}"
table_name = "${aws_dynamodb_table.table_raw_data.name}"
iot_rule_data_topic = "${local.service_name}"
}
}
resource "local_file" "iot_rule_payload" {
filename = "./iot_rule_payload"
content = "${data.template_file.iot_rule_patch.rendered}"
}
resource "null_resource" "iot_rule_patch" {
provisioner "local-exec" {
command = "aws iot delete-topic-rule --rule-name ${local.iot_rule_name} || :"
}
provisioner "local-exec" {
command = "aws iot create-topic-rule --rule-name ${local.iot_rule_name} --topic-rule-payload file://iot_rule_payload"
}
depends_on = [
"aws_iam_role_policy.iot_rule_role_policy"
]
} |
Support for this functionality has been merged and will release with version 2.62.0 of the Terraform AWS Provider, later this week. Thanks to @edwardbrowncross for the implementation. 👍 |
This has been released in version 2.62.0 of the Terraform AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template for triage. Thanks! |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks! |
Community Note
Description
There is already a dynamodb IOT topic rule ("aws_iot_topic_rule"), but the dynamodb version 2 rule is not yet suported. It would be nice to have that also.
The version 2.0 rule action is called something like "Split message into multiple columns of a database table (DynamoDB Version 2)" and it's available via AWS Management console.
New or Affected Resource(s)
Potential Terraform Configuration
References
See "dynamoDBv2" rule
The text was updated successfully, but these errors were encountered: