Skip to content

Commit 130d29a

Browse files
authored
Merge pull request #27789 from KevinEady/f-ivs-stream_key
new data source: aws_ivs_stream_key
2 parents 31fb377 + f279e30 commit 130d29a

File tree

10 files changed

+267
-0
lines changed

10 files changed

+267
-0
lines changed

.changelog/27789.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-data-source
2+
aws_ivs_stream_key
3+
```

examples/ivs/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# IVS (Interactive Video Service) Example
2+
3+
This example shows how to deploy an AWS IVS channel using Terraform only. The
4+
example creates an S3 bucket for recording.
5+
6+
To run, configure your AWS provider as described in https://www.terraform.io/docs/providers/aws/index.html
7+
8+
## Running the example
9+
10+
Run `terraform apply` to see it work.
11+
12+
By default, resources are created in the `us-west-2` region. To override the
13+
region, set the variable `aws_region` to a different value.

examples/ivs/main.tf

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
terraform {
2+
required_version = ">= 0.12"
3+
}
4+
5+
provider "aws" {
6+
region = var.aws_region
7+
}
8+
9+
resource "aws_s3_bucket" "example" {
10+
bucket_prefix = "tf-ivs-stream-archive"
11+
force_destroy = true
12+
}
13+
14+
resource "aws_ivs_recording_configuration" "example" {
15+
name = "tf-ivs-recording-configuration"
16+
destination_configuration {
17+
s3 {
18+
bucket_name = aws_s3_bucket.example.id
19+
}
20+
}
21+
}
22+
23+
resource "aws_ivs_channel" "example" {
24+
name = "tf-ivs-channel"
25+
recording_configuration_arn = aws_ivs_recording_configuration.example.arn
26+
}
27+
28+
data "aws_ivs_stream_key" "example" {
29+
channel_arn = aws_ivs_channel.example.arn
30+
}

examples/ivs/outputs.tf

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
output "ingest_endpoint" {
2+
value = aws_ivs_channel.example.ingest_endpoint
3+
}
4+
5+
output "stream_key" {
6+
value = data.aws_ivs_stream_key.example.value
7+
}
8+
9+
output "playback_url" {
10+
value = aws_ivs_channel.example.playback_url
11+
}

examples/ivs/variables.tf

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
variable "aws_region" {
2+
description = "The AWS region to create things in."
3+
default = "us-west-2"
4+
}

internal/provider/provider.go

+2
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,8 @@ func New(_ context.Context) (*schema.Provider, error) {
716716

717717
"aws_iot_endpoint": iot.DataSourceEndpoint(),
718718

719+
"aws_ivs_stream_key": ivs.DataSourceStreamKey(),
720+
719721
"aws_msk_broker_nodes": kafka.DataSourceBrokerNodes(),
720722
"aws_msk_cluster": kafka.DataSourceCluster(),
721723
"aws_msk_configuration": kafka.DataSourceConfiguration(),

internal/service/ivs/find.go

+46
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,49 @@ func FindChannelByID(ctx context.Context, conn *ivs.IVS, arn string) (*ivs.Chann
7878

7979
return out.Channel, nil
8080
}
81+
82+
func FindStreamKeyByChannelID(ctx context.Context, conn *ivs.IVS, channelArn string) (*ivs.StreamKey, error) {
83+
in := &ivs.ListStreamKeysInput{
84+
ChannelArn: aws.String(channelArn),
85+
}
86+
out, err := conn.ListStreamKeysWithContext(ctx, in)
87+
if tfawserr.ErrCodeEquals(err, ivs.ErrCodeResourceNotFoundException) {
88+
return nil, &resource.NotFoundError{
89+
LastError: err,
90+
LastRequest: in,
91+
}
92+
}
93+
94+
if err != nil {
95+
return nil, err
96+
}
97+
98+
if len(out.StreamKeys) < 1 {
99+
return nil, &resource.NotFoundError{
100+
LastRequest: in,
101+
}
102+
}
103+
104+
streamKeyArn := out.StreamKeys[0].Arn
105+
106+
return findStreamKeyByID(ctx, conn, *streamKeyArn)
107+
}
108+
109+
func findStreamKeyByID(ctx context.Context, conn *ivs.IVS, id string) (*ivs.StreamKey, error) {
110+
in := &ivs.GetStreamKeyInput{
111+
Arn: aws.String(id),
112+
}
113+
out, err := conn.GetStreamKeyWithContext(ctx, in)
114+
if tfawserr.ErrCodeEquals(err, ivs.ErrCodeResourceNotFoundException) {
115+
return nil, &resource.NotFoundError{
116+
LastError: err,
117+
LastRequest: in,
118+
}
119+
}
120+
121+
if err != nil {
122+
return nil, err
123+
}
124+
125+
return out.StreamKey, nil
126+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package ivs
2+
3+
import (
4+
"context"
5+
6+
"github.com/aws/aws-sdk-go/aws"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/hashicorp/terraform-provider-aws/internal/conns"
10+
"github.com/hashicorp/terraform-provider-aws/internal/create"
11+
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
12+
"github.com/hashicorp/terraform-provider-aws/names"
13+
)
14+
15+
func DataSourceStreamKey() *schema.Resource {
16+
return &schema.Resource{
17+
ReadWithoutTimeout: dataSourceStreamKeyRead,
18+
Schema: map[string]*schema.Schema{
19+
"arn": {
20+
Type: schema.TypeString,
21+
Computed: true,
22+
},
23+
"channel_arn": {
24+
Type: schema.TypeString,
25+
Required: true,
26+
},
27+
"value": {
28+
Type: schema.TypeString,
29+
Computed: true,
30+
},
31+
"tags": tftags.TagsSchemaComputed(),
32+
},
33+
}
34+
}
35+
36+
const (
37+
DSNameStreamKey = "Stream Key Data Source"
38+
)
39+
40+
func dataSourceStreamKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
41+
conn := meta.(*conns.AWSClient).IVSConn
42+
43+
channelArn := d.Get("channel_arn").(string)
44+
45+
out, err := FindStreamKeyByChannelID(ctx, conn, channelArn)
46+
if err != nil {
47+
return create.DiagError(names.IVS, create.ErrActionReading, DSNameStreamKey, channelArn, err)
48+
}
49+
50+
d.SetId(aws.StringValue(out.Arn))
51+
52+
d.Set("arn", out.Arn)
53+
d.Set("channel_arn", out.ChannelArn)
54+
d.Set("value", out.Value)
55+
56+
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig
57+
58+
//lintignore:AWSR002
59+
if err := d.Set("tags", KeyValueTags(out.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
60+
return create.DiagError(names.IVS, create.ErrActionSetting, DSNameStreamKey, d.Id(), err)
61+
}
62+
63+
return nil
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package ivs_test
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"testing"
7+
8+
"github.com/aws/aws-sdk-go/service/ivs"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
11+
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
12+
)
13+
14+
func TestAccIVSStreamKeyDataSource_basic(t *testing.T) {
15+
dataSourceName := "data.aws_ivs_stream_key.test"
16+
channelResourceName := "aws_ivs_channel.test"
17+
18+
resource.ParallelTest(t, resource.TestCase{
19+
PreCheck: func() { acctest.PreCheck(t) },
20+
ErrorCheck: acctest.ErrorCheck(t, ivs.EndpointsID),
21+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
22+
Steps: []resource.TestStep{
23+
{
24+
Config: testAccStreamKeyDataSourceConfig_basic(),
25+
Check: resource.ComposeTestCheckFunc(
26+
testAccCheckStreamKeyDataSource(dataSourceName),
27+
resource.TestCheckResourceAttrPair(dataSourceName, "channel_arn", channelResourceName, "id"),
28+
resource.TestCheckResourceAttrSet(dataSourceName, "value"),
29+
acctest.MatchResourceAttrRegionalARN(dataSourceName, "arn", "ivs", regexp.MustCompile(`stream-key/.+`)),
30+
),
31+
},
32+
},
33+
})
34+
}
35+
36+
func testAccCheckStreamKeyDataSource(n string) resource.TestCheckFunc {
37+
return func(s *terraform.State) error {
38+
rs, ok := s.RootModule().Resources[n]
39+
if !ok {
40+
return fmt.Errorf("Can't find Stream Key data source: %s", n)
41+
}
42+
43+
if rs.Primary.ID == "" {
44+
return fmt.Errorf("Stream Key data source ID not set")
45+
}
46+
return nil
47+
}
48+
}
49+
50+
func testAccStreamKeyDataSourceConfig_basic() string {
51+
return `
52+
resource "aws_ivs_channel" "test" {
53+
}
54+
55+
data "aws_ivs_stream_key" "test" {
56+
channel_arn = aws_ivs_channel.test.arn
57+
}
58+
`
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
subcategory: "IVS (Interactive Video)"
3+
layout: "aws"
4+
page_title: "AWS: aws_ivs_stream_key"
5+
description: |-
6+
Terraform data source for managing an AWS IVS (Interactive Video) Stream Key.
7+
---
8+
9+
# Data Source: aws_ivs_stream_key
10+
11+
Terraform data source for managing an AWS IVS (Interactive Video) Stream Key.
12+
13+
## Example Usage
14+
15+
### Basic Usage
16+
17+
```terraform
18+
data "aws_ivs_stream_key" "example" {
19+
channel_arn = "arn:aws:ivs:us-west-2:326937407773:channel/0Y1lcs4U7jk5"
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
The following arguments are required:
26+
27+
* `channel_arn` - (Required) ARN of the Channel.
28+
29+
## Attributes Reference
30+
31+
In addition to all arguments above, the following attributes are exported:
32+
33+
* `arn` - ARN of the Stream Key.
34+
* `tags` - Map of tags assigned to the resource.
35+
* `value` - Stream Key value.

0 commit comments

Comments
 (0)