Skip to content

Commit

Permalink
Merge pull request #6437 from terraform-providers/f-aws_lambda_event_…
Browse files Browse the repository at this point in the history
…source_mapping-starting_position_timestamp

resource/aws_lambda_event_source_mapping: Add starting_position_timestamp argument
  • Loading branch information
bflad authored Nov 13, 2018
2 parents 4b33184 + aeeee42 commit 81fbf28
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 7 deletions.
17 changes: 17 additions & 0 deletions aws/resource_aws_lambda_event_source_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

func resourceAwsLambdaEventSourceMapping() *schema.Resource {
Expand Down Expand Up @@ -48,6 +49,17 @@ func resourceAwsLambdaEventSourceMapping() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
lambda.EventSourcePositionAtTimestamp,
lambda.EventSourcePositionLatest,
lambda.EventSourcePositionTrimHorizon,
}, false),
},
"starting_position_timestamp": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.ValidateRFC3339TimeString,
},
"batch_size": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -136,6 +148,11 @@ func resourceAwsLambdaEventSourceMappingCreate(d *schema.ResourceData, meta inte
params.StartingPosition = aws.String(startingPosition.(string))
}

if startingPositionTimestamp, ok := d.GetOk("starting_position_timestamp"); ok {
t, _ := time.Parse(time.RFC3339, startingPositionTimestamp.(string))
params.StartingPositionTimestamp = aws.Time(t)
}

// IAM profiles and roles can take some time to propagate in AWS:
// http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html#launch-instance-with-role-console
// Error creating Lambda function: InvalidParameterValueException: The
Expand Down
110 changes: 110 additions & 0 deletions aws/resource_aws_lambda_event_source_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,38 @@ func TestAccAWSLambdaEventSourceMapping_changesInEnabledAreDetected(t *testing.T
})
}

func TestAccAWSLambdaEventSourceMapping_StartingPositionTimestamp(t *testing.T) {
var conf lambda.EventSourceMappingConfiguration
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_lambda_event_source_mapping.test"
startingPositionTimestamp := time.Now().UTC().Format(time.RFC3339)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLambdaEventSourceMappingDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLambdaEventSourceMappingConfigKinesisStartingPositionTimestamp(rName, startingPositionTimestamp),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaEventSourceMappingExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "starting_position", "AT_TIMESTAMP"),
resource.TestCheckResourceAttr(resourceName, "starting_position_timestamp", startingPositionTimestamp),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"starting_position",
"starting_position_timestamp",
},
},
},
})
}

func testAccCheckAWSLambdaEventSourceMappingIsBeingDisabled(conf *lambda.EventSourceMappingConfiguration) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).lambdaconn
Expand Down Expand Up @@ -449,6 +481,84 @@ func testAccCheckAWSLambdaEventSourceMappingAttributes(mapping *lambda.EventSour
}
}

func testAccAWSLambdaEventSourceMappingConfigKinesisBase(rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "test" {
name = %q
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_role_policy" "test" {
role = "${aws_iam_role.test.name}"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"kinesis:GetRecords",
"kinesis:GetShardIterator",
"kinesis:DescribeStream"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"kinesis:ListStreams"
],
"Resource": "*"
}
]
}
EOF
}
resource "aws_kinesis_stream" "test" {
name = %q
shard_count = 1
}
resource "aws_lambda_function" "test" {
filename = "test-fixtures/lambdatest.zip"
function_name = %q
handler = "exports.example"
role = "${aws_iam_role.test.arn}"
runtime = "nodejs4.3"
}
`, rName, rName, rName)
}

func testAccAWSLambdaEventSourceMappingConfigKinesisStartingPositionTimestamp(rName, startingPositionTimestamp string) string {
return testAccAWSLambdaEventSourceMappingConfigKinesisBase(rName) + fmt.Sprintf(`
resource "aws_lambda_event_source_mapping" "test" {
batch_size = 100
enabled = true
event_source_arn = "${aws_kinesis_stream.test.arn}"
function_name = "${aws_lambda_function.test.arn}"
starting_position = "AT_TIMESTAMP"
starting_position_timestamp = %q
}
`, startingPositionTimestamp)
}

func testAccAWSLambdaEventSourceMappingConfig_kinesis(roleName, policyName, attName, streamName,
funcName, uFuncName string) string {
return fmt.Sprintf(`
Expand Down
34 changes: 27 additions & 7 deletions website/docs/r/lambda_event_source_mapping.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,32 @@ For information about event source mappings, see [CreateEventSourceMapping][2] i

## Example Usage

### DynamoDB

```hcl
resource "aws_lambda_event_source_mapping" "example" {
event_source_arn = "${aws_dynamodb_table.example.stream_arn}"
function_name = "${aws_lambda_function.example.arn}"
starting_position = "LATEST"
}
```

### Kinesis

```hcl
resource "aws_lambda_event_source_mapping" "example" {
event_source_arn = "${aws_kinesis_stream.example.arn}"
function_name = "${aws_lambda_function.example.arn}"
starting_position = "LATEST"
}
```

### SQS

```hcl
resource "aws_lambda_event_source_mapping" "event_source_mapping" {
batch_size = 100
event_source_arn = "arn:aws:kinesis:REGION:123456789012:stream/stream_name"
enabled = true
function_name = "arn:aws:lambda:REGION:123456789012:function:function_name"
starting_position = "TRIM_HORIZON|LATEST"
resource "aws_lambda_event_source_mapping" "example" {
event_source_arn = "${aws_sqs_queue.sqs_queue_test.arn}"
function_name = "${aws_lambda_function.example.arn}"
}
```

Expand All @@ -31,7 +50,8 @@ resource "aws_lambda_event_source_mapping" "event_source_mapping" {
* `event_source_arn` - (Required) The event source ARN - can either be a Kinesis or DynamoDB stream.
* `enabled` - (Optional) Determines if the mapping will be enabled on creation. Defaults to `true`.
* `function_name` - (Required) The name or the ARN of the Lambda function that will be subscribing to events.
* `starting_position` - (Optional) The position in the stream where AWS Lambda should start reading. Must be one of either `TRIM_HORIZON` or `LATEST` if getting events from Kinesis or DynamoDB. Must not be provided if getting events from SQS.
* `starting_position` - (Optional) The position in the stream where AWS Lambda should start reading. Must be one of `AT_TIMESTAMP` (Kinesis only), `LATEST` or `TRIM_HORIZON` if getting events from Kinesis or DynamoDB. Must not be provided if getting events from SQS. More information about these positions can be found in the [AWS DynamoDB Streams API Reference](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_GetShardIterator.html) and [AWS Kinesis API Reference](https://docs.aws.amazon.com/kinesis/latest/APIReference/API_GetShardIterator.html#Kinesis-GetShardIterator-request-ShardIteratorType).
* `starting_position_timestamp` - (Optional) A timestamp in [RFC3339 format](https://tools.ietf.org/html/rfc3339#section-5.8) of the data record which to start reading when using `starting_position` set to `AT_TIMESTAMP`. If a record with this exact timestamp does not exist, the next later record is chosen. If the timestamp is older than the current trim horizon, the oldest available record is chosen.

## Attributes Reference

Expand Down

0 comments on commit 81fbf28

Please sign in to comment.