forked from hashicorp/terraform-provider-aws
/
resource_aws_flow_log.go
210 lines (176 loc) · 5.22 KB
/
resource_aws_flow_log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package aws
import (
"fmt"
"log"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceAwsFlowLog() *schema.Resource {
return &schema.Resource{
Create: resourceAwsLogFlowCreate,
Read: resourceAwsLogFlowRead,
Delete: resourceAwsLogFlowDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"iam_role_arn": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"log_destination": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"log_group_name"},
ValidateFunc: validateArn,
StateFunc: func(arn interface{}) string {
// aws_cloudwatch_log_group arn attribute references contain a trailing `:*`, which breaks functionality
return strings.TrimSuffix(arn.(string), ":*")
},
},
"log_destination_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: ec2.LogDestinationTypeCloudWatchLogs,
ValidateFunc: validation.StringInSlice([]string{
ec2.LogDestinationTypeCloudWatchLogs,
ec2.LogDestinationTypeS3,
}, false),
},
"log_group_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"log_destination"},
Deprecated: "use 'log_destination' argument instead",
},
"vpc_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"subnet_id", "eni_id"},
},
"subnet_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"eni_id", "vpc_id"},
},
"eni_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"subnet_id", "vpc_id"},
},
"traffic_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceAwsLogFlowCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
types := []struct {
ID string
Type string
}{
{ID: d.Get("vpc_id").(string), Type: "VPC"},
{ID: d.Get("subnet_id").(string), Type: "Subnet"},
{ID: d.Get("eni_id").(string), Type: "NetworkInterface"},
}
var resourceId string
var resourceType string
for _, t := range types {
if t.ID != "" {
resourceId = t.ID
resourceType = t.Type
break
}
}
if resourceId == "" || resourceType == "" {
return fmt.Errorf("Error: Flow Logs require either a VPC, Subnet, or ENI ID")
}
opts := &ec2.CreateFlowLogsInput{
LogDestinationType: aws.String(d.Get("log_destination_type").(string)),
ResourceIds: []*string{aws.String(resourceId)},
ResourceType: aws.String(resourceType),
TrafficType: aws.String(d.Get("traffic_type").(string)),
}
if v, ok := d.GetOk("iam_role_arn"); ok && v != "" {
opts.DeliverLogsPermissionArn = aws.String(v.(string))
}
if v, ok := d.GetOk("log_destination"); ok && v != "" {
opts.LogDestination = aws.String(strings.TrimSuffix(v.(string), ":*"))
}
if v, ok := d.GetOk("log_group_name"); ok && v != "" {
opts.LogGroupName = aws.String(v.(string))
}
log.Printf(
"[DEBUG] Flow Log Create configuration: %s", opts)
resp, err := conn.CreateFlowLogs(opts)
if err != nil {
return fmt.Errorf("Error creating Flow Log for (%s), error: %s", resourceId, err)
}
if len(resp.FlowLogIds) > 1 {
return fmt.Errorf("Error: multiple Flow Logs created for (%s)", resourceId)
}
d.SetId(*resp.FlowLogIds[0])
return resourceAwsLogFlowRead(d, meta)
}
func resourceAwsLogFlowRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
opts := &ec2.DescribeFlowLogsInput{
FlowLogIds: []*string{aws.String(d.Id())},
}
resp, err := conn.DescribeFlowLogs(opts)
if err != nil {
log.Printf("[WARN] Error describing Flow Logs for id (%s)", d.Id())
d.SetId("")
return nil
}
if len(resp.FlowLogs) == 0 {
log.Printf("[WARN] No Flow Logs found for id (%s)", d.Id())
d.SetId("")
return nil
}
fl := resp.FlowLogs[0]
d.Set("traffic_type", fl.TrafficType)
d.Set("log_destination", fl.LogDestination)
d.Set("log_destination_type", fl.LogDestinationType)
d.Set("log_group_name", fl.LogGroupName)
d.Set("iam_role_arn", fl.DeliverLogsPermissionArn)
var resourceKey string
if strings.HasPrefix(*fl.ResourceId, "vpc-") {
resourceKey = "vpc_id"
} else if strings.HasPrefix(*fl.ResourceId, "subnet-") {
resourceKey = "subnet_id"
} else if strings.HasPrefix(*fl.ResourceId, "eni-") {
resourceKey = "eni_id"
}
if resourceKey != "" {
d.Set(resourceKey, fl.ResourceId)
}
return nil
}
func resourceAwsLogFlowDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
log.Printf(
"[DEBUG] Flow Log Destroy: %s", d.Id())
_, err := conn.DeleteFlowLogs(&ec2.DeleteFlowLogsInput{
FlowLogIds: []*string{aws.String(d.Id())},
})
if err != nil {
return fmt.Errorf("Error deleting Flow Log with ID (%s), error: %s", d.Id(), err)
}
return nil
}