Skip to content

Commit

Permalink
Merge pull request #20528 from ddrouin/d-add-rabbitmq-lambda-event-so…
Browse files Browse the repository at this point in the history
…urce-doc

D add rabbitmq lambda event source doc
  • Loading branch information
ewbankkit authored Aug 12, 2021
2 parents 81ac4bc + 3ed1ba7 commit d732d60
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 4 deletions.
154 changes: 152 additions & 2 deletions aws/resource_aws_lambda_event_source_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,44 @@ func TestAccAWSLambdaEventSourceMapping_ActiveMQ(t *testing.T) {
})
}

func TestAccAWSLambdaEventSourceMapping_RabbitMQ(t *testing.T) {
var v lambda.EventSourceMappingConfiguration
resourceName := "aws_lambda_event_source_mapping.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAWSSecretsManager(t)
testAccPartitionHasServicePreCheck("mq", t)
testAccPreCheckAWSMq(t)
},
ErrorCheck: testAccErrorCheck(t, lambda.EndpointsID, "mq", "secretsmanager"),
Providers: testAccProviders,
CheckDestroy: testAccCheckLambdaEventSourceMappingDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLambdaEventSourceMappingConfigRabbitMQ(rName, "100"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaEventSourceMappingExists(resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "batch_size", "100"),
resource.TestCheckResourceAttr(resourceName, "enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "queues.#", "1"),
resource.TestCheckTypeSetElemAttr(resourceName, "queues.*", "test"),
resource.TestCheckResourceAttr(resourceName, "source_access_configuration.#", "2"),
),
},
// batch_size became optional. Ensure that if the user supplies the default
// value, but then moves to not providing the value, that we don't consider this
// a diff.
{
PlanOnly: true,
Config: testAccAWSLambdaEventSourceMappingConfigRabbitMQ(rName, "null"),
},
},
})
}

func testAccCheckAWSLambdaEventSourceMappingIsBeingDisabled(conf *lambda.EventSourceMappingConfiguration) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).lambdaconn
Expand Down Expand Up @@ -1202,7 +1240,7 @@ resource "aws_lambda_function" "test" {
`, rName))
}

func testAccAWSLambdaEventSourceMappingConfigMQBase(rName string) string {
func testAccAWSLambdaEventSourceMappingConfigActiveMQBase(rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "test" {
name = %[1]q
Expand Down Expand Up @@ -1305,6 +1343,92 @@ resource "aws_secretsmanager_secret_version" "test" {
`, rName)
}

func testAccAWSLambdaEventSourceMappingConfigRabbitMQBase(rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "test" {
name = %[1]q
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow"
}]
}
EOF
}
resource "aws_iam_role_policy" "test" {
role = aws_iam_role.test.name
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"mq:DescribeBroker",
"secretsmanager:GetSecretValue",
"ec2:CreateNetworkInterface",
"ec2:DescribeNetworkInterfaces",
"ec2:DescribeVpcs",
"ec2:DeleteNetworkInterface",
"ec2:DescribeSubnets",
"ec2:DescribeSecurityGroups",
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
}
]
}
EOF
}
resource "aws_lambda_function" "test" {
filename = "test-fixtures/lambdatest.zip"
function_name = %[1]q
handler = "exports.example"
role = aws_iam_role.test.arn
runtime = "nodejs12.x"
}
resource "aws_mq_broker" "test" {
broker_name = %[1]q
engine_type = "RabbitMQ"
engine_version = "3.8.11"
host_instance_type = "mq.t3.micro"
authentication_strategy = "simple"
logs {
general = true
}
user {
username = "Test"
password = "TestTest1234"
}
publicly_accessible = true
}
resource "aws_secretsmanager_secret" "test" {
name = %[1]q
}
resource "aws_secretsmanager_secret_version" "test" {
secret_id = aws_secretsmanager_secret.test.id
secret_string = jsonencode({ username = "Test", password = "TestTest1234" })
}
`, rName)
}

func testAccAWSLambdaEventSourceMappingConfigKinesisStartingPositionTimestamp(rName, startingPositionTimestamp string) string {
return composeConfig(testAccAWSLambdaEventSourceMappingConfigKinesisBase(rName) + fmt.Sprintf(`
resource "aws_lambda_event_source_mapping" "test" {
Expand Down Expand Up @@ -1614,14 +1738,40 @@ func testAccAWSLambdaEventSourceMappingConfigActiveMQ(rName, batchSize string) s
batchSize = "null"
}

return composeConfig(testAccAWSLambdaEventSourceMappingConfigMQBase(rName), fmt.Sprintf(`
return composeConfig(testAccAWSLambdaEventSourceMappingConfigActiveMQBase(rName), fmt.Sprintf(`
resource "aws_lambda_event_source_mapping" "test" {
batch_size = %[1]s
event_source_arn = aws_mq_broker.test.arn
enabled = true
function_name = aws_lambda_function.test.arn
queues = ["test"]
source_access_configuration {
type = "BASIC_AUTH"
uri = aws_secretsmanager_secret_version.test.arn
}
}
`, batchSize))
}

func testAccAWSLambdaEventSourceMappingConfigRabbitMQ(rName, batchSize string) string {
if batchSize == "" {
batchSize = "null"
}

return composeConfig(testAccAWSLambdaEventSourceMappingConfigRabbitMQBase(rName), fmt.Sprintf(`
resource "aws_lambda_event_source_mapping" "test" {
batch_size = %[1]s
event_source_arn = aws_mq_broker.test.arn
enabled = true
function_name = aws_lambda_function.test.arn
queues = ["test"]
source_access_configuration {
type = "VIRTUAL_HOST"
uri = "/vhost"
}
source_access_configuration {
type = "BASIC_AUTH"
uri = aws_secretsmanager_secret_version.test.arn
Expand Down
43 changes: 41 additions & 2 deletions website/docs/r/lambda_event_source_mapping.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ subcategory: "Lambda"
layout: "aws"
page_title: "AWS: aws_lambda_event_source_mapping"
description: |-
Provides a Lambda event source mapping. This allows Lambda functions to get events from Kinesis, DynamoDB, SQS and Managed Streaming for Apache Kafka (MSK).
Provides a Lambda event source mapping. This allows Lambda functions to get events from Kinesis, DynamoDB, SQS, Amazon MQ and Managed Streaming for Apache Kafka (MSK).
---

# Resource: aws_lambda_event_source_mapping

Provides a Lambda event source mapping. This allows Lambda functions to get events from Kinesis, DynamoDB, SQS and Managed Streaming for Apache Kafka (MSK).
Provides a Lambda event source mapping. This allows Lambda functions to get events from Kinesis, DynamoDB, SQS, Amazon MQ and Managed Streaming for Apache Kafka (MSK).

For information about Lambda and how to use it, see [What is AWS Lambda?][1].
For information about event source mappings, see [CreateEventSourceMapping][2] in the API docs.
Expand Down Expand Up @@ -86,6 +86,45 @@ resource "aws_lambda_event_source_mapping" "example" {
}
```

### Amazon MQ (ActiveMQ)

```terraform
resource "aws_lambda_event_source_mapping" "example" {
batch_size = 10
event_source_arn = aws_mq_broker.example.arn
enabled = true
function_name = aws_lambda_function.example.arn
queues = ["example"]
source_access_configuration {
type = "BASIC_AUTH"
uri = aws_secretsmanager_secret_version.example.arn
}
}
```

### Amazon MQ (RabbitMQ)

```terraform
resource "aws_lambda_event_source_mapping" "example" {
batch_size = 1
event_source_arn = aws_mq_broker.example.arn
enabled = true
function_name = aws_lambda_function.example.arn
queues = ["example"]
source_access_configuration {
type = "VIRTUAL_HOST"
uri = "/example"
}
source_access_configuration {
type = "BASIC_AUTH"
uri = aws_secretsmanager_secret_version.example.arn
}
}
```

## Argument Reference

* `batch_size` - (Optional) The largest number of records that Lambda will retrieve from your event source at the time of invocation. Defaults to `100` for DynamoDB, Kinesis, MQ and MSK, `10` for SQS.
Expand Down

0 comments on commit d732d60

Please sign in to comment.