diff --git a/plugins/processors/aws/ec2/ec2.go b/plugins/processors/aws/ec2/ec2.go index 7126214152a51..088ec09c83f5f 100644 --- a/plugins/processors/aws/ec2/ec2.go +++ b/plugins/processors/aws/ec2/ec2.go @@ -124,6 +124,20 @@ func (r *AwsEc2Processor) Init() error { return errors.New("no tags specified in configuration") } + for _, tag := range r.ImdsTags { + if len(tag) == 0 || !isImdsTagAllowed(tag) { + return fmt.Errorf("not allowed metadata tag specified in configuration: %s", tag) + } + r.imdsTags[tag] = struct{}{} + } + if len(r.imdsTags) == 0 && len(r.EC2Tags) == 0 { + return errors.New("no allowed metadata tags specified in configuration") + } + + return nil +} + +func (r *AwsEc2Processor) Start(acc telegraf.Accumulator) error { ctx := context.Background() cfg, err := awsconfig.LoadDefaultConfig(ctx) if err != nil { @@ -161,21 +175,6 @@ func (r *AwsEc2Processor) Init() error { } } - for _, tag := range r.ImdsTags { - if len(tag) > 0 && isImdsTagAllowed(tag) { - r.imdsTags[tag] = struct{}{} - } else { - return fmt.Errorf("not allowed metadata tag specified in configuration: %s", tag) - } - } - if len(r.imdsTags) == 0 && len(r.EC2Tags) == 0 { - return errors.New("no allowed metadata tags specified in configuration") - } - - return nil -} - -func (r *AwsEc2Processor) Start(acc telegraf.Accumulator) error { if r.Ordered { r.parallel = parallel.NewOrdered(acc, r.asyncAdd, DefaultMaxOrderedQueueSize, r.MaxParallelCalls) } else { diff --git a/plugins/processors/aws/ec2/ec2_test.go b/plugins/processors/aws/ec2/ec2_test.go index 8eb599206ff99..88fd661eb5c07 100644 --- a/plugins/processors/aws/ec2/ec2_test.go +++ b/plugins/processors/aws/ec2/ec2_test.go @@ -13,8 +13,7 @@ func TestBasicStartup(t *testing.T) { p.Log = &testutil.Logger{} p.ImdsTags = []string{"accountId", "instanceId"} acc := &testutil.Accumulator{} - require.NoError(t, p.Start(acc)) - require.NoError(t, p.Stop()) + require.NoError(t, p.Init()) require.Len(t, acc.GetTelegrafMetrics(), 0) require.Len(t, acc.Errors, 0) @@ -26,8 +25,7 @@ func TestBasicStartupWithEC2Tags(t *testing.T) { p.ImdsTags = []string{"accountId", "instanceId"} p.EC2Tags = []string{"Name"} acc := &testutil.Accumulator{} - require.NoError(t, p.Start(acc)) - require.NoError(t, p.Stop()) + require.NoError(t, p.Init()) require.Len(t, acc.GetTelegrafMetrics(), 0) require.Len(t, acc.Errors, 0)