-
Notifications
You must be signed in to change notification settings - Fork 9.2k
/
resource_aws_flow_log.go
270 lines (226 loc) · 7.11 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
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-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)
func resourceAwsFlowLog() *schema.Resource {
return &schema.Resource{
Create: resourceAwsLogFlowCreate,
Read: resourceAwsLogFlowRead,
Update: resourceAwsLogFlowUpdate,
Delete: resourceAwsLogFlowDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"iam_role_arn": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateArn,
},
"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,
ValidateFunc: validation.StringInSlice([]string{
ec2.TrafficTypeAccept,
ec2.TrafficTypeAll,
ec2.TrafficTypeReject,
}, false),
},
"log_format": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"max_aggregation_interval": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Default: 600,
ValidateFunc: validation.IntInSlice([]int{60, 600}),
},
"tags": tagsSchema(),
},
}
}
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))
}
if v, ok := d.GetOk("log_format"); ok && v != "" {
opts.LogFormat = aws.String(v.(string))
}
if v, ok := d.GetOk("max_aggregation_interval"); ok {
opts.MaxAggregationInterval = aws.Int64(int64(v.(int)))
}
if v, ok := d.GetOk("tags"); ok && len(v.(map[string]interface{})) > 0 {
opts.TagSpecifications = ec2TagSpecificationsFromMap(d.Get("tags").(map[string]interface{}), ec2.ResourceTypeVpcFlowLog)
}
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.Unsuccessful) > 0 {
return fmt.Errorf("Error creating Flow Log for (%s), error: %s", resourceId, *resp.Unsuccessful[0].Error.Message)
}
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
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig
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)
d.Set("log_format", fl.LogFormat)
d.Set("max_aggregation_interval", fl.MaxAggregationInterval)
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)
}
if err := d.Set("tags", keyvaluetags.Ec2KeyValueTags(fl.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}
return nil
}
func resourceAwsLogFlowUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
if d.HasChange("tags") {
o, n := d.GetChange("tags")
if err := keyvaluetags.Ec2UpdateTags(conn, d.Id(), o, n); err != nil {
return fmt.Errorf("error updating tags: %s", err)
}
}
return resourceAwsLogFlowRead(d, meta)
}
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
}